数据结构——特殊线性表的实现(模板类)
一. 栈
1.栈的特点
元素具有后进先出的特性,在链栈中没有头结点
2.顺序栈
1.构造函数
template<typename T>
SeqStack<T>::SeqStack()
{
top=-1;//栈顶坐标设为-1
}
2.入栈操作
template<typename T>
void SeqStack<T>::Push(T x)
{
if(top==S-1) throw "上溢";//S为栈的最大长度
data[++top]=x;
}
3.出栈操作
template<typename T>
T SeqStack<T>::Pop(T x)
{
if(top==-1) throw "下溢";
return data[top--];
}
4.取栈顶元素
template<typename T>
T SeqStack<T>::GetTop()
{
if(top==-1) throw "栈空";
return data[top];
}
5.判空操作
template<typename T>
int SeqStack<T>::Empty()
{
if(top==-1) return 1;
return 0;
}
3.链栈
1.构造函数
template<typename T>
LinkStack<T>::LinkStack()
{
top=NULL;
}
2.入栈操作
template<typename T>
void LinkStack<T>::Push(T x)
{
Node<T> *s=NULL;
s=new Node<T>;
s->data=x;
s-next=top;
top=s;
}
3.出栈操作
template<typename T>
T LinkStack<T>::Pop()
{
Node<T> *p=NULL;
T x;
if(top==NULL) throw "下溢";
x=top->data;
p=top;
top=top->next;
delete p;
return x;
}
4.取栈顶元素
template<typename T>
T LinkStack<T>::GetTop()
{
if(top==NULL) throw "栈空";
return top->data;
}
5.判空操作
template<typename T>
int LinkStack<T>::Empty()
{
if(top->data==NULL) return 1;
return 0;
}
二.队列
1.队列的特点
元素具有先进先出的特性,在队列中有头结点
2.顺序队列
1.构造函数
template<typename T>
CirQueue<T>::CirQueue()
{
rear=S-1;
front=S-1;
}
2.入队操作
template<typename T>
void CirQueue<T>::EnQueue(T x)
{
if((rear+1)%S==front) throw "上溢";
rear=(rear+1)%S;
data[rear]=x;
}
3.出队操作
template<typename T>
T CirQueue<T>::DeQueue()
{
if(rear==front) throw "下溢";
front=(front+1)%S;
return data[front];
}
4.取队头元素
template<typename T>
T CirQueue<T>::GetHead()
{
if(rear==front) throw "下溢";
return data[(front+1)%S];
}
5.判空操作
template<typename T>
int CirQueue<T>::Empty()
{
if(rear==front) return 1;
return 0;
}
3.链队列
1.构造函数
template<typename T>
LinkQueue<T>::LinkQueue()
{
Node<T> *s=NULL;
s=new Node<T>;
s->next=NULL;
front=rear=s;
}
2.入队操作
template<typename T>
void LinkQueue<T>::EnQueue(T x)
{
Node<T> *s=NULL;
s=new Node<T>;
s->data=x;
s->next=NULL;
rear->next=s;
rear=s;
}
3.出队操作
template<typename T>
T LinkQueue<T>::DeQueue()
{
T x;
Node<T> *p=NULL;
if(rear==front) throw"下溢";
p=front->next;
x=p->next;
if(p->next==NULL) rear=front;
delete p;
return x;
}
4.取队头元素
template<typename T>
T LinkQueue<T>::GetHead()
{
if(front->next==NULL) throw"队列为空";
return front->next->data;
}
5.判空操作
template<typename T>
int LinkQueue<T>::Empty()
{
if(front==rear) return 1;
retuen 0;
}
三.扩展
两栈共享空间
1.入栈操作
template<typename T>
void BothStack<T>::Push(int i,T x)
{
if(top1==top2-1) throw "上溢";
if(i==1) data[++top1]=x;
else data[++top2]=x;
}
2.出栈操作
template<typename T>
T BothStack<T>::Pop(int i)
{
if(i==1){
if(top1==-1) throw"下溢";
return data[top1--];
}
else{
if(top2==S) throw"下溢";
return data[top2++];
}
}
循环队列
#include<iostream>
using namespace std;
const int S=6;
template <typename T>
class CirQueue{
public:
CirQueue();
~CirQueue();
void EnQueue(T x);
T DeQueue();
T GetHead();
int Empty();
private:
T data[S];
int front,rear;
};
template <typename T>
CirQueue<T>::CirQueue(){
rear=front=S-1;
}
template <typename T>
CirQueue<T>::~CirQueue(){}
template <typename T>
void CirQueue<T>::EnQueue(T x){
if((rear+1)%S==front) throw "overflow";
rear=(rear+1)%S;
data[rear]=x;
}
template <typename T>
T CirQueue<T>::DeQueue(){
if(rear==front) throw "overflow";
front=(front+1)%S;
return data[front];
}
template <typename T>
T CirQueue<T>::GetHead(){
if(rear==front) throw "overflow";
return data[(front+1)%S];
}
template <typename T>
int CirQueue<T>::Empty(){
if(rear==front) return 1;
else return 0;
}
int main(){
CirQueue<int> a;
cout<<a.Empty()<<endl;
a.EnQueue(1);
cout<<a.GetHead()<<endl;
a.EnQueue(2);
cout<<a.DeQueue()<<endl;
cout<<a.GetHead()<<endl;
a.EnQueue(3);
cout<<a.DeQueue()<<endl;
cout<<a.GetHead()<<endl;
a.EnQueue(4);
cout<<a.DeQueue()<<endl;
cout<<a.GetHead()<<endl;
a.EnQueue(5);
cout<<a.DeQueue()<<endl;
cout<<a.Empty()<<endl;
}
链栈
#include<iostream>
using namespace std;
template <typename T>
class Node{
public:
T data;
Node<T> *next;
};
template <typename T>
class LinkStack{
public:
LinkStack();
~LinkStack();
void Push(T x);
T Pop();
T GetTop();
int Empty();
private:
Node<T> *top;
};
template <typename T>
LinkStack<T>::LinkStack(){
top=nullptr;
}
template <typename T>
LinkStack<T>::~LinkStack(){
Node<T> *p=nullptr;
while(top){
p=new Node<T>;
p=top;
top=top->next;
delete p;
}
}
template <typename T>
void LinkStack<T>::Push(T x){
Node<T> *s=nullptr;
s=new Node<T>;
s->data=x;
s->next=top;
top=s;
}
template <typename T>
T LinkStack<T>::Pop(){
Node<T> *p=top;
if(top) {
top=top->next;
delete p;
return top->data;
}
else throw "overflow";
}
template <typename T>
T LinkStack<T>::GetTop(){
if(top) return top->data;
else throw "overfolw";
}
template <typename T>
int LinkStack<T>::Empty(){
if(top) return 0;
else return 1;
}
int main(){
LinkStack<int> a;
cout<<a.Empty()<<endl;
a.Push(1);
a.Push(2);
a.Push(3);
cout<<a.Empty()<<endl;
cout<<a.GetTop()<<endl;
cout<<a.Pop()<<endl;
cout<<a.GetTop()<<endl;
}
顺序栈
#include<iostream>
using namespace std;
const int S=100;
template <typename T>
class SeqStack{
public:
SeqStack();
~SeqStack();
void Push(T x);
T Pop();
T GetTop();
int Empty();
private:
T data[S];
int top;
};
template <typename T>
SeqStack<T>::SeqStack(){
top=-1;
}
template <typename T>
SeqStack<T>::~SeqStack(){}
template <typename T>
void SeqStack<T>::Push(T x){
if(top==S-1) throw "overflow";
else data[++top]=x;
}
template <typename T>
T SeqStack<T>::Pop(){
if(top==-1) throw "overflow";
else return data[top--];
}
template <typename T>
T SeqStack<T>::GetTop(){
if(top==-1) throw "overflow";
else return data[top];
}
template <typename T>
int SeqStack<T>::Empty(){
if(top==-1) return 1;
else return 0;
}
int main(){
SeqStack<int> a;
cout<<a.Empty()<<endl;
a.Push(1);
cout<<a.GetTop()<<endl;
a.Push(2);
cout<<a.GetTop()<<endl;
a.Push(3);
cout<<a.GetTop()<<endl;
a.Push(4);
cout<<a.GetTop()<<endl;
a.Push(5);
cout<<a.GetTop()<<endl;
while(!a.Empty()){
cout<<a.Pop()<<endl;
cout<<a.GetTop()<<endl;
}
}
链队列
#include<iostream>
using namespace std;
const int S=6;
template <typename T>
class Node{
public:
T data;
Node<T> *next;
};
template <typename T>
class LinkQueue{
public:
LinkQueue();
~LinkQueue();
void EnQueue(T x);
T DeQueue();
T GetHead();
int Empty();
private:
Node<T> *front,*rear;
};
template <typename T>
LinkQueue<T>::LinkQueue(){
Node<T> *s=nullptr;
s=new Node<T>;
s->next=nullptr;
front=rear=s;
}
template <typename T>
LinkQueue<T>::~LinkQueue(){
Node<T> *p=nullptr;
while(front!=rear){
p=new Node<T>;
p=front->next;
front->next=p->next;
delete p;
}
}
template <typename T>
void LinkQueue<T>::EnQueue(T x){
Node<T> *s=nullptr;
s=new Node<T>;
s->data=x;
s->next=nullptr;
rear->next=s;
rear=s;
}
template <typename T>
T LinkQueue<T>::DeQueue(){
if(rear==front) throw "overflow";
Node<T> *p=front->next;
T x=p->data;
front->next=p->next;
if(p->next==nullptr) rear=front;
delete p;
return x;
}
template <typename T>
T LinkQueue<T>::GetHead(){
if(front==rear) throw "overflow";
return front->next->data;
}
template <typename T>
int LinkQueue<T>::Empty(){
if(front==rear) return 1;
else return 0;
}
int main(){
LinkQueue<int> a;
cout<<a.Empty()<<endl;
a.EnQueue(1);
cout<<a.Empty()<<endl;
a.EnQueue(2);
cout<<a.GetHead()<<endl;
cout<<a.DeQueue()<<endl;
cout<<a.GetHead()<<endl;
}