数据结构的顺序栈、链式栈、链队列、循环队列的实现

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#define MAXQSIZE 5
# define OVERFLOW 0
#define OK 1
# define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int SElemType;
typedef int Status;
using namespace std;
 typedef struct {
SElemType *base;
SElemType *top;
int stacksize;
 }SqStack;
 //构造空栈
 Status InitStack(SqStack &S){
S.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S.base) exit(OVERFLOW);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
 }
 //栈的初始化
int input(SqStack &S,int n){
printf("请输入数据:\n");
for(int i=0;i<n;i++)
scanf("%d",S.top++);
return OK;
}
//栈的遍历
Status Otput(SqStack S,int n){
for(int i=0;i<n;i++)
printf("%d\t",*--S.top);
return 1;
}
//入栈操作
Status Push(SqStack &S,SElemType e){
SElemType *top,*base;
if(S.top-S.base>=S.stacksize){
S.base=(SElemType*)realloc(S.base,(STACKINCREMENT+S.stacksize)*sizeof(SElemType));
    if(!S.base) exit(OVERFLOW);
    S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return OK;
}
//出栈
Status Pop(SqStack &S,SElemType &e){
SElemType *top,*base;
if(S.top==S.base) return 0;
e=*(--S.top);
return 1;
}


//链式栈
typedef int LElemType;
typedef struct Lnode{
LElemType data;
struct Lnode *next;
}Lnode,*LinkStack;
//创建链表
Status CreateStack(LinkStack &S,int n){
LinkStack top;
LElemType data;
S=NULL;
printf("\n请输入数据:\n");
for(int i=1;i<=n;i++){
top=(LinkStack)malloc(sizeof(Lnode));
scanf("%d",&(top->data));
top->next=S;
S=top;
}
return OK;
}
//打印创建的链栈
Status disp(LinkStack S){
while(S){
printf("%d\t",S->data);
S=S->next;
//Sleep(1000);
}
cout<<endl;
return OK;
}
//链入栈
Status LinkStackPush(LinkStack &S,LElemType e){
LinkStack top;
   top=(LinkStack)malloc(sizeof(Lnode));
top->data=e;
top->next=S;
S=top;
}
//链出栈
Status LinkStackPop(LinkStack &S,LElemType &e){
    LinkStack top;
    top=S;
    while(S)
    {
     e=top->data;
     printf("%d\t",e);
     S=top->next;
     free(top);
     top=S;
     //Sleep(1000);
    }
    return OK;
}
//链队列
typedef int QElemType;
typedef struct QNode{
    QElemType data;
    struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
    QueuePtr front;
    QueuePtr rear;
}LinkQueue;
//创建链队列
Status InitQueue(LinkQueue &Q,int n){
   QueuePtr q;
    Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
    if(!Q.front)exit(OVERFLOW);
    Q.front->next=NULL;
    cout<<"\n请输入数据:"<<endl;
    for(int i=1;i<=n;i++){
    q=(QueuePtr)malloc(sizeof(QNode));
    scanf("%d",&(q->data));
    q->next=NULL;
     Q.rear->next=q;
     Q.rear=q;
    }
    return OK;
}
//打印链队
Status out(LinkQueue Q){
QueuePtr p;
p=Q.front;
 do{
    p=p->next;
    printf("%d\t",p->data);
 }while(p!=Q.rear);
 return OK;
}
//插入链队
Status QueuePush(LinkQueue &Q,QElemType e){
    QueuePtr p;
    p=(QueuePtr)malloc(sizeof(QNode));
    p->data=e;
    p->next=NULL;
    Q.rear->next=p;
    Q.rear=p;
    return OK;
}
Status QueuePop(LinkQueue &Q,QElemType &e){
  QueuePtr p;
  p=Q.front->next;
  e=p->data;
  Q.front->next=p->next;
  free(p);
  return OK;
}
//循环队列
typedef int QElemType;
typedef struct{
    QElemType *base;
    int front,rear;
}SqQueue;
//构建空循环队列
Status InitQueue(SqQueue &Q,int n){
    Q.base=(QElemType*)malloc(MAXQSIZE*sizeof(SqQueue));
    if(!Q.base)exit(OVERFLOW);
    Q.front=Q.rear=0;
     cout<<"\n请输入数据"<<endl;
   while(Q.rear<n){
        scanf("%d", &(Q.base[Q.rear]));
        Q.rear=(Q.rear+1)%MAXQSIZE;
    }
    return OK;
}
//输出链表
Status shuchu(SqQueue Q){
    while(Q.rear!=Q.front){
        printf("%d\t",Q.base[Q.front++]);
    }
    return OK;
}
//插入队列
Status EnQueue(SqQueue &Q,QElemType e){
    if((Q.rear+1)%MAXQSIZE==Q.front) exit(OVERFLOW);
    Q.base[Q.rear]=e;
    Q.rear=(Q.rear+1)%MAXQSIZE;
    return OK;
}
//删除对头
Status DeQueue(SqQueue &Q,QElemType &e){
    if(Q.front==Q.rear)exit(OVERFLOW);
    e=Q.base[Q.front];
    Q.front=(Q.front+1)%MAXQSIZE;
    return OK;
}
int main(){
//顺序栈
    SqStack S;
    int e;
InitStack(S);
input(S,5);
printf("打印创建的顺序栈:\n");
Otput(S,5);
Push(S,2);
printf("\n打印插入后的顺序栈:\n");
Otput(S,6);
Pop(S,e);
printf("\n打印取取后的顺序栈:\n");
Otput(S,5);
//链式栈
LinkStack A;
LElemType E;
CreateStack(A,5);
cout<<"打印创建的链式栈"<<endl;
disp(A);
LinkStackPush(A,5);
cout<<"打印插入的链式栈"<<endl;
 disp(A);
 cout<<"链式栈出栈:"<<endl;
 LinkStackPop(A,E);
 //链队列
 LinkQueue Q;QElemType b;
 InitQueue(Q,5);
 cout<<"打印链队列:"<<endl;
 out(Q);
 QueuePush(Q,2);
 cout<<endl<<"打印插入后链队列:"<<endl;
 out(Q);
 QueuePop(Q,b);
 cout<<endl<<"打印出栈列对列:"<<endl;
 out(Q);
 //循环队列
 SqQueue D;
 QElemType c;
    InitQueue(D,3);
    cout<<"输出循环队:"<<endl;
    shuchu(D);
    cout<<endl<<"插入后循环队列:"<<endl;
    EnQueue(D,5);
    shuchu(D);
    cout<<endl<<"删除后入循环队列:"<<endl;
    DeQueue(D,c);
    shuchu(D);
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值