复习 [数据结构] ---- 栈和队列

        1. /*------------------------------------- 
        2.     链队列  
        3. -------------------------------------*/ 
        4. #include <stdio.h> 
        5. #include <stdlib.h> 
        6. #define ElemType char 
        7. #define MaxSize 100
        8. typedef struct node     //定义节点类型 
        9.     ElemType data;      //数据域 
        10.     struct node *next;  //指针域 
        11. }SNode; 
        12. typedef struct
        13. {   
        14.     SNode *front;
        15.     SNode *rear;
        16. }LQueue;
        17. void InitQueue(LQueue *&qu)
        18. {
        19.     qu=(LQueue *)malloc(sizeof(LQueue));    //建立链队头节点
        20.     qu->front=qu->rear=NULL;
        21. }
        22. void EnQueue(LQueue *&qu,ElemType x)
        23. {
        24.     SNode *s;
        25.     s=(SNode *)malloc(sizeof(SNode));
        26.     s->data=x;
        27.     s->next=NULL;
        28.     if(qu->rear==NULL)                  //原来为空队
        29.     {
        30.         qu->front=qu->rear=s;
        31.     }
        32.     else
        33.     {
        34.         qu->rear->next=s;               //将*s插入队尾
        35.         qu->rear=s;                     //由qu->rear指向
        36.     }
        37. }
        38. int DeQueue(LQueue *&qu,ElemType &x)
        39. {
        40.     SNode *p;
        41.     if(qu->rear==NULL)
        42.         return 0;
        43.     p=qu->front;
        44.     if(qu->front==qu->rear)
        45.         qu->front=qu->rear=NULL;
        46.     else
        47.         qu->front=qu->front->next;
        48.     x=p->data;
        49.     free(p);
        50.     return 1;
        51. }
        52. int QueueEmpty(LQueue *qu)
        53. {
        54.     if(qu->front==NULL)
        55.         return 1;
        56.     else
        57.         return 0;
        58. }
        59. void main()
        60. {
        61.     LQueue *lq;
        62.     InitQueue(lq);
        63.     EnQueue(lq,'A');
        64.     EnQueue(lq,'B');
        65.     EnQueue(lq,'C');
        66.     EnQueue(lq,'D');
        67.     EnQueue(lq,'E');
        68.     char Temp;
        69.     Temp=NULL;
        70.     DeQueue(lq,Temp);
        71.     printf("出队元素为:%c/n",Temp);
        72.     Temp=NULL;
        73.     DeQueue(lq,Temp);
        74.     printf("出队元素为:%c/n",Temp);
        75.     Temp=NULL;
        76.     DeQueue(lq,Temp);
        77.     printf("出队元素为:%c/n",Temp);
        78.     if(QueueEmpty(lq))
        79.         printf("/n链队列为空/n");
        80.     else
        81.         printf("/n链队列中还有元素/n");
        82. }
        /*------------------------------------- 
      1.     链栈   
      2. -------------------------------------*/ 
      3. #include <stdio.h> 
      4. #include <stdlib.h> 
      5. #define ElemType char 
      6. typedef struct node     //定义节点类型 
      7.     ElemType data;      //数据域 
      8.     struct node *next;  //指针域 
      9. }SNode; 
      10. void InitStack(SNode *&st)  //置空栈   
      11. {
      12.     st=NULL;
      13. }
      14. void push(SNode *&st,ElemType x)    //在栈顶指针是st的链栈中插入一个值为x的节点
      15. {
      16.     SNode *p;
      17.     p=(SNode *)malloc(sizeof(SNode));       //创建一个节点*p 
      18.     p->data=x;
      19.     p->next=NULL;
      20.     if(st==NULL)                            //原来为空栈
      21.         st=p;
      22.     else
      23.     {
      24.         p->next=st;
      25.         st=p;
      26.     }
      27. }
      28. int gettop(SNode *st,ElemType &x)           //将栈st的栈顶节点的data域值赋给x
      29. {
      30.     if(st==NULL)                            //向下溢出
      31.         return 0;                           //返回失败
      32.     else
      33.     {
      34.         x=st->data;
      35.         return 1;                           //返回成功
      36.     }
      37. }
      38. int pop(SNode *&st,ElemType &x)             //将栈st的栈顶节点的data域值赋给x,然后删除栈顶节点
      39. {
      40.     SNode *p=st;
      41.     if(st==NULL)
      42.         return 0;
      43.     else
      44.     {
      45.         x=st->data;
      46.         st=st->next;
      47.         free(p);
      48.         return 1;
      49.     }
      50. }
      51. int StackEmpty(SNode *st)
      52. {
      53.     if(st==NULL)
      54.         return 1;
      55.     else
      56.         return 0;
      57. }
      58. void main()
      59. {
      60.     SNode *ss;
      61.     
      62.     InitStack(ss);
      63.     push(ss,'A');
      64.     push(ss,'B');
      65.     push(ss,'C');
      66.     push(ss,'D');
      67.     push(ss,'E');
      68.     char Temp;
      69.     gettop(ss,Temp);
      70.     printf("栈顶元素:%c/n",Temp);
      71.     Temp=NULL;
      72.     pop(ss,Temp);
      73.     printf("出栈元素:%c/n",Temp);
      74.     gettop(ss,Temp);
      75.     printf("栈顶元素:%c/n",Temp);
      76.     if(StackEmpty(ss))
      77.         printf("/n栈为空/n");
      78.     else
      79.         printf("/n栈中还有元素/n");
      80. }
      /*-----------------------------------
    1.     顺序队列
    2. -----------------------------------*/
    3. #include <stdio.h>
    4. #include <stdlib.h>
    5. #define ElemType char 
    6. #define MaxSize 50
    7. typedef struct
    8. {
    9.     ElemType data[MaxSize];     //存放队列元素
    10.     int front,rear;             //定义队头指针和队尾指针
    11. }Queue;
    12. void InitQueue(Queue &qu)
    13. {
    14.     qu.rear=qu.front=0;
    15. }
    16. int EnQueue(Queue &qu,ElemType x)       //入队
    17. {
    18.     if((qu.rear+1)%MaxSize==qu.front)   //队满
    19.         return 0;                       //报错
    20.     else
    21.     {
    22.         qu.rear=(qu.rear+1)%MaxSize;
    23.         qu.data[qu.rear]=x;
    24.         return 1;
    25.     }
    26. }
    27. int DnQueue(Queue &qu,ElemType x)       //出队
    28. {
    29.     if(qu.rear==qu.front)               //队空
    30.         return 0;                       //报错
    31.     else
    32.     {
    33.         qu.front=(qu.front+1)%MaxSize;
    34.         x=qu.data[qu.front];
    35.         return 1;
    36.     }
    37. }
    38. int QueueEmpty(Queue qu)
    39. {
    40.     if(qu.rear==qu.front)               //队空
    41.         return 1;
    42.     else
    43.         return 0;
    44. }
    45. void main()
    46. {
    47.     Queue qq;
    48.     InitQueue(qq);
    49.     EnQueue(qq,'A');
    50.     EnQueue(qq,'B');
    51.     EnQueue(qq,'C');
    52.     EnQueue(qq,'D');
    53.     EnQueue(qq,'E');
    54.     if(QueueEmpty(qq))
    55.         printf("/n队列为空/n");
    56.     else
    57.         printf("/n队列中还有元素/n");
    58. }
    /*---------------------------------
  1.     顺序栈
  2.   特别感谢  诛仙(QQ:16822065) 
  3. ---------------------------------*/
  4. #include <stdio.h> 
  5. #define MaxSize 100 
  6. typedef char ElemType; 
  7. typedef struct 
  8.     ElemType s[MaxSize]; 
  9.     int top; 
  10. }Stack;
  11. void InitStack(Stack &st)       //置栈空
  12. {
  13.     st.top = -1;
  14. }
  15. int push(Stack &st,ElemType x)  //将元素x压入到栈st中
  16. {
  17.     if(st.top == MaxSize - 1)   //栈上溢出
  18.         return 0;
  19.     else                        //否则栈指针增1,将x赋给栈顶元素
  20.     {
  21.         st.top++;
  22.         st.s[st.top]=x;
  23.         return 1;
  24.     }
  25. }
  26. int gettop(Stack st,ElemType &x)//将栈st的栈顶元素赋给x,栈指针不变
  27. {
  28.     if(st.top==-1)              //栈下溢出
  29.         return 0;
  30.     else
  31.     {
  32.         x=st.s[st.top];         //否则将栈顶元素值赋给x
  33.         return 1;               //返回成功信息
  34.     }
  35. }
  36. int pop(Stack &st,ElemType &x)  //将栈st的栈顶元素赋给x,栈指针递减
  37. {
  38.     if(st.top==-1)              //栈下溢出
  39.         return 0;
  40.     else
  41.     {
  42.         x=st.s[st.top];         //否则把栈顶元素赋给x
  43.         st.top--;
  44.         return 1;               //返回成功信息
  45.     }
  46. }
  47. int StackEmpty(Stack st)
  48. {
  49.     if(st.top==-1)
  50.         return 1;               //如果栈为空返回1
  51.     else
  52.         return 0;               //否则返回0
  53. }
  54. void main()
  55. {
  56.     Stack ss;
  57.     InitStack(ss);
  58.     push(ss,'A');
  59.     push(ss,'B');
  60.     push(ss,'C');
  61.     push(ss,'D');
  62.     char Temp;
  63.     gettop(ss,Temp);
  64.     printf("栈顶元素为:%c/n",Temp);
  65.     Temp=NULL;
  66.     pop(ss,Temp);
  67.     printf("出栈元素为:%c/n",Temp);
  68.     
  69.     gettop(ss,Temp);
  70.     printf("栈顶元素为:%c/n",Temp);
  71.     if(StackEmpty(ss))
  72.         printf("/n栈为空/n");
  73.     else
  74.         printf("/n栈中还有元素/n");
  75. }

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值