1.顺序栈
#include<iostream.h>
const int StackSize=10;
template<class DataType>
class SeqStack
{
public:
SeqStack();
~SeqStack(){}
void Push(DataType x);
DataType Pop();
DataType GetTop();
int Empty();
private:
DataType data[StackSize];
int top;
};
template<class DataType>
SeqStack<DataType>::SeqStack()
{
top=-1;
}
template<class DataType>
void SeqStack<DataType>::Push(DataType x)
{
if(top==StackSize-1)throw"上溢";
top++;
data[top]=x;
}
template<class DataType>
DataType SeqStack<DataType>::Pop()
{
DataType x;
if(top==-1)throw"下溢";
x=data[top--];
return x;
}
template<class DataType>
DataType SeqStack<DataType>::GetTop()
{
if(top!=-1)
return data[top];
else
return 0;
}
template<class DataType>
int SeqStack<DataType>::Empty()
{
if(top==-1)return 1;
else return 0;
}
void main()
{
SeqStack<int>S;
if(S.Empty())
cout<<"栈为空"<<endl;
else cout<<"栈非空"<<endl;
cout<<"对15和10执行入栈操作"<<endl;
S.Push(15);
S.Push(10);
cout<<"栈顶元素为:"<<endl;
cout<<S.GetTop()<<endl;
cout<<"执行一次出栈操作"<<endl;
S.Pop();
cout<<"栈顶元素为:"<<endl;
cout<<S.GetTop()<<endl;
}
2.链栈
#include<iostream.h>
template<class DataType>
struct Node
{
DataType data;
Node<DataType>*next;
};
template<class DataType>
class LinkStack
{
public:
LinkStack(){top=NULL;}
~LinkStack(){};
void Push(DataType x);
DataType Pop();
DataType GetTop(){if(top!=NULL)return top->data;return 0;}
int Empty(){return top==NULL?1:0;}
private:
Node<DataType>*top;
};
template<class DataType>
void LinkStack<DataType>::Push(DataType x)
{ Node<DataType>*s;
s=new Node<DataType>;s->data=x;
s->next=top;top=s;
}
template<class DataType>
DataType LinkStack<DataType>::Pop()
{ int x;
Node<DataType>*p;
if(top==NULL)throw"下溢";
x=top->data;p=top;
top=top->next;
delete p;
return x;
}
void main()
{
LinkStack<int>S;
if(S.Empty())
cout<<"栈为空"<<endl;
else
cout<<"栈非空"<<endl;
cout<<"对15和10执行入栈操作"<<endl;
S.Push(15);
S.Push(10);
cout<<"栈顶元素为:"<<endl;
cout<<S.GetTop()<<endl;
cout<<"执行一次出栈操作"<<endl;
S.Pop();
cout<<"栈顶元素为:"<<endl;
cout<<S.GetTop()<<endl;
}
3.顺序队列
#include<iostream.h>
class queue
{
int front;
int rear;
int *array;
int size;
public:
queue(int MaxSize)
{
array=new int[MaxSize+1];
front=rear=0;
size=MaxSize;
}
~queue()
{
delete[] array;
}
void push(int x)
{
if(front==rear%size+1)
{
cout<<"上溢"<<endl;
return;
}
if(front==0)
front=1;
rear=rear%size+1;
array[rear]=x;
}
void pop()
{
if(front==0)
{
cout<<"下溢"<<endl;
return;
}
cout<<"弹出:"<<array[front]<<endl;
if(front==rear)
{
front=rear=0;
return;
}
front=front%size+1;
}
};
int main()
{
queue a(10);
for(int i=1;i<=11;i++)
a.push(i);
for(i=1;i<=11;i++)
a.pop();
return 0;
}
链队列
#include<iostream.h>
struct node
{
int date;
node *next;
};
class lq
{
node *front;
node *rear;
public:
lq()
{
node *s=new node;
s->next=NULL;
front=rear=s;
}
~lq()
{
node *s=NULL;
while(front->next!=NULL)
{
s=front;
front=front->next;
delete s;
}
}
void eq(int x)
{
node *s=new node;
s->date=x;
s->next=NULL;
rear->next=s;
rear=s;
}
void dq()
{
if(front==rear)
{
cout<<"下溢"<<endl;
return;
}
node *s=NULL;
node *d=new node;
d->next=NULL;
s=front->next;
if(front->next==rear)
front=rear=d;
else
front->next=s->next;
cout<<"弹出:"<<s->date<<endl;
delete s;
}
};
int main()
{
lq duilie;
for(int i=1;i<=10;i++)
duilie.eq(i);
for(i=1;i<=11;i++)
duilie.dq();
return 0;
}