栈
#include <iostream>
using namespace std;
template <typename T>
class mystack
{
private:
T *data; //存储栈的容器
int top; //记录栈顶元素的下标
int size; //最大容量
public:
//无参构造
mystack():size(10)
{
data=new T [size];
top=-1;
size=10;
}
//有参构造函数
mystack(int s)
{
data=new T[s];
top=-1;
size=s;
}
//向栈顶插入元素
void push(T e)
{
if(top==size-1)
{
cout<<"栈已满"<<endl;
return;
}
top++; //偏移栈顶位置
data[top]=e; //将数据压入栈中
cout<<"插入成功"<<endl;
}
//赋值给容器适配器
mystack& operator=(const mystack& other)
{
if (this != &other) // 防止自赋值
{
// 释放当前对象的内存
delete[] data;
// 复制其他对象的属性
size = other.size;
top = other.top;
// 分配新内存并复制数据
data = new T[size];
for (int i = 0; i <= top; ++i)
{
data[i] = other.data[i];
}
}
return *this;
}
void show()
{
if(empty())
{
cout<<"栈是空的"<<endl;
}
for(int i=top;i>=0;i--)
{
cout<<data[i]<<" ";
}
cout<<endl;
}
//判空
bool empty()
{
return top==-1;
}
//返回容纳的元素数
int getsize()
{
return size;
}
//访问栈顶元素
T get()
{
if(empty())
{
cout<<"操作失败"<<endl;
}
return data[top];
}
//删除栈顶元素
void pop()
{
if(empty())
{
cout<<"栈为空,无法删除"<<endl;
}
cout<<"删除成功"<<endl;
top--;
}
};
int main()
{
mystack<double> s1;
s1.push(1.5);
s1.push(3.8);
s1.push(5.9);
s1.show();
cout<<"*******"<<endl;
s1.pop();
s1.show();
cout<<"*******"<<endl;
cout<<"栈顶元素为"<<s1.get()<<endl;
mystack<double> s2;
s2=s1;
s2.show();
return 0;
}
队列
#include <iostream>
using namespace std;
template <typename T>
class queue
{
private:
T *data;
int size;
int front;
int tail;
public:
//无参构造
queue():size(20)
{
data=new T [size];
front=0;
tail=0;
}
//有参构造
queue(int s, T e)
{
data=new T [size];
size=s;
front=0;
tail=0;
//判断逻辑
if(full())
{
cout<<"插入失败"<<endl;
}
//入队逻辑
data[tail]=e;
//队尾后移
tail=(tail+1)%size;
cout<<"入队成功"<<endl;
}
~queue()
{
delete []data;
}
//判空
int empty()
{
return front==tail;
}
//判满
int full()
{
return (tail+1)%size==front;
}
// 赋值给容器适配器
queue &operator=(const queue &other)
{
//防止自己给自己赋值
if(this==&other)
{
return *this;
}
//释放当前空间
delete []data;
//重新分配内存
size=other.size;
data=new T[size];
for(int i=0;i<size;i++)
{
data[i]=other.data[i];
}
front=other.front;
tail=other.tail;
return *this;
}
//访问第一个元素
T get()
{
if(empty())
{
cout<<"队列是空的"<<endl;
throw std::out_of_range("Queue is empty"); // 抛出异常
}
return data[front];
}
//访问最后一个元素
T get_tail()
{
if(empty())
{
cout<<"队列是空的"<<endl;
throw std::out_of_range("Queue is empty"); // 抛出异常
}
return data[tail-1];
}
//返回容纳的元素数
T get_size()
{
return size;
}
//向队列尾部插入元素
void push(T e)
{
//判断逻辑
if(full())
{
cout<<"插入失败"<<endl;
}
//入队逻辑
data[tail]=e;
//队尾后移
tail=(tail+1)%size;
cout<<"入队成功"<<endl;
}
//删除首个元素
void pop()
{
if(empty())
{
cout<<"删除失败"<<endl;
}
front=(front+1)%size;
}
//输出栈中的元素
void show()
{
//判断逻辑
if(empty())
{
cout<<"输出失败"<<endl;
return;
}
for(int i=front;i!=tail;i=(i+1)%size)
{
cout<<data[i]<<" ";
}
cout<<endl;
}
};
int main()
{
queue<string> q;
q.push("hello");
q.push("aa");
q.show();
q.show();
q.push("yyqx");
//q.push(7);
q.show();
cout<<"第一个成员是"<<q.get()<<endl;
cout<<"最后一个成员是"<<q.get_tail()<<endl;
cout<<"*******"<<endl;
//删除首个元素
q.pop();
q.show();
cout<<"******"<<endl;
//赋值
queue<string> q1;
q1=q;
q1.show();
return 0;
}
思维导图