数据结构——特殊的线性表(栈和队列)

数据结构——特殊线性表的实现(模板类)

一. 栈

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;

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值