使用數組實現堆棧

 

  1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3. #define MAX 10 
  4.  
  5. int stack[10]; 
  6. int top=-1; 
  7. void push(int number) 
  8.     printf("push a number\n"); 
  9.     if(top>=9) 
  10.     { 
  11.         printf("the stack is full"); 
  12.     }else
  13.     top++; 
  14.     stack[top]=number; 
  15.     } 
  16.  
  17. int  pop() 
  18.     int temp; 
  19.     printf("pop a number\n"); 
  20.     if(top<0) 
  21.     { 
  22.         printf("the stack is empty"); 
  23.         return -1; 
  24.     }else
  25.             temp=stack[top]; 
  26.             top--; 
  27.             printf("%d",temp); 
  28.             return temp; 
  29.     } 
  30.  
  31. void show() 
  32.  
  33.     for(;top>=0;top--) 
  34.     { 
  35.         printf("%d\t",stack[top]); 
  36.     } 
  37.  
  38.  
  39. int main() 
  40.     int select; 
  41.     int number; 
  42.  
  43.     printf("(1)please input a stack data\n"); 
  44.     printf("(2)please output a stack data\n"); 
  45.     printf("(3)exit\n"); 
  46.     printf("please input one by input a number"); 
  47.  
  48.     scanf("%d",&select); 
  49.  
  50.     while(select!=3) 
  51.     { 
  52.         switch(select) 
  53.         { 
  54.             case 1:scanf("%d",&number);push(number);break
  55.             case 2:pop();break
  56.             default:exit;break
  57.         } 
  58.         printf("(1)please input a stack data\n"); 
  59.         printf("(2)please output a stack data\n"); 
  60.         printf("(3)exit"); 
  61.         printf("please input one by input a number\n"); 
  62.  
  63.         scanf("%d",&select); 
  64.     } 
  65.  
  66.     show(); 
  67.  
  68.     return 0; 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章