栈
#include <iostream>
using namespace std;
//封装栈
template<typename T>
class myStack
{
private:
T *data;
int top=-1; //记录栈顶元素下标
int size; //栈的大小
public:
myStack():size(10)
{
data = new T[size];
top=-1;
}
myStack(int size)
{
data = new T[size];
top=-1;
this->size=size;
}
~myStack(){delete []data;}
//operator=
myStack &operator=(const myStack &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];
}
top=other.top;
return *this;
}
//访问栈顶元素
T my_top()
{
return data[top];
}
//判空
bool my_empty()
{
return top==-1;
}
//判满
bool my_full()
{
return top==size-1;
}
//返回容纳的元素数
int my_size()
{
return size;
}
//向栈顶插入元素
void my_push(const T &a)
{
//判断栈是否已经满
if(my_full())
{
cout<<"入栈失败!"<<endl;
return ;
}
//入栈
top++;
data[top]=a;
cout<<"入栈成功!"<<endl;
}
//删除栈顶元素
void my_pop()
{
if(my_empty())
{
cout<<"出栈失败!"<<endl;
return ;
}
//判断是否为空
cout<<data[top]<<"出栈成功!"<<endl;
top--;
}
//遍历栈
void show()
{
for(int i=0;i<=top;i++)
{
cout<<data[i]<<" ";
}
cout<<endl;
}
};
int main()
{
//插入
myStack<string> s;
s.my_push("hello");
s.my_push("a!");
s.my_push("my");
s.my_push("name");
s.show();
cout << "**************************" << endl;
//删除
s.my_pop();
s.my_pop();
s.show();
cout << "**************************" << endl;
//赋值
myStack<string> s1;
s1=s;
s1.show();
cout << "**************************" << endl;
//访问栈顶
cout<<s.my_top()<<endl;
return 0;
}
队列
#include <iostream>
using namespace std;
//模板库:封装队列
template<typename T>
class myQueue
{
private:
T *data; //队列容器
int size; //容器大小
int front; //队头下标
int back; //队尾下标
public:
myQueue():size(15)
{
data = new T[size];
size=15;
front=0;
back=0;
}
myQueue(int size)
{
data = new T[size];
this->size=size;
front=0;
back=0;
}
~myQueue(){delete []data;}
//operator=
myQueue &operator=(const myQueue &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;
back=other.back;
return *this;
}
//判空
bool my_empty()
{
return front==back;
}
//判满
bool my_full()
{
return (back+1)%size==front;
}
//访问当一个元素
int get_front()
{
if(my_empty())
{
cout<<"访问失败!"<<endl;
return -1;
}
return data[front];
}
//访问最后一个元素
int get_back()
{
if(my_empty())
{
cout<<"访问失败!"<<endl;
return -1;
}
//back=0,表示最后一个元素在末尾
if(back==0)
{
return data[size-1];
}
return data[back-1];
}
//返回容纳的元素个数
int my_size()
{
return size;
}
//尾插
void my_push(T a)
{
if(my_full())
{
cout<<"入队失败!"<<endl;
return ;
}
//入队
data[back]=a;
//队列变化
back=(back+1)%size;
cout<<"入队成功!"<<endl;
}
//头删
void my_pop()
{
if(my_empty())
{
cout<<"出队失败!"<<endl;
return ;
}
cout<<"出队成功!"<<endl;
//队头变化
front=(front+1)%size;
}
//遍历
void show()
{
if(my_empty())
{
cout<<"遍历失败!"<<endl;
}
for(int i=front;i!=back;i=(i+1)%size)
{
cout<<data[i]<<" ";
}
cout<<endl;
}
};
int main()
{
myQueue<int> q(10);
//入队
q.my_push(23);
q.my_push(22);
q.show();
//访问首尾元素
cout<<"首"<<q.get_front()<<endl;
cout<<"尾"<<q.get_back()<<endl;
//赋值
myQueue<int> q2;
q2=q;
q2.show();
//出队
q.my_pop();
q.show();
return 0;
}
思维导图