1.顺序栈
源代码:
#ifndef SeqStack_H
#define SeqStack_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;
};
#endif
#include"SeqStack.h"
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];
}
template<class DataType>
int SeqStack<DataType>::Empty()
{
if(top==-1) return 1;
else return 0;
}
#include<iostream>
using namespace std;
#include"SeqStack.cpp"
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.链栈
源代码:
#ifndef SeqStack_H
#define SeqStack_H
const int StackSize=10;
template<class DataType>
struct Node
{
DataType data;
Node<DataType> * next;
};
template<class DataType>
class LinkStack
{
public:
LinkStack();
~LinkStack();
void Push(DataType x);//入栈
DataType Pop();//出栈
int Empty();/*判断栈是否为空*/
private:
Node<DataType> * top;
};
#endif
#include"LinkStack.h"
template<class DataType>
LinkStack<DataType>::LinkStack()
{
top=NULL;
}
template<class DataType>
LinkStack<DataType>::~LinkStack()
{
Node<DataType> * q=NULL;
while(top!=NULL)
{
q=top->next;
delete q;
}
}
template<class DataType>
void LinkStack<DataType>::Push(DataType x)/*入栈*/
{
Node<DataType> * s=NULL;
s=new Node<DataType>;
s->data=x;
s->next=top;
top=s;
}
template<class DataType>
DataType LinkStack<DataType>::Pop()/*弹栈*/
{
DataType x;
Node<DataType> * p=NULL;
if(top=NULL) throw"下溢";
x=top->data;
p=top;
top=top->next;
delete p;
return x;
}
template<class DataType>
int LinkStack<DataType>::Empty()
{
if(top=NULL) return 1;
else return 0;
}
#include<iostream.h>
#include"LinkStack.cpp"
void main()
{
LinkStack<int>t;
if(t.Empty())
cout<<"栈为空"<<endl;
else
cout<<"栈非空"<<endl;
cout<<"对15和10执行入栈操作"<<endl;
t.Push(15);
t.Push(10);
cout<<"执行一次出栈操作"<<endl;
t.Pop();
}
运行结果:
3.顺序队列
源代码:
#ifndef CirQueue_H
#define CirQueue_H
const int QueueSize=100;
template<class DataType>
class CirQueue
{
public:
CirQueue();
~CirQueue(){}
void EnQueue(DataType x);//入队
DataType DeQueue();//出队
int Empty();
private:
DataType data[QueueSize];
int front,rear;
};
#endif;
#include"CirQueue.h"
template<class DataType>
CirQueue<DataType>::CirQueue()
{
front=rear=QueueSize-1;
}
template<class DataType>
void CirQueue<DataType>::EnQueue(DataType x)
{
if((rear+1)%QueueSize==front) throw"上溢";
rear=(rear+1)%QueueSize;
data[rear]=x;
}
template<class DataType>
DataType CirQueue<DataType>::DeQueue()
{
if(rear==front) throw"下溢";
front=(front+1)%QueueSize;
return data[front];
}
template<class DataType>
int CirQueue<DataType>::Empty()
{
if(front==rear)
return 1;
else
return 0;
}
#include<iostream>
using namespace std;
#include"CirQueue.cpp"
void main()
{
CirQueue<int>S;
if(S.Empty())
cout<<"队列为空"<<endl;
else
cout<<"队列非空"<<endl;
cout<<"元素10和15执行入队操作:"<<endl;
S.EnQueue(10);
S.EnQueue(15);
cout<<"执行出队操作:"<<endl;
S.DeQueue();
}
运行结果:
4.链队列
源代码:
#ifndef LinkQueue_H
#define LinkQueue_H
template<class DataType>
struct Node
{
DataType data;
Node<DataType> * next;
};
template<class DataType>
class LinkQueue
{
public:
LinkQueue();
~LinkQueue();
void EnQueue(DataType x);//入队
DataType DeQueue();//出队
int Empty();
private:
Node<DataType> * front,*rear;
};
#endif
#include"LinkQueue.h"
template<class DataType>
LinkQueue<DataType>::LinkQueue()
{
Node<DataType> * s=NULL;
s=new Node<DataType>;
s->next=NULL;
front=rear=s;
}
template<class DataType>
LinkQueue<DataType>::~LinkQueue()
{
Node<DataType> * p=NULL;
while(front!=NULL)
{
p=front->next;
delete front;
front=p;
}
}
template<class DataType>
void LinkQueue<DataType>::EnQueue(DataType x)
{
Node<DataType> * s=NULL;
s=new Node<DataType>;
s->data=x;
s->next=NULL;
rear->next=s;
rear=s;
}
template<class DataType>
DataType LinkQueue<DataType>::DeQueue()
{
Node<DataType> * p=NULL;
int x;
if(rear=front) throw"下溢";
p=front->next;
x=p->data;
front->next=p->next;
if(p->next==NULL) rear=front;//判断出队前队列是否为1
delete p;
return x;
}
template<class DataType>
int LinkQueue<DataType>::Empty()
{
if(front=rear)
return 1;
else
return 0;
}
#include<iostream.h>
#include"LinkQueue.cpp"
void main()
{
LinkQueue<int> q;
if(q.Empty())
cout<<"队列为空"<<endl;
else
cout<<"队列非空"<<endl;
cout<<"对15和10执行入栈操作:"<<endl;
try
{
q.EnQueue(15);
q.EnQueue(10);
}
catch(char* wrong)
{
cout<<wrong<<endl;
}
cout<<"执行出队操作:"<<endl;
try
{
q.DeQueue();
}
catch(char* wrong)
{
cout<<wrong<<endl;
}
}
运行结果:
5.用顺序栈将十进制转换为二进制
源代码:
#ifndef ChaStack_H
#define ChaStack_H
const int StackSize=10;
template<class DataType>
class ChaStack
{
public:
ChaStack();
~ChaStack(){}
void Push(DataType x);
DataType Pop();
void Decimaltor(int num);//转换
int Empty();
private:
DataType data[StackSize];
int top;
};
#endif;
#include"ChaStack.h"
template<class DataType>
ChaStack<DataType>::ChaStack()
{
top=-1;
}
template<class DataType>
void ChaStack<DataType>::Push(DataType x)
{
if(top==StackSize-1)throw"上溢";
top++;
data[top]=x;
}
template<class DataType>
DataType ChaStack<DataType>::Pop()
{
DataType x;
if(top==-1)throw"下溢";
x=data[top--];
return x;
}
template<class DataType>
void ChaStack<DataType>::Decimaltor(int num)
{
DataType x;
while(num!=0)
{
x=num%2;
data[++top]=x;
num=num/2;
}
while(top!=-1)
cout<<(data[top--])<<endl;
}
template<class DataType>
int ChaStack<DataType>::Empty()
{
if(top=-1)
return 1;
else
return 0;
}
#include<iostream.h>
#include"ChaStack.cpp"
void main()
{
ChaStack<int>C;
if(C.Empty())
cout<<"栈为空"<<endl;
else
cout<<"栈非空"<<endl;
cout<<"对302执行入栈操作"<<endl;
C.Push(302);
cout<<"其二进制为:"<<endl;
C.Decimaltor(302);
cout<<"执行出栈操作"<<endl;
C.Pop();
}
运行结果: