#include <iostream>
#define MAX 128
using namespace std;
class Queue
{
private:
int *p=new int[MAX];//队列的数组
int tail;//记录队尾元素
int head;//记录对头元素
public:
//构造函数
Queue(int t=0)
{
head=t;
tail=t;
cout<<"无参构造函数"<<endl;
}
//析构函数
~Queue()
{
cout<<"Stack::析构函数"<<endl;
}
//拷贝构造函数
Queue(const Queue &other):p(other.p),tail(other.tail),head(other.head)
{
cout<<"拷贝构造函数"<<endl;
}
//入队
int queue_push(int e)
{
if(queue_full())
{
cout<<"入队失败"<<endl;
return -1;
}
p[tail]=e;
tail++;
cout<<"入队成功"<<endl;
return 0;
}
//出队
int queue_pop()
{
if(queue_empty())
{
cout<<"出队失败"<<endl;
return -1;
}
int e = p[head];
head = (head+1)%MAX;
cout<<e<<" 出队成功"<<endl;
return 0;
}
//清空队列
bool queue_delete()
{
while(head!=tail)
{
queue_pop();
}
delete [] p;
p=nullptr;
cout<<"清空队列成功"<<endl;
return true;
}
//判空
bool queue_empty()
{
if(head==tail)
{
cout<<"队列空"<<endl;
return true;
}
return false;
}
//判满
bool queue_full()
{
if((tail+1)==0)
{
cout<<"队列满了"<<endl;
return true;
}
return false;
}
//队列的大小
void queue_getsize()
{
int size;
size=(tail-head+MAX)%MAX;
cout<<"队的大小为:"<<size<<endl;
}
void show(int i)
{
cout<<p[i]<<" ";
}
};
int main()
{
Queue q1;
int e;
int s;
q1.queue_empty();
cout<<"请输入要入队的个数:";
cin>>s;
for(int i=0;i<s;i++)
{
cout<<"请输入要入队的元素:";
cin>>e;
q1.queue_push(e);
}
q1.queue_getsize();
for(int i=0;i<s;i++)
{
q1.show(i);
}
cout<<endl;
q1.queue_delete();
return 0;
}
#include <iostream>
#define MAX 128
using namespace std;
class Stack
{
private:
int *p=new int[MAX];
int top;
public:
Stack()
{
top=-1;
cout<<"无参构造"<<endl;
}
~Stack()
{
cout<<"析构函数"<<endl;
}
Stack(const Stack &other):p(other.p),top(other.top)
{
cout<<"拷贝构造"<<endl;
}
//判空
int stack_empty()
{
return top==-1;
}
//判满
int stack_full()
{
return top==MAX-1;
}
//入栈
bool stack_push(int data)
{
top++;
p[top]=data;
cout<<"成功"<<data<<endl;
return true;
}
//出栈
bool stack_pop()
{
if(stack_empty())
{
return false;
}
int data=p[top];
top--;
cout<<data<<endl;
return true;
}
//清空
void stack_free()
{
while(top!=-1)
{
stack_pop();
}
delete [] p;
p=nullptr;
cout<<"清理完毕"<<endl;
}
//栈顶元素
int stack_top()
{
return p[top];
}
//大小
int stack_size()
{
return top+1;
}
};
int main()
{
Stack s;
s.stack_push(2);
s.stack_push(3);
s.stack_push(5);
s.stack_pop();
s.stack_pop();
s.stack_pop();
s.stack_size();
s.stack_free();
return 0;
}