C语言实现栈(基于数组)

来源:互联网 发布:betterzip mac版 编辑:程序博客网 时间:2024/06/15 02:19

栈是一种操作受限的数据结构,只允许从一段操作,而且先进后出(FILO  first in last out)这里将栈的操作封装在C语言的头文件里

实现栈的代码如下

[cpp] view plain copy
  1. #include<stdbool.h>  
  2. #define maxsize 10  
  3. typedef int datatype;  
  4.   
  5. //Sequence Stack 实现顺序栈,使用数组来实现  
  6. struct stack  
  7. {  
  8.     datatype data[maxsize];  
  9.     int top;  
  10. };  
  11.   
  12. typedef struct stack Stack;  
  13. //创建栈  
  14.   
  15. Stack s;  
  16. //初始化栈  
  17. void init()  
  18. {  
  19.     s.top=-1;  
  20. }  
  21.   
  22. //判断栈是否为空  
  23. bool Empty()  
  24. {  
  25.     if(s.top==-1)  
  26.     {  
  27.         return true;  
  28.     }  
  29.     else  
  30.     {  
  31.         return false;  
  32.     }  
  33. }  
  34.   
  35. //判断栈是否已满了  
  36. bool full()  
  37. {  
  38.     if(s.top==maxsize-1)  
  39.     {  
  40.         return true;  
  41.     }  
  42.     else  
  43.     {  
  44.         return false;  
  45.     }  
  46. }  
  47.   
  48. //入栈  
  49. void Push(datatype element)  
  50. {  
  51.     if(!full())  
  52.     {  
  53.         s.top++;  
  54.         s.data[s.top]=element;  
  55.     }  
  56.     else  
  57.     {  
  58.         printf("栈满\n");  
  59.     }  
  60. }  
  61.   
  62. //出栈  
  63. void Pop()  
  64. {  
  65.     if(!Empty())  
  66.     {  
  67.         s.top--;  
  68.     }  
  69.     else  
  70.     {  
  71.         printf("栈空\n");  
  72.     }  
  73. }  
  74.   
  75. //取栈顶元素  
  76. datatype Top()  
  77. {  
  78.     if(!Empty())  
  79.     {  
  80.         return s.data[s.top];  
  81.     }  
  82.     else  
  83.     {  
  84.         printf("栈空\n");  
  85.     }  
  86. }  
  87.   
  88. //销毁栈  
  89. void Destroy()  
  90. {  
  91.     s.top=-1;  
  92. }  


测试栈的代码如下:

[cpp] view plain copy
  1. #include<stdio.h>  
  2. #include "SequenceStack.h"  
  3. int main()  
  4. {  
  5.     int i=0;  
  6.     // struct stack s;  
  7.     //初始化栈  
  8.     printf("\n###########初始化栈###########\n");  
  9.     init();  
  10.     printf("----------------------------------");      
  11.     //入栈操作  
  12.     printf("\n###########入栈操作###########\n");  
  13.     for(i=0;i<=10;i++)  
  14.     {  
  15.         Push(i);  
  16.     }  
  17.     printf("----------------------------------");  
  18.     printf("\n###########取栈顶元素###########\n");  
  19.     printf("%d\n",Top());  
  20.     printf("----------------------------------");      
  21.     //出栈操作  
  22.     printf("\n###########出栈操作###########\n");  
  23.     for(i=0;i<=10;i++)  
  24.     {  
  25.         Pop();  
  26.     }    
  27.     printf("----------------------------------");      
  28.     printf("\n###########取栈顶元素###########\n");  
  29.     Top();  
  30.     printf("----------------------------------");  
  31.     printf("\n###########销毁栈###########\n");   
  32.     Push(10);  
  33.     Destroy();  
  34.     Top();  
  35. }  


这样实现栈,宏定义处maxsize表示栈的大小,方便以后修改,typedef也是同样的效果,可以随时改变数据类型。

在操作栈的时候也只要用到push、pop、top、等方法就可以了

这样的栈有一个明显的缺点,是栈被定义在头文件里,这样使用者只能使用一个栈,无法同时建立两个栈,考虑到这一点,对栈的代码做出修改

实现栈的代码

[cpp] view plain copy
  1. #include<stdbool.h>  
  2. #define maxsize 10  
  3. typedef int datatype;  
  4.   
  5. //Sequence Stack 实现顺序栈,使用数组来实现  
  6. struct stack  
  7. {  
  8.     datatype data[maxsize];  
  9.     int top;  
  10. };  
  11.   
  12. typedef struct stack Stack;  
  13. //创建栈  
  14.   
  15. // Stack s;  
  16. //初始化栈  
  17. void init(Stack *s)  
  18. {  
  19.     s->top=-1;  
  20. }  
  21.   
  22. //判断栈是否为空  
  23. bool Empty(Stack *s)  
  24. {  
  25.     if(s->top==-1)  
  26.     {  
  27.         return true;  
  28.     }  
  29.     else  
  30.     {  
  31.         return false;  
  32.     }  
  33. }  
  34.   
  35. //判断栈是否已满了  
  36. bool full(Stack *s)  
  37. {  
  38.     if(s->top==maxsize-1)  
  39.     {  
  40.         return true;  
  41.     }  
  42.     else  
  43.     {  
  44.         return false;  
  45.     }  
  46. }  
  47.   
  48. //入栈  
  49. void Push(Stack *s,datatype element)  
  50. {  
  51.     if(!full(s))  
  52.     {  
  53.         s->top++;  
  54.         s->data[s->top]=element;  
  55.     }  
  56.     else  
  57.     {  
  58.         printf("栈满\n");  
  59.     }  
  60. }  
  61.   
  62. //出栈  
  63. void Pop(Stack *s)  
  64. {  
  65.     if(!Empty(s))  
  66.     {  
  67.         s->top--;  
  68.     }  
  69.     else  
  70.     {  
  71.         printf("栈空\n");  
  72.     }  
  73. }  
  74.   
  75. //取栈顶元素  
  76. datatype Top(Stack *s)  
  77. {  
  78.     if(!Empty(s))  
  79.     {  
  80.         return s->data[s->top];  
  81.     }  
  82.     else  
  83.     {  
  84.         printf("栈空\n");  
  85.     }  
  86. }  
  87.   
  88. //销毁栈  
  89. void Destroy(Stack *s)  
  90. {  
  91.     s->top=-1;  
  92. }  

测试栈的代码

[cpp] view plain copy
  1. #include<stdio.h>  
  2. #include "SequenceStack1.0.h"  
  3. int main()  
  4. {  
  5.     int i=0;  
  6.     Stack p;   
  7.     Stack *s;  
  8.     s=&p;  
  9.     // struct stack s;  
  10.     //初始化栈  
  11.     printf("\n###########初始化栈###########\n");  
  12.     init(s);  
  13.     printf("----------------------------------");      
  14.     //入栈操作  
  15.     printf("\n###########入栈操作###########\n");  
  16.     for(i=0;i<=10;i++)  
  17.     {  
  18.         Push(s,i);  
  19.     }  
  20.     printf("----------------------------------");  
  21.     //取栈顶元素  
  22.     printf("\n###########取栈顶元素###########\n");  
  23.     printf("%d\n",Top(s));  
  24.     printf("----------------------------------");      
  25.     //出栈操作  
  26.     printf("\n###########出栈操作###########\n");  
  27.     for(i=0;i<=12;i++)  
  28.     {  
  29.         Pop(s);  
  30.     }    
  31.     printf("----------------------------------");  
  32.     //取栈顶元素      
  33.     printf("\n###########取栈顶元素###########\n");  
  34.     Top(s);  
  35.     printf("----------------------------------");  
  36.     //销毁栈  
  37.     printf("\n###########销毁栈###########\n");   
  38.     Push(s,10);  
  39.     Destroy(s);  
  40.     Top(s);  
  41. }  

这里可以在主函数中自己定义栈,从而同时实现多个栈,但是这样的栈还是大小固定的,每次修改maxsize很麻烦,所以想到malloc函数,对栈的代码做了进一步的修改

实现栈的代码

[cpp] view plain copy
  1. #include<stdbool.h>  
  2. #include<stdlib.h>  
  3. #define maxsize 10  
  4. typedef int datatype;  
  5.   
  6. //Sequence Stack 实现顺序栈,使用数组来实现  
  7. struct stack  
  8. {  
  9.     datatype *data;  
  10.     int top;  
  11. };  
  12.   
  13. typedef struct stack Stack;  
  14. //创建栈  
  15.   
  16. int realsize=maxsize;  
  17.   
  18. // Stack s;  
  19. //初始化栈  
  20. void init(Stack *s)  
  21. {  
  22.     s->data = (datatype *)malloc(sizeof(datatype)*maxsize);  
  23.     s->top=-1;  
  24. }  
  25.   
  26. //判断栈是否为空  
  27. bool Empty(Stack *s)  
  28. {  
  29.     if(s->top==-1)  
  30.     {  
  31.         return true;  
  32.     }  
  33.     else  
  34.     {  
  35.         return false;  
  36.     }  
  37. }  
  38.   
  39. //判断栈是否已满了  
  40. void full(Stack *s)  
  41. {  
  42.     if(s->top==realsize-1)  
  43.     {  
  44.         realsize++;  
  45.         s->data=(datatype *)realloc(s->data,realsize);  
  46.     }  
  47. }  
  48.   
  49. //入栈  
  50. void Push(Stack *s,datatype element)  
  51. {  
  52.     full(s);  
  53.     s->top++;  
  54.     s->data[s->top]=element;  
  55. }  
  56.   
  57. //出栈  
  58. void Pop(Stack *s)  
  59. {  
  60.     if(!Empty(s))  
  61.     {  
  62.         s->top--;  
  63.     }  
  64.     else  
  65.     {  
  66.         printf("栈空\n");  
  67.     }  
  68. }  
  69.   
  70. //取栈顶元素  
  71. datatype Top(Stack *s)  
  72. {  
  73.     if(!Empty(s))  
  74.     {  
  75.         return s->data[s->top];  
  76.     }  
  77.     else  
  78.     {  
  79.         printf("栈空\n");  
  80.     }  
  81. }  
  82.   
  83. //销毁栈  
  84. void Destroy(Stack *s)  
  85. {  
  86.     s->top=-1;  
  87.     // free(s.data); //容易导致失败  
  88. }  

测试栈的代码

[cpp] view plain copy
  1. #include<stdio.h>  
  2. #include "DynamicStack1.0.h"  
  3. int main()  
  4. {  
  5.     int i=0;  
  6.     Stack p;   
  7.     Stack *s;  
  8.     s=&p;  
  9.     // struct stack s;  
  10.     //初始化栈  
  11.     printf("\n###########初始化栈###########\n");  
  12.     init(s);  
  13.     printf("----------------------------------");      
  14.     //入栈操作  
  15.     printf("\n###########入栈操作###########\n");  
  16.     for(i=0;i<=10;i++)  
  17.     {  
  18.         Push(s,i);  
  19.     }  
  20.     printf("----------------------------------");  
  21.     //取栈顶元素  
  22.     printf("\n###########取栈顶元素###########\n");  
  23.     printf("%d\n",Top(s));  
  24.     printf("----------------------------------");      
  25.     //出栈操作  
  26.     printf("\n###########出栈操作###########\n");  
  27.     for(i=0;i<=12;i++)  
  28.     {  
  29.         Pop(s);  
  30.     }    
  31.     printf("----------------------------------");  
  32.     //取栈顶元素      
  33.     printf("\n###########取栈顶元素###########\n");  
  34.     Top(s);  
  35.     printf("----------------------------------");  
  36.     //销毁栈  
  37.     printf("\n###########销毁栈###########\n");   
  38.     Push(s,10);  
  39.     Destroy(s);  
  40.     Top(s);  
  41. }  

这样就可以实现无限大小的栈了,这里realloc之后的指针,在free的时候容易出错

原创粉丝点击