数据结构——栈

//数据结构--栈
什么是栈:
栈实际上也是线性表的一种,只是他比较特殊,为什么特殊呢?因为他一端是封闭的,不允许进行插入和删除元素操作,另一端是开口
,允许进行插入和删除元素操作。




栈的特点:
栈的特点是先进后出,后进先出。比如:
我拿一个箱子,要往里面放书,先放:
数学书一本
英文书一本
化学书一本
程序设计书一本
现在我要将这些书全部拿出来,顺序就是
数学书
英文书
化学书
程序设计书
从上面的这个例子看到先进后出,后进先出,这个就是一个现实生活中一个栈的抽象例子。





好了,现在理解了什么是栈,那么现在我们开始用C语言来实现栈。
在写代码前我们先来看看栈中的一些名词:
入栈:往栈中插入一个元素,我们称之为入栈运算;
出栈:从栈中删除一个元素,我们称之为出栈运算;


问来又来了,我们进行一系列的插入和删除操作后,我们怎么知道栈,是否还有空间进行插入新元素。和栈是否为空;
这时是不是就得用一个标识来记录呢!就得用一个指向栈顶的变量。这样我们进行插入和删除操作时,就可以通过指向
栈顶的这个指针来判断当前栈的状态了;


变量意思:
//--------------------------------------------
//s 栈存储空间
//m 栈存储空间容量
//top 栈顶指针;
//x   入栈元素
//--------------------------------------------
下面是C语言代码实现:
//建立空栈:


  1. #include <stdio.h>  
  2. void main()  
  3. {  
  4.     int m, top;  
  5.     int * s;  
  6.     s = (int*)malloc(m * sizeof(int));   //动态申请容量为m的存储空间;  
  7.     top = 0;                             //栈顶指针为0,表示此时栈为空;  
  8. return;  
  9. }  




//入栈运算
  1. void push(int * s, int m, int * top, int x)  
  2. {  
  3.     if(*top == m)  
  4.         {printf("Stack-overfolw!\n");return;}   //检查栈是否已满;  
  5.       
  6.     * top = * top + 1;                            
  7.     s[top - 1] = x;  
  8.       
  9. return;  
  10. }  


//退栈运算
  1. int pop(int * v, int *top)  
  2. {  
  3. int y;  
  4.     if(*top == 0)   
  5.         {printf("Stack-Underflow!\n");return;}  
  6.     y = v[*top -1];  
  7.     * top = * top - 1;  
  8. return (y);  
  9. }  




//读栈顶
  1. int read(int * v, int *top)  
  2. {  
  3.     int y;  
  4.     if(*top == 0 )  
  5.         {printf("Stack_underflow!\n");return;}  
  6.     y = v[*top - 1];  
  7. return (y);  
  8. }  




//状态检查
  1. int flag(int *top, int m)  
  2. {  
  3.     if(*top == m) return (-1);  
  4.     if(*top == 0) return ( 0);  
  5. return(1);  
  6. }  


//输出所有元素


  1. void prt_elem(int *v, int * top)  
  2. {  
  3.     int i;  
  4.     printf("top: %d\n",*top);  
  5.     for(i = 0; i < *top; i++)  
  6.         printf("%d\n",v[i]);  
  7. return;  
  8. }  



例子:

  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. //进栈  
  4. void push(int *v, int *top, int m, int i)  
  5. {  
  6.     if(*top == m)   
  7.         {printf("Stack_overflow!\n");return;}  
  8.     * top = * top + 1;  
  9.     v[*top - 1] = i;  
  10. return;  
  11. }  
  12.   
  13. //退栈  
  14. int pop(int * v, int * top)  
  15. {  
  16.     int y;  
  17.     if(*top == 0)  
  18.     {  
  19.         printf("Stack_underflow!\n");  
  20.         return 0;  
  21.     }  
  22.   
  23.     y = v[*top-1];  
  24.     *top = * top - 1;  
  25. return (y);  
  26. }  
  27.   
  28. //读栈  
  29. int read(int * v, int *top)  
  30. {  
  31.     int y;  
  32.     if(*top == 0)  
  33.     {  
  34.         printf("stack_underflow!\n");  
  35.         return 0;  
  36.     }  
  37.   
  38.     y = v[*top - 1];  
  39. return (y);  
  40. }  
  41.   
  42. //状态检查;  
  43. int flag_stack(int * top, int m)  
  44. {  
  45.     if(*top == m) return (-1); //上溢  
  46.     if(*top == 0) return ( 0);  //栈为空;  
  47. return (1);  
  48. }  
  49. //输出栈中所有元素  
  50. void prt_stack(int *v, int *top)  
  51. {  
  52. int i;  
  53.     printf("top: %d\n",*top);  
  54.     for(i =0; i < *top; i++)  
  55.         printf("%d\n",v[i]);  
  56. return;  
  57. }  
  58. void main()  
  59. {  
  60.     int * v, top, m, i, x;  
  61.     m = 5;   //建立一个能存放十个整形数据的一个栈;  
  62.     v = (int*)malloc(m * sizeof(int));  
  63.     top = 0;   //初始化栈课为空;  
  64.       
  65.     //进行对栈的操作;  
  66.       
  67.     if(flag_stack(&top,m)!=-1)  
  68.     {  
  69.         push(v,&top,m,34);  
  70.     }else   
  71.     {  
  72.         printf("push fail!\n");  
  73.     }  
  74.   
  75.       
  76.     if(flag_stack(&top,m)!=-1)  
  77.     {  
  78.         push(v,&top,m,324);  
  79.     }else   
  80.     {  
  81.         printf("push fail!\n");  
  82.     }  
  83.   
  84.         if(flag_stack(&top,m)!=-1)  
  85.     {  
  86.         push(v,&top,m,334);  
  87.     }else   
  88.     {  
  89.         printf("push fail!\n");  
  90.     }  
  91.   
  92.         if(flag_stack(&top,m)!=-1)  
  93.     {  
  94.         push(v,&top,m,3334);  
  95.     }else   
  96.     {  
  97.         printf("push fail!\n");  
  98.     }  
  99.   
  100.         if(flag_stack(&top,m)!=-1)  
  101.     {  
  102.         push(v,&top,m,334);  
  103.     }else   
  104.     {  
  105.         printf("push fail!\n");  
  106.     }  
  107.   
  108.     if(flag_stack(&top,m)!=-1)   //在插入第六个元素时,已超出栈的容量,故不会进行插入操作了,这时会提示错误!  
  109.     {  
  110.         push(v,&top,m,34);  
  111.     }else   
  112.     {  
  113.         printf("push fail!\n");  
  114.     }  
  115.     prt_stack(v,&top);  
  116.   
  117.     printf("----------------------------\n");  
  118.       
  119. //  int result;  
  120.    printf("pop number is : %d\n",pop(v,&top));  
  121.    printf("read stack top : %d\n",read(v,&top));  
  122.    prt_stack(v,&top);  
  123. }  
  124.   
  125. /*-------------运行结果-------------- 
  126. push fail! 
  127. top: 5 
  128. 34 
  129. 324 
  130. 334 
  131. 3334 
  132. 334 
  133. ---------------------------- 
  134. pop number is : 334 
  135. read stack top : 3334 
  136. top: 4 
  137. 34 
  138. 324 
  139. 334 
  140. 3334 
  141. Press any key to continue 
  142.  
  143. */  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值