zoukankan      html  css  js  c++  java
  • 顺序栈

      1 #include<iostream>
      2 using namespace std;
      3 #define STACKLENGTH 20
      4 typedef struct Stack
      5 {
      6     int s[STACKLENGTH];
      7     int t;
      8 }Stack;
      9 
     10 void initial(Stack *stack)
     11 {
     12     //第一个元素舍去不用
     13     stack->s[0] = 0;
     14     stack->t = 0;
     15 }
     16 //入栈
     17 bool push(Stack *stack,int elem)
     18 {
     19     if (stack->t+1 == STACKLENGTH)
     20     {
     21         cout<<"overflow"<<endl;
     22         return false;
     23     }
     24 
     25     stack->t++;
     26     stack->s[stack->t] = elem;
     27     return true;
     28 }
     29 //弹出栈元素
     30 bool pop(Stack *stack ,int *elem)
     31 {
     32     if (stack->t <= 0 )
     33     {
     34         cout<<"栈空!"<<endl;
     35         return false;
     36     }
     37     *elem = stack->s[stack->t];
     38     stack->t--;
     39     return true;
     40 }
     41 //读取栈顶元素
     42 bool top(Stack *stack, int *elem)
     43 {
     44     if (stack->t <= 0 )
     45     {
     46         cout<<"栈空!"<<endl;
     47         return false;
     48     }
     49     *elem = stack->s[stack->t];
     50 
     51     return true;
     52 }
     53 
     54 //判断栈是否为空
     55 bool isempty(Stack stack)
     56 {
     57     return !stack.t;
     58 }
     59 
     60 //取出栈顶元素
     61 bool poptop(Stack *stack ,int *elem)
     62 {
     63     if(top(stack,elem) && pop(stack,elem))
     64     {
     65         return true;
     66     }
     67     return false;
     68 
     69 }
     70 void print(Stack stack)
     71 {
     72     int nlength = stack.t;
     73     for (int i = 1 ; i <= nlength;i++ )
     74     {
     75         cout<<stack.s[i]<<endl;
     76     }
     77 }
     78 int main()
     79 {
     80     Stack stack;
     81     initial(&stack);
     82     push(&stack,1);
     83     push(&stack,2);
     84     print(stack);
     85     cout<<"---------------------------"<<endl;
     86     int ele;
     87     if(pop(&stack,&ele))
     88     {
     89         cout<<ele<<endl;
     90     }
     91     //print(stack);
     92     cout<<"-----------------------------"<<endl;
     93     if (top(&stack,&ele))
     94     {
     95         cout<<ele<<endl;
     96     }
     97     cout<<"---------------------------------"<<endl;
     98     if (!isempty(stack))
     99     {
    100         cout<<"栈非空!"<<endl;
    101     }
    102 
    103     cout<<"-------------------------------------"<<endl;
    104     if (poptop(&stack,&ele))
    105     {
    106         cout<<ele<<endl;
    107     }
    108 
    109     cout<<"------------------------------------"<<endl;
    110     if (isempty(stack))
    111     {
    112         cout<<"栈空!"<<endl;
    113     }
    114     return 0 ;
    115 }

    t不仅标志栈是否为空,还是数组的下标,两个作用!

  • 相关阅读:
    [SDOI2017]新生舞会
    [SCOI2007]最大土地面积
    [JLOI2014]松鼠的新家
    [AHOI2009]中国象棋
    【转载】树链剖分.By.Xminh
    HGOI20180904(NOIP2018模拟sxn出题)
    HGOI20180831 NOIP2018模拟
    【字符串算法1】 再谈字符串Hash(优雅的暴力)
    【字符串算法2】浅谈Manacher算法
    【字符串算法3】浅谈KMP算法
  • 原文地址:https://www.cnblogs.com/yaoxc/p/3301093.html
Copyright © 2011-2022 走看看