zoukankan      html  css  js  c++  java
  • 顺序栈的基本操作

      1    /*顺序栈的基本操作*/
      2#include "stdafx.h"
      3     #include <stdio.h>
      4     #define MaxSize 100
      5
      6     typedef char ElemType;
      7     typedef struct
      8     {
      9        char stack[MaxSize];
     10        int top;
     11     }
    stacktype;
     12//****************************************************************initstack()
     13     void initstack(stacktype *S)
     14     {
     15        S->top=-1;
     16     }

     17//****************************************************************push()
     18     void push(stacktype *S,ElemType x)
     19     {
     20           if (S->top==MaxSize) 
     21               printf("栈上溢出!\n");
     22           else
     23           {
     24              S->top++;
     25              S->stack[S->top]=x;
     26           }

     27     }

     28//****************************************************************pop()
     29     void pop(stacktype *S)
     30     {
     31       if (S->top==-1)
     32           printf("栈下溢出!\n");
     33       else
     34           S->top--;
     35     }

     36//****************************************************************gettop()
     37     ElemType gettop(stacktype *S)
     38     {
     39         if (S->top==-1)
     40         {
     41             printf("栈空!\n");
     42             return NULL;
     43         }

     44         else return(S->stack[S->top]);
     45     }

     46//****************************************************************empty()
     47     int empty(stacktype *S)
     48     {
     49       if (S->top==-1)
     50           return(1);   //空栈则返回1
     51       else
     52           return(0);
     53     }

     54//****************************************************************display()
     55     void display(stacktype *S)
     56     {
     57       int i;
     58       printf("栈中元素:");
     59       for (i=S->top;i>=0;i--)
     60       printf("%c ",S->stack[i]);
     61       printf("\n");
     62     }

     63
     64  void main()
     65  {
     66       stacktype L;
     67       stacktype *st=&L;
     68       printf("建立一空栈\n");
     69       initstack(st);
     70       printf("栈空:%d\n",empty(st));
     71       printf("依次插入a,b,c,d元素\n");
     72       push(st,'a');
     73       push(st,'b');
     74       push(st,'c');
     75       push(st,'d');
     76       display(st);
     77       printf("退一次栈\n");
     78       pop(st);
     79       printf("栈顶元素:%c\n",gettop(st));
     80       printf("退一次栈\n");
     81       pop(st);
     82       display(st);
     83  }

     84
     85/*运行结果如下:
     86
     87    建立一空栈
     88    栈空:1
     89    依次插入a,b,c,d元素
     90    栈中元素:d c b a
     91    退一次栈
     92    栈顶元素:c
     93    退一次栈
     94    栈中元素:b a
     95
     96*/

     97
     98
     99
    100
  • 相关阅读:
    西交应用统计学(四)
    SPSS非参数检验
    并查集实现
    二叉树遍历非递归算法
    算法导论——渐近符号、递归及解法
    SPSS均值过程和T检验
    二维数组的查找及向函数传递二维数组问题
    printf()的格式
    C++ string类型的读写
    替换字符串中的空格
  • 原文地址:https://www.cnblogs.com/yuxi/p/647076.html
Copyright © 2011-2022 走看看