顺序栈和链栈的各种操作

来源:互联网 发布:网络语言bbs是什么意思 编辑:程序博客网 时间:2024/05/01 21:19

一.先说顺序栈

view plainprint?
  1. /********************************************************** 
  2. 顺序栈的各种操作13-5-1.cpp 
  3. 1.初始化栈 
  4. 2.判断栈空 
  5. 3.入栈 
  6. 4.出栈 
  7. 5.取栈顶元素 
  8. 6.主函数测试以上功能 
  9. ***********************************************************/  
  10. #include<stdio.h>  
  11. #include<stdlib.h>  
  12.   
  13. #define MAX 1000  
  14. typedef struct  
  15. {  
  16.     int data[MAX];  
  17.     int top;  
  18. }SeStack;  
  19.   
  20. void InitStack(SeStack *S)  
  21. {  
  22.     S->top=-1;  
  23. }  
  24.   
  25. int EmptyStack(SeStack *S)  
  26. {  
  27.     return ((S->top==-1)?1:0);  
  28. }  
  29.   
  30. int Push(SeStack *S, int x)  
  31. {  
  32.     if(S->top==MAX-1)  
  33.     {  
  34.         printf("栈满!\n");  
  35.         return 0;  
  36.     }  
  37.     S->data[++S->top]=x;  
  38.     return 1;  
  39. }  
  40.   
  41. int Pop(SeStack *S, int *x)  
  42. {  
  43.     if(EmptyStack(S))  
  44.     {  
  45.         printf("栈空!\n");  
  46.         return 0;  
  47.     }  
  48.     *x=S->data[S->top--];  
  49.     return 1;  
  50. }  
  51.   
  52. int GetTop(SeStack *S, int *x)  
  53. {  
  54.     if(EmptyStack(S))  
  55.     {  
  56.         printf("栈空!\n");  
  57.         return 0;  
  58.     }  
  59.     *x=S->data[S->top];  
  60.     return 1;  
  61. }  
  62.   
  63. int main()  
  64. {  
  65.     SeStack *S=(SeStack *)malloc(sizeof(SeStack));  
  66.     int n, x, fx, j;  
  67.     InitStack(S);  
  68.     printf("输入入栈元素个数:\n");  
  69.     scanf("%d",&n);  
  70.     for(int i=1; i<=n; i++)  
  71.     {  
  72.         Push(S, i);  
  73.     }  
  74.   
  75.     GetTop(S, &fx);  
  76.     printf("栈顶元素:%d\n", fx);  
  77.       
  78.     printf("栈中元素:\n");  
  79.     for(j=S->top; j>-1; j--)  
  80.     {  
  81.         printf("%3d",S->data[j]);  
  82.     }  
  83.   
  84.     for(int i=1; i<=n; i++)  
  85.     {  
  86.         Pop(S, &x);  
  87.         printf("\n出栈元素为:%d\n", x);  
  88.     }  
  89.     system("pause");  
  90.     return 0;  
  91. }  

二.再说链栈

view plainprint?
  1. /********************************************************** 
  2. 链栈的各种操作13-5-2.cpp和13-5-3.cpp 
  3. 1.初始化栈 
  4. 2.判断栈空 
  5. 3.入栈 
  6. 4.出栈 
  7. 5.取栈顶元素 
  8. 6.主函数测试以上功能 
  9. ***********************************************************/  
  10. #include<stdio.h>  
  11. #include<stdlib.h>  
  12.   
  13. typedef struct Lnode  
  14. {  
  15.     int data;  
  16.     struct Lnode *next;  
  17. }LinkList;  
  18. typedef LinkList LinkStack;  
  19.   
  20. void  InitStack(LinkStack *top)  
  21. {  
  22.     /* 
  23.     top=(LinkStack *)malloc(sizeof(LinkStack));//(1) 
  24.     if(top==NULL) 
  25.     { 
  26.         printf("分配空间失败!\n"); 
  27.         return 0; 
  28.     } 
  29. */  
  30.     //printf("(InitStack)top addr:%x\n", top);  
  31.     top->next=NULL;  
  32.     //return 1;  
  33. }  
  34.   
  35. int EmptyStack(LinkStack *top)  
  36. {  
  37.     return ((top->next==NULL)?1:0);  
  38. }  
  39.   
  40. int Push(LinkStack *top, int x)  
  41. {  
  42.     LinkStack *s=(LinkStack *)malloc(sizeof(LinkStack));  
  43.     if(s==NULL)  
  44.     {  
  45.         printf("分配空间失败!\n");  
  46.         return 0;  
  47.     }  
  48.     s->data=x;  
  49.     s->next=top->next;  
  50.     top->next=s;  
  51.     //printf("(Push)top addr:%x\n", top);  
  52.     return 1;  
  53. }  
  54.   
  55. int Pop(LinkStack *top, int *x)  
  56. {  
  57.     LinkStack *p;  
  58.     if(EmptyStack(top))  
  59.     {  
  60.         printf("栈空!\n");  
  61.         return 0;  
  62.     }  
  63.     *x=top->next->data;  
  64.     p=top->next;  
  65.     top->next=p->next;  
  66.     //printf("(Pop)top addr:%x\n", top);  
  67.     free(p);  
  68.     return 1;  
  69. }  
  70.   
  71. int GetTop(LinkStack *top, int *x)  
  72. {  
  73.     if(EmptyStack(top))  
  74.     {  
  75.         printf("栈空!\n");  
  76.         return 0;  
  77.     }  
  78.     *x=top->next->data;  
  79.     return 1;  
  80. }  
  81.   
  82. int main()  
  83. {  
  84.     LinkStack *top=(LinkStack *)malloc(sizeof(LinkStack));  
  85.     //printf("(main)top addr:%x\n", top);  
  86.     LinkStack *p;  
  87.     int n, x, fx;  
  88.   
  89.     InitStack(top);  
  90.   
  91.     printf("输入入栈元素个数n:\n");  
  92.     scanf("%d", &n);  
  93.     for(int i=1; i<=n; i++)  
  94.     {  
  95.         Push(top, i);  
  96.     }  
  97.   
  98.     GetTop(top, &fx);  
  99.     printf("栈顶元素:%d\n", fx);  
  100.     printf("栈中元素:\n");  
  101.     for(p=top->next; p!=NULL; p=p->next)//(2)  
  102.     {  
  103.         printf("%3d", p->data);  
  104.     }     
  105. for(int i=1; i<=n; i++)  
  106.     {  
  107.         Pop(top, &x);  
  108.         printf("\n出栈元素为:%d\n", x);  
  109.     }  
  110.     system("pause");  
  111.     return 0;  
  112. }  

原创粉丝点击