思维导图
顺序栈
#include <iostream>
using namespace std;
class My_stack
{
private:
int *ptr;
int top;
int length;
public:
My_stack():ptr(new int[10]),top(-1),length(10){cout<<"无参创建"<<endl;}
My_stack(int n):ptr(new int[n]),top(-1),length(n){cout<<"有参创建"<<endl;}
~My_stack()
{
cout<<"stack 析构"<<endl;
delete []ptr;
}
int isempty();
int isfull();
void input(int &data);
void output();
void erg();
int &gettop()
{
return ptr[top];
}
};
void My_stack::erg()
{
for(int i=top;i>=0;i--)
{
cout<<ptr[i]<<endl;
}
}
void My_stack::input(int &data)
{
ptr[++top]=data;
}
void My_stack::output()
{
cout<<ptr[top--]<<endl;
}
int My_stack::isempty()
{
if(top==-1)
{
cout<<"栈为空"<<endl;
return 0;
}
else
{
cout<<"栈不为空"<<endl;
return 1;
}
}
int My_stack::isfull()
{
if(top==length-1)
{
cout<<"栈满"<<endl;
return 0;
}
else
{
cout<<"栈未满"<<endl;
return 1;
}
}
int main()
{
int len;
cout<<"请输入栈的长度"<<endl;
cin>>len;
My_stack m1(len);
string s;
while(1)
{
cout<<"输入指令"<<endl;
cin>>s;
if(s=="input")
{
char s1;
cout<<"是否进栈"<<endl;
cin>>s1;
while(s1=='y'&&m1.isfull())
{
int temp;
cout<<"输入进栈值"<<endl;
cin>>temp;
m1.input(temp);
cout<<"是否继续进栈"<<endl;
cin>>s1;
}
}
else if(s=="output")
{
char s1;
cout<<"是否出栈"<<endl;
cin>>s1;
while(s1=='y'&&m1.isempty())
{
m1.output();
cout<<"是否继续出栈"<<endl;
cin>>s1;
}
}
else if(s=="ergodic")
{
m1.erg();
}
else if(s=="gettop")
{
cout<<m1.gettop()<<endl;
}
else if(s=="exit")
{
break;
}
}
m1.~My_stack();
return 0;
}
循环队列
#include <iostream>
using namespace std;
template <typename T>
class Cricle
{
private:
T *ptr; //首地址
int size; //长度
int front; //队头
int rear; //队尾
public:
Cricle(){};
Cricle(int s):ptr(new T[size]),size(s),front(0),rear(0){};
~Cricle()
{
delete []ptr;
};
Cricle &operator = (const Cricle &other)
{
if(this == &other)
{
return *this;
}
delete []ptr;
return strncat(this->ptr,&other.ptr,size);
}
bool emtpy()
{
return front == rear;
}
bool full()
{
return front = (rear+1)%size;
}
int setsize()
{
return size = front;
}
int push(T data)
{
if(full())
{
return -1;
}
rear = (rear +1)%size;
return 0;
}
int pop()
{
if(emtpy())
{
return -1;
}
front = (front +1)%size;
return 0;
}
int getsize()
{
return (size - front+rear)%size;
}
void show()
{
if(emtpy())
{
return ;
}
for(int i=front;i!=rear;i=(i+1)%size)
{
cout<<ptr[i]<<endl;
}
}
};
int main()
{
return 0;
}