1.顺序表:
list.hpp
#ifndef LIST_HPP
#define LIST_HPP
#include <iostream>
using namespace std;
template <class L>
class Seqlist
{
private:
L *ptr;
L size;
L len=0;
public:
void init(L n)
{
//堆区申请空间(大小为n)
this->ptr=new L[n];
//bzero(this->ptr,sizeof(L)*n);
this->len=0;
this->size=n;
//
}
bool empty()
{
return this->len==0;
}
bool full()
{
return this->len==this->size;
}
void push_back(L e)//尾插
{
if(this->full())
{
return ;
}
this->ptr[len++]=e;
}
//插入
void insert(L index,L num)
{
if(full())
{
return;
}
for(L i=len-1;i>=index-1;i--)
{
ptr[i+1]=ptr[i];
}
ptr[index-1]=num;
len++;
}
//任意位置删除
void erase(L index)
{
if(empty())
{
return;
}
for(L i=index;i<len;i++)
{
ptr[i-1]=ptr[i];
ptr[i]=NULL;
}
len--;
}
//尾删
void pop_back()
{
if(empty())
{
return;
}
ptr[len-1]=NULL;
len--;
}
void show()
{//判空
if(len==0)
{
return;
}
cout<<"顺序表"<<endl;
for(int i=0;i<len;i++)
{
cout<<ptr[i]<<ends;
}
cout<<endl;
}
//当前长度
L cur_size()
{
if(empty())
{
return 0;
}
else
{
return len;
}
}
L at(L index)
{
L num;
num=ptr[index-1];
return num;
}
void sort(bool flag)
{
if(flag)
{
for(int i=1;i<len;i++)
{
for(int j=0;j<len-i;j++)
{
if(ptr[j]>ptr[j+1])
{
L temp;
temp=ptr[j];
ptr[j]=ptr[j+1];
ptr[j+1]=temp;
}
}
}
}
else
{
for(int i=1;i<len;i++)
{
for(int j=0;j<len-i;j++)
{
if(ptr[j]<ptr[j+1])
{
L temp;
temp=ptr[j];
ptr[j]=ptr[j+1];
ptr[j+1]=temp;
}
}
}
}
}
};
#endif // LIST_HPP
2.栈:
strack.hpp
#ifndef STACK_HPP
#define STACK_HPP
#include <iostream>
#include <exception>
using namespace std;
template <class T>
class StackNode
{
public:
T data;//存储数据
StackNode *next;//指向下一个节点
//构造函数
StackNode(T d):data(d),next(nullptr){}
};
template <class T>
class Stack
{
private:
StackNode<T> *top;//指向栈顶
int len;//栈中元素数量
public:
Stack():top(nullptr),len(0){}
//析构函数
~Stack()
{
while(!empty())
{
pop();
}
}
bool empty() //判断栈是否为空
{
return top==nullptr;
}
int size()//获取栈的大小
{
return len;
}
//压栈操作
void push(T element)
{
StackNode<T> *newnode=new StackNode<T>(element);//申请空间并初始化
newnode->next=top;
top=newnode;
len++;
}
//出栈操作
T pop()
{
if(empty())
{
throw out_of_range("空栈");
}
StackNode<T> *temp=top;
T value=top->data;
top=top->next;
delete temp;
len--;
return value;
}
//查看栈顶元素
T look_top()
{
if(empty())
{
throw out_of_range("空栈");
}
return top->data;
}
//清空栈
void clear()
{
while (!empty())
{
pop();
}
}
Stack& operator=(const Stack& other)
{
if (this != &other)
{
clear(); // 清空当前栈
// 复制元素
StackNode<T> *current = other.top;
while (current != nullptr)
{
push(current->data);
current = current->next;
}
}
return *this;
}
void swap(Stack& other)
{
StackNode<T> * tempTop = top;
top = other.top;
other.top = tempTop;
T templen = len;
len = other.len;
other.len = templen;
}
};
#endif // STACK_HPP
3.队列:
queue.hpp
#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
#include <exception>
using namespace std;
template <class T>
class QueueNode
{
public:
T data;
QueueNode *next;
//有参构造
QueueNode(T d):data(d),next(nullptr){}
};
template <class T>
class Queue
{
private:
QueueNode<T>* front; // 指向队列头部的指针
QueueNode<T>* rear; // 指向队列尾部的指针
int count; // 队列中元素的数量
public:
// 构造函数
Queue() : front(nullptr), rear(nullptr), count(0)
{
}
~Queue()
{
clear();
}
void clear()
{
while (front != nullptr)
{
QueueNode<T>* temp = front;
front = front->next;
delete temp;
}
rear = nullptr;
count = 0;
}
// 查看队列前端元素
T frontElement()
{
if (empty())
{
throw std::out_of_range("空队列");
}
return front->data;
}
// 查看队列尾部元素
T backElement()
{
if (empty())
{
throw std::out_of_range("空队列");
}
return rear->data;
}
//判断队列是否为空
bool empty()
{
return front == nullptr;
}
// 获取队列的大小
int size()
{
return count;
}
// 入队操作
void push(T element)
{
QueueNode<T>* newNode = new QueueNode<T>(element);
if (rear == nullptr) // 队列为空
{
front = rear = newNode;
} else
{
rear->next = newNode;
rear = newNode;
}
count++;
}
// 出队操作
T pop()
{
if (empty()) {
throw std::out_of_range("空队列");
}
QueueNode<T>* temp = front;
T dequeuedValue = front->data;
front = front->next;
if (front == nullptr) {
rear = nullptr;
}
delete temp;
count--;
return dequeuedValue;
}
void swap(Queue& other) {
using std::swap;
swap(front, other.front);
swap(rear, other.rear);
swap(count, other.count);
}
Queue& operator=(const Queue& other)
{
if (this != &other)
{
Queue temp(other); // 临时对象,用于深拷贝
swap(temp); // 与临时对象交换内容
}
return *this;
}
};
#endif // QUEUE_H