• 数据结构实验报告(二)


    实验二 栈、队列

    (1)实验内容
    1.采用链式存储实现栈的初始化、入栈、出栈操作。
    2.采用顺序存储实现栈的初始化、入栈、出栈操作。
    3.采用链式存储实现队列的初始化、入队、出队操作。
    4.采用顺序存储实现循环队列的初始化、入队、出队操作。
    5.在主函数中设计一个简单的菜单,分别测试上述算法。
    *6.综合训练:1)利用栈实现表达式求值算法。
    2)利用栈实现迷宫求解。

      1 #include <iostream>
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 #define MAXSIZE 100
      5 #define OVERFLOW -2
      6 using namespace std;
      7 //顺序栈
      8 typedef int Elemtype;
      9 typedef struct {
     10 Elemtype elem[MAXSIZE];
     11 int top;
     12 }SeqStack;
     13 void initSeqStack(SeqStack &s){
     14 s.top=-1;
     15 }
     16 int stackEmpty(SeqStack &s){
     17 if (s.top==-1)
     18 return 1;
     19 return 0;//或者返回 表达式的真值return s.top==-1;
     20 }
     21 int push(SeqStack &s,Elemtype e){
     22 if (s.top>=MAXSIZE-1)
     23 return 0;
     24 else {
     25 s.top++;
     26 s.elem[s.top]=e;
     27 return 1;
     28 }
     29 }
     30 int pop(SeqStack &s,Elemtype &e){
     31 if (s.top==-1)
     32 return 0;
     33 else {
     34 e=s.elem[s.top];
     35 s.top--;
     36 return 1;
     37 }
     38 }
     39 int gettop(SeqStack &s,Elemtype &e){
     40 if (s.top==-1)
     41 return 0;
     42 else {
     43 e=s.elem[s.top];
     44 return 1;
     45 }
     46 }
     47 void display(SeqStack &s){
     48 for (int i=0;i<=s.top;i++){
     49 cout<<s.elem[i]<<" ";
     50 }
     51 cout<<endl;
     52 }
     53 
     54 //链栈
     55 typedef struct node {
     56 Elemtype data;
     57 struct node *next;
     58 }node,*linkstack;
     59 void initLinkstack(linkstack &top){
     60 top=NULL;//无头节点的链栈
     61 }
     62 int linkstackEmpty(linkstack &top){
     63 return top==NULL;
     64 }
     65 int linkstackPush(linkstack &top,Elemtype e){
     66 linkstack p=(linkstack )malloc (sizeof(node));
     67 if (!p) exit(OVERFLOW);
     68 p->data=e;
     69 p->next=top;
     70 top=p;
     71 return 1;
     72 }
     73 int linkstackPop(linkstack &top,Elemtype &e){
     74 e=top->data;
     75 linkstack p=top;
     76 top=top->next;
     77 free(p);
     78 return 1;
     79 }
     80 void getTop(linkstack &top,Elemtype &E){
     81 E=top->data;
     82 }
     83 void displaylinkstack(linkstack &top){
     84 linkstack p=top;
     85 while (p){
     86 printf ("%d ",p->data);
     87 p=p->next;
     88 }
     89 printf ("
    ");
     90 }
     91 //顺序循环队列
     92 typedef int Elemtype;
     93 typedef struct{
     94 Elemtype data[MAXSIZE];
     95 int rear,front;
     96 }Seqqueue;
     97 void initSeqqueue(Seqqueue &q){
     98 q.rear=q.front=-1;
     99 }
    100 int emptySeqqueue(Seqqueue &q){
    101 return q.rear==q.front;
    102 }
    103 int enSeqqueue(Seqqueue &q,Elemtype e){
    104     //先判断是否队满
    105     if ((q.rear+1)%MAXSIZE==q.front){
    106     printf ("full!
    ");
    107     return 0;
    108     }
    109     q.rear=(q.rear+1)%MAXSIZE;
    110     q.data[q.rear]=e;
    111     return 1;
    112 }
    113 int deSeqqueue(Seqqueue &q,Elemtype &e){
    114 if (emptySeqqueue(q)){
    115 printf ("null!
    ");
    116 return 0;
    117 }
    118 q.front=(q.front+1)%MAXSIZE;
    119 e=q.data[q.front];
    120 return 1;
    121 }
    122 Elemtype getFront(Seqqueue &q){
    123 if (emptySeqqueue(q)){
    124 printf ("null!
    ");
    125 }
    126 else {
    127 Elemtype e;
    128 q.front=(q.front+1)%MAXSIZE;
    129 e=q.data[q.front];
    130 return e;
    131 }
    132 }
    133 int length(Seqqueue &q){
    134 return (q.rear-q.front+MAXSIZE)%MAXSIZE;
    135 }
    136 void display(Seqqueue &q){
    137 if (emptySeqqueue(q)){
    138 printf ("null!
    ");
    139 }
    140 else {
    141 int i=(1+q.front)%MAXSIZE;
    142 for (int j=0;j<length(q);j++){
    143 printf ("%d ",q.data[i]);
    144 i++;
    145 }
    146 printf ("
    ");
    147 }
    148 }
    149 
    150 
    151 //链队列
    152 typedef int Elemtype;
    153 typedef struct qnode {
    154 Elemtype data;
    155 struct qnode *next;
    156 }qnode,*Queueptr;
    157 typedef struct {
    158 Queueptr front ;
    159 Queueptr rear;
    160 }linkQueue;
    161 int  initQueue(linkQueue &q){
    162 Queueptr lq=(Queueptr)malloc(sizeof(qnode));
    163 if (!lq) exit(OVERFLOW);
    164 lq->next=NULL;
    165 q.front=q.rear=lq;
    166 }
    167 int isempty(linkQueue q){
    168 return q.front==q.rear;
    169 }
    170 int enterQueue(linkQueue &q,Elemtype e){
    171 Queueptr p=(Queueptr)malloc(sizeof(qnode));
    172 if (!p) exit(OVERFLOW);
    173 p->data=e;
    174 p->next=q.rear->next;
    175 q.rear->next=p;
    176 q.rear=p;
    177 return 1;
    178 }
    179 int deQueue(linkQueue &q,Elemtype &e){
    180     //出队依旧要判空,入队不需要判满了
    181     if (q.rear==q.front){
    182     printf("null
    ");
    183     return 0;
    184     }
    185 Queueptr p=q.front->next;
    186 e=p->data;
    187 q.front->next=p->next;
    188 //这里要特别注意如果链表中唯一的元素要出队,尾指针必须要重新指向头结点,不然丢失该指针了
    189 if (q.front->next==NULL){//或者q.rear==p;
    190 q.rear=q.front;
    191 }
    192 free(p);
    193 return 1;
    194 }
    195 void display(linkQueue &q){
    196 Queueptr p=q.front->next;
    197 while (p){
    198 cout<<p->data<<" ";
    199 p=p->next;
    200 }
    201 cout<<endl;
    202 }
    203 int main()
    204 {
    205     SeqStack s;
    206     Elemtype e;
    207     linkstack top;
    208     Seqqueue q;
    209     linkQueue Q;
    210 
    211     cout<<"---------------------------------------------------"<<endl;
    212     cout<<"1.采用顺序存储实现栈的初始化、入栈、出栈操作"<<endl;
    213     cout<<"2.采用链式存储实现栈的初始化、入栈、出栈操作"<<endl;
    214     cout<<"3.采用顺序存储实现队列的初始化、入队、出队操作"<<endl;
    215     cout<<"4.采用链式存储实现循环队列的初始化、入队、出队操作"<<endl;
    216     cout<<"---------------------------------------------------"<<endl;
    217     int x,i;
    218     cout<<"请输入您的选择!"<<endl;
    219     while (cin>>x){
    220     switch(x){
    221     case 1:{
    222     initSeqStack(s);
    223     cout<<"初始化顺序栈成功!"<<endl;
    224     cout<<"请输入压栈的内容!"<<endl;
    225     cin>>i;
    226     while (i!=-999){
    227     push(s,i);
    228     cin>>i;
    229     }
    230     cout<<"压栈成功!显示栈内数字!"<<endl;
    231     display(s);
    232     cout<<"获取栈顶元素!"<<endl;
    233     gettop(s,e);
    234     printf ("%d
    ",e);
    235     cout<<"弹栈!"<<endl;
    236     while (s.top>0){
    237     pop(s,e);
    238     cout<<"弹出元素:"<<e<<" ";
    239     }
    240     cout<<endl;
    241     cout<<"显示栈内数字!"<<endl;
    242     display(s);
    243     break;
    244     }
    245     case 2:
    246     initLinkstack(top);
    247     cout<<"初始化链栈成功!"<<endl;
    248     cout<<"请输入压栈的内容!"<<endl;
    249     cin>>i;
    250     while (i!=-999){
    251     linkstackPush(top,i);
    252     cin>>i;
    253     }
    254 
    255     cout<<"压栈成功!显示栈内数字!"<<endl;
    256     displaylinkstack(top);
    257     cout<<"获取栈顶元素!"<<endl;
    258     getTop(top,e);
    259     printf ("%d
    ",e);
    260     while (top){
    261     linkstackPop(top,e);
    262     cout<<"弹出元素:"<<e<<" ";
    263     }
    264     cout<<endl;
    265     cout<<"显示栈内数字!"<<endl;
    266     displaylinkstack(top);
    267     break;
    268     case 3:
    269     initSeqqueue(q);
    270     cout<<"初始化顺序队列成功!"<<endl;
    271     cout<<"请输入进队的内容!"<<endl;
    272     cin>>i;
    273     while (i!=-999){
    274     enSeqqueue(q,i);
    275     cin>>i;
    276     }
    277     cout<<"进队成功!显示队内数字!"<<endl;
    278     display(q);
    279     cout<<"返回队头元素!"<<endl;
    280     e=getFront(q);
    281     cout<<e<<endl;
    282     while (!emptySeqqueue(q)){
    283     deSeqqueue(q,e);
    284     cout<<"出队元素"<<e<<" ";
    285     }
    286     cout<<endl;
    287     cout<<"显示队内数字"<<endl;
    288     display(q);
    289     break;
    290     case 4:
    291     initQueue(Q);
    292     cout<<"初始化链队列成功!"<<endl;
    293     cout<<"请输入进队的内容!"<<endl;
    294     cin>>i;
    295     while (i!=-999){
    296     enterQueue(Q,i);
    297     cin>>i;
    298     }
    299     cout<<"进队成功!显示队内数字!"<<endl;
    300     display(Q);
    301 
    302     while (!isempty(Q)){
    303     deQueue(Q,e);
    304     cout<<"出队元素"<<e<<" ";
    305     }
    306     cout<<endl;
    307     cout<<"显示队内数字"<<endl;
    308     display(Q);
    309     break;
    310     }
    311     cout<<"请输入您的选择!"<<endl;
    312     }
    313 
    314     return 0;
    315 }

    这里写图片描述
    这里写图片描述

  • 相关阅读:
    【DWT笔记】基于小波变换的降噪技术
    【DWT笔记】傅里叶变换与小波变换
    Matlab命令——目录操作(windows&Linux)
    【DCT笔记】DCT变换、DCT反变换、分块DCT变换
    JSOI2018 防御网络
    NOI2018 屠龙勇士
    CRT&EXCRT学习笔记
    CF662C Binary Table
    HNOI2017 礼物
    ZJOI2014 力
  • 原文地址:https://www.cnblogs.com/twomeng/p/9476680.html
Copyright © 2020-2023  润新知