栈与队列的基本操作

关于栈与队列两种存储结构方式的基本操作队列的顺序结构采用了循环队列的方式。。。
#include<iostream>
using namespace std;
#include<cstdio>
typedef int Elem;
#define MaxStack 100
#define QueueSize 20
#define OVERFLOW -2
#define ERROR -1
#define SUCCESS 0
/******************************
********类型构造结构***********
******************************/
typedef struct Lnode{       //构造链表结点
	Elem data;
	struct Lnode *next;
}LNode,*List;

typedef struct stack{  //顺序结构栈
	Elem *base,*top;
	int size;
}Stack;

typedef struct {        //链式栈
	List top;
}LStack;

typedef struct{         //顺序队列
	Elem *base;
	int front;
	int rear;
} Queue;

typedef struct {        //链式队列
	List front ,rear;
}LQueue;

/******************************
********顺序栈方法************
******************************/
bool InitStack(Stack &s){
	s.base=new Elem(MaxStack);
	if(s.base==0)	return (OVERFLOW);
	s.top=s.base;
	s.size=MaxStack;
	return SUCCESS;
}
bool DestroyStack(Stack &s){
	if(s.base==NULL) return ERROR;
	delete []s.base;
	s.top=NULL;
	s.size=0;
	return SUCCESS;
}
bool GetTop(Stack s,Elem &e){
	if(s.top==s.base) return ERROR;
	e=*(s.top-1);
	return SUCCESS;
}

bool EmptyStack(Stack &s){
	s.top=s.base;
	s.size=MaxStack;
	return SUCCESS;
}
bool Push(Stack &s,Elem e){
	if(s.top-s.base>=s.size)	return OVERFLOW;
	*(s.top++)=e;
	return SUCCESS;
}
bool Pop(Stack &s,Elem &e){
	if(s.top==s.base)	return ERROR;
	e=*(--s.top);
	return SUCCESS;
}
bool trverseS(Stack &s){
    Elem *p=s.base;
    while(p!=s.top){
        cout<<" "<<*p;
        p++;
    }
    cout<<endl;
}

/******************************
********链式栈方法************
******************************/

bool InitLStack(LStack &s){
    List p;
	s.top=new Lnode;                //存疑
	if(s.top==NULL)	return ERROR;
	p=s.top;
	s.top=NULL;
	delete p;
	return SUCCESS;
}
bool LPush(LStack &s,Elem e){
	List pnew=new Lnode;
	if(pnew==NULL)	return ERROR;
	pnew->data=e;
	pnew->next=s.top;
	s.top=pnew;
	return SUCCESS;
}
bool LPop(LStack &s,Elem &e){
	if(s.top==NULL)	return ERROR;
	e=s.top->data;
	List pold=s.top;
	s.top=s.top->next;
	delete pold;
	return SUCCESS;
}
void LtrverseS(LStack &s){
    List p=s.top;
    while(p!=NULL){
        cout<<" "<<p->data;
        p=p->next;
    }
    cout<<endl;
}

/******************************
********顺序队列方法***********
******************************/
bool InitQueue(Queue &q){
    q.base=new Elem(QueueSize);
    if(q.base==NULL)    return OVERFLOW;
    q.front=q.rear=0;
    return SUCCESS;
}
bool EnQueue(Queue &q,Elem e){      //从队尾rear入
    if((q.rear+1)%QueueSize==q.front)   return OVERFLOW;
    q.base[q.rear]=e;
    q.rear=(q.rear+1)%QueueSize;
    return SUCCESS;
}

bool DeQueue(Queue &q,Elem &e){      //从队头front出
    if(q.rear==q.front)     return ERROR;
    e=*(q.base+q.front);
    q.front=(q.front+1)%QueueSize;
    return SUCCESS;
}

void trverseQ(Queue &q){        //从对头到队尾
    int k=q.front;
    while(k!=q.rear){
        cout<<" "<<*(q.base+k);
        k=(k+1)%QueueSize;
    }
    cout<<endl;
    return ;
}
/******************************
********链式队列方法***********
******************************/
bool InitLQueue(LQueue &q){
    q.front=new Lnode;
    if(q.front==NULL) return ERROR;
    q.rear=q.front;
    q.front->next=NULL;
    return SUCCESS;
}

bool EnLQueue(LQueue &q,Elem e){
    List pnew=new Lnode;
    pnew->next=NULL;
    pnew->data=e;
    q.rear->next=pnew;
    q.rear=pnew;
    return SUCCESS;
}

bool DeLQueue(LQueue &q,Elem &e){
    List pold;
    if(q.front==q.rear) return ERROR;
    e=q.front->next->data;
    pold=q.rear;
    q.front=q.front->next;
    delete pold;
    return SUCCESS;
}

void LtrverseQ(LQueue &q){
    List p=q.front;
    p=p->next;
    while(p!=NULL){
        cout<<" "<<p->data;
        p=p->next;
    }
    cout<<endl;
    return ;
}

/******************************
********测试栈与队列方法******
******************************/

bool stacktest(){
    Elem e;
    Stack s;
    if(InitStack(s)!=SUCCESS)   return ERROR;
    cout<<"开始顺序栈的测试,包括出栈和入栈,遍历输出:"<<endl;
    cout<<"进栈若干个元素,以0结尾"<<endl;
    while(1){
        cin>>e;
        if(e==0)    break;
        if(Push(s,e)!=SUCCESS)  return ERROR;
    }
    cout<<"从栈底向上遍历栈:";
    trverseS(s);

    cout<<"栈的进栈和出栈测试:"<<endl;
    cout<<"PUSH:"<<endl;
    cin>>e;
    if(Push(s,e)!=SUCCESS)  return ERROR;
    cout<<"从栈底向上遍历栈:";
    trverseS(s);
    cout<<endl;

    cout<<"PUSH:"<<endl;
    cin>>e;
    if(Push(s,e)!=SUCCESS)  return ERROR;
    cout<<"从栈底向上遍历栈:";
    trverseS(s);
    cout<<endl;

    cout<<"POP:"<<endl;
    if(Pop(s,e)!=SUCCESS)  return ERROR;
    cout<<"出栈元素为"<<e<<"从栈底向上遍历栈:";
    trverseS(s);
    cout<<endl;

    cout<<"POP:"<<endl;
    if(Pop(s,e)!=SUCCESS)  return ERROR;
    cout<<"出栈元素为"<<e<<"  从栈底向上遍历栈:";
    trverseS(s);
    cout<<endl;

    return SUCCESS;
}

bool lstacktest(){
    Elem e;
    LStack s;
    if(InitLStack(s)!=SUCCESS)   return ERROR;
    cout<<"开始链式栈的测试,包括出栈和入栈,遍历输出:"<<endl;
    cout<<"进栈若干个元素,以0结尾"<<endl;
    while(1){
        cin>>e;
        if(e==0)    break;
        if(LPush(s,e)!=SUCCESS)  return ERROR;
    }
    cout<<endl<<"从栈顶向下遍历栈:";
    LtrverseS(s);

    cout<<"栈的进栈和出栈测试:"<<endl;
    cout<<"PUSH:"<<endl;
    cin>>e;
    if(LPush(s,e)!=SUCCESS)  return ERROR;
    cout<<"从栈顶向下遍历栈:";
    LtrverseS(s);
    cout<<endl;

    cout<<"PUSH:"<<endl;
    cin>>e;
    if(LPush(s,e)!=SUCCESS)  return ERROR;
    cout<<"从栈顶向下遍历栈:";
    LtrverseS(s);
    cout<<endl;

    cout<<"POP:"<<endl;
    if(LPop(s,e)!=SUCCESS)  return ERROR;
    cout<<"出栈元素为"<<e<<"  从栈顶向下遍历栈:";
    LtrverseS(s);
    cout<<endl;

    cout<<"POP:"<<endl;
    if(LPop(s,e)!=SUCCESS)  return ERROR;
    cout<<"出栈元素为"<<e<<"  从栈顶向下遍历栈:";
    LtrverseS(s);
    cout<<endl;

    return SUCCESS;
}

bool queuetest(){
    Elem e;
    Queue q;
    if(InitQueue(q)!=SUCCESS)   return ERROR;
    cout<<"开始顺序队列的测试,包括入队和出队,遍历输出:"<<endl;
    cout<<"入队若干个元素,以0结尾"<<endl;
    while(1){
        cin>>e;
        if(e==0)    break;
        if(EnQueue(q,e)!=SUCCESS)  return ERROR;
    }
    cout<<"从队头遍历队列 :";
    trverseQ(q);

    cout<<"栈的进栈和出栈测试:"<<endl;
    cout<<"ENQUEUE:"<<endl;
    cin>>e;
    if(EnQueue(q,e)!=SUCCESS)  return ERROR;
    cout<<"从队头遍历队列 :";
    trverseQ(q);
    cout<<endl;

    cout<<"ENQUEUE:"<<endl;
    cin>>e;
    if(EnQueue(q,e)!=SUCCESS)  return ERROR;
    cout<<"从队头遍历队列 :";
    trverseQ(q);
    cout<<endl;

    cout<<"DEQUEUE:"<<endl;
    if(DeQueue(q,e)!=SUCCESS)  return ERROR;
    cout<<"出队元素为"<<e<<"从队头遍历队列 :";
    trverseQ(q);
    cout<<endl;

    cout<<"DEQUEUE:"<<endl;
    if(DeQueue(q,e)!=SUCCESS)  return ERROR;
    cout<<"出队元素为"<<e<<"从队头遍历队列 :";
    trverseQ(q);
    cout<<endl;

    return SUCCESS;
}

bool lqueuetest(){
    Elem e;
    LQueue q;
    if(InitLQueue(q)!=SUCCESS)   return ERROR;
    cout<<"开始链式队列的测试,包括入队和出队,遍历输出:"<<endl;
    cout<<"入队若干个元素,以0结尾"<<endl;
    while(1){
        cin>>e;
        if(e==0)    break;
        if(EnLQueue(q,e)!=SUCCESS)  return ERROR;
    }
    cout<<"从队头遍历队列 :";
    LtrverseQ(q);

    cout<<"栈的进栈和出栈测试:"<<endl;
    cout<<"ENQUEUE:"<<endl;
    cin>>e;
    if(EnLQueue(q,e)!=SUCCESS)  return ERROR;
    cout<<"从队头遍历队列 :";
    LtrverseQ(q);
    cout<<endl;

    cout<<"ENQUEUE:"<<endl;
    cin>>e;
    if(EnLQueue(q,e)!=SUCCESS)  return ERROR;
    cout<<"从队头遍历队列 :";
    LtrverseQ(q);
    cout<<endl;

    cout<<"DEQUEUE:"<<endl;
    if(DeLQueue(q,e)!=SUCCESS)  return ERROR;
    cout<<"出队元素为"<<e<<"从队头遍历队列 :";
    LtrverseQ(q);
    cout<<endl;

    cout<<"DEQUEUE:"<<endl;
    if(DeLQueue(q,e)!=SUCCESS)  return ERROR;
    cout<<"出队元素为"<<e<<"从队头遍历队列 :";
    LtrverseQ(q);
    cout<<endl;

    return SUCCESS;
}
//bool lstacktest(){;}
//bool queuetest(){;}
//bool lqueuetest(){;}
void TEST(){
    int tsnum;
    cout<<"本次实验进行栈与队列的有关操作:"<<endl;
    cout<<"输入1测试顺序栈的操作"<<endl;
    cout<<"输入2测试链式栈的操作"<<endl;
    cout<<"输入3测试顺序队列的操作"<<endl;
    cout<<"输入4测试链式队列的操作"<<endl;
    cout<<"输入其他数字跳出测试循环"<<endl;
    while(1){
        cout<<"请输入:";
        cin>>tsnum;
        cout<<endl;
        switch(tsnum){
            case 1:if(stacktest()!=SUCCESS) return ;break;
            case 2:if(lstacktest()!=SUCCESS) return ;break;
            case 3:if(queuetest()!=SUCCESS) return ;break;
            case 4:if(lqueuetest()!=SUCCESS) return ;break;
            default :return ;break;
        }
        cout<<"继续测试";
    }
    return ;
}

int main(){
    TEST();
    return 0;
}



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值