1、顺序栈:
#include <iostream>
using namespace std;
const int StackSize=10;
class SeqStack{
public:
SeqStack(); //构造函数,初始化一个空栈
~SeqStack(){}
void Push(int x);
int Pop();
int GetTop();
int Empty();
private:
int data[StackSize];//存放栈元素的数组
int top; //栈顶指针,为栈顶元素在数组中的下标
};
SeqStack::SeqStack(){
top=-1;}
void SeqStack::Push(int x){
if(top==StackSize-1)throw "上溢";
data[++top]=x;}
int SeqStack::Pop(){
if(top==-1)throw"下溢";
int x=data[top--];
return x;}
int SeqStack::GetTop(){
if(top!=-1)
return data[top];}
int SeqStack::Empty()
if(top==-1)return 1;
else return 0;
}
void main(){
SeqStack k1;
if(k1.Empty())
cout<<"栈为空"<<"\n"<<endl;
else
cout<<"栈非空"<<endl;
cout<<"对99和88执行入栈操作"<<"\n"<<endl;
k1.Push(99);
k1.Push(88);
cout<<"栈顶元素为:"<<"\n"<<endl;
cout<<k1.GetTop()<<"\n"<<endl;
cout<<"执行一次出栈操作"<<"\n"<<endl;
k1.Pop();
cout<<"栈顶元素为:"<<"\n"<<endl;
cout<<k1.GetTop()<<"\n"<<endl;}
2、链栈:
#include<iostream>
using namespace std;
struct Node
{
int data;
Node *next;
};
class LinkStack{
public:
LinkStack();
~LinkStack();
void Push(int x);
int Pop();
int GetTop();
int Empty();
private:
Node *top;
};
//构造函数
LinkStack::LinkStack(){
top=new Node;
top=NULL;}
//压栈函数
void LinkStack::Push(int x){
Node *s=NULL;
s=new Node;
s->data=x;
s->next=top;
top=s;}
//弹栈函数
int LinkStack::Pop(){
if(top==NULL)throw"下溢";
else
int x;Node *p=NULL;
p=top;
x=top->data;
top=top->next;
delete p;
return x;}
//取栈顶元素
int LinkStack::GetTop(){
if(top!=NULL)
return top->data;}
//判空函数
int LinkStack::Empty(){
if(top==NULL)
return 1;
else return 0;}
//析构函数
LinkStack::~LinkStack(){
Node *q=NULL;
while(top!=NULL)
q=top;
top=top->next;
delete q;}
//主函数
void main(){
LinkStack s;
if(s.Empty())
cout<<"此栈为空!"<<"\n"<<endl;
else
cout<<"此栈不为空"<<"\n"<<endl;
cout<<"对99和88进行压栈操作""\n"<<endl;
s.Push(99);
s.Push(88);
cout<<"栈顶元素为:"<<"\n"<<endl;
cout<<s.GetTop()<<"\n"<<endl;
cout<<"执行一次弹栈操作"<<"\n"<<endl;
s.Pop();
cout<<"栈顶元素为:"<<"\n"<<endl;
cout<<s.GetTop()<<"\n"<<endl;}
3、顺序队列
#include <iostream>
using namespace std;
const int MAX=10;
class Queue{
private:
int front;
int rear;
int data[MAX];
public:
Queue(){front=rear=MAX-1;}
~Queue(){}
void EnQueue(int x);
int DeQueue();
int GetQueue(){if (front!=rear) return data[(front+1)%MAX];};
int Empty();
};
void Queue::EnQueue(int x){
if ((rear-1)%MAX==front) throw "上溢";
rear=(rear+1)%MAX;
data[rear]=x;
}
int Queue::DeQueue(){
if (front==rear) throw "下溢";
int x=data[front];
front=(front+1)%MAX;
return x;
}
int Queue::Empty(){
if (front==rear) return 1; else return 0;
}
int main(){
Queue S;
if (S.Empty())
cout<<"队为空!"<<endl;
else
cout<<"队不为空!"<<endl;
cout<<"1入队:"<<endl;
S.EnQueue(1);
cout<<"2入队:"<<endl;
S.EnQueue(2);
cout<<"取队头:"<<endl;
cout<<S.GetQueue()<<endl;
cout<<"一次出队:"<<endl;
S.DeQueue();
cout<<"取队头:"<<endl;
cout<<S.GetQueue()<<endl;
return 0;
}
4、链队列
#include <iostream>
using namespace std;
struct Data {
int data;
struct Data *next;
} *q;
class Queue{
private:
Data *rear,*front;
public:
Queue(){rear=NULL;front=NULL;}
~Queue(){}
void EnQueue(int x);
int DeQueue();
intGetQueue();
int Empty();
};
void Queue::EnQueue(int x){
q=new Data;
q->data=x;
q->next=NULL;
if (Empty())
front=rear=q; //若队为空,让队头和队尾指针都指向新增空间
else {
rear->next=q;
rear=q;}
}
int Queue::DeQueue(){
if (Empty()) throw "下溢";
q=front;
int x=front->data;
front=front->next;
delete q;
return x;
}
int Queue::GetQueue(){
if (!Empty())
return front->data;
}
int Queue::Empty(){
if (front==NULL&&rear==NULL) return 1;
else return 0;
}
int main(){
Queue S;
if (S.Empty())
cout<<"队为空!"<<endl;
else
cout<<"队不为空!"<<endl;
cout<<"10入队:"<<endl;
S.EnQueue(10);
cout<<"5入队:"<<endl;
S.EnQueue(5);
cout<<"取队头元素:"<<endl;
cout<<S.GetQueue()<<endl;
cout<<"一次出队:"<<endl;
S.DeQueue();
cout<<"取队头元素:"<<endl;
cout<<S.GetQueue()<<endl;
return 0;
}