• C语言实现栈


      1 /**
      2  * 数据结构中的栈C语言实现 
      3  * 参考书籍:《数据结构C语言版》.严蔚敏 吴伟民 著. 
      4  * 编译环境:windows 7 x64
      5  * 编辑软件:c-free 5.0 
      6  * 树琦博客:http://www.cnblogs.com/shuqi 
      7  * 文件名:stack.cpp
      8  * 创建时间:2014年4月21日  19:49:19 
      9  */
     10  
     11  /********************************引入头文件**************************************************/ 
     12 #include<stdio.h>
     13 #include<stdlib.h>
     14 #include<malloc.h>
     15 
     16 /**********************************定义*******************************************************/ 
     17 /*定义状态码*/  
     18 #define OK 0            //正常 
     19 #define ERROR -1        //出错 
     20 #define OVERFLOW -2        //内存申请不成功    
     21 #define DEFSIZE 10      //栈的默认大小 
     22 #define INCREAMSIZE 10     //每次当栈空间满时,增量 
     23 /* 定义结构体 */
     24 typedef int Status;     //定义状态     
     25 typedef int ElemType;      //定义栈内元素类型
     26 
     27 //定义栈的的数据结构 
     28 typedef struct{
     29     ElemType *base;     //栈底指针 
     30     ElemType *top;        //栈顶指针 
     31     int stackSize;        // 栈大小 
     32     int realSize;        // 栈当前大小,可以不定义 
     33 }SqStack; 
     34 
     35 /*********************************stack操作方法**************************************************/
     36 
     37 //初始化一个栈
     38 Status InitStack(SqStack &sqstack){
     39     //申请默认栈大小 
     40     sqstack.base = (ElemType*)malloc(DEFSIZE*sizeof(ElemType)); 
     41     if(!sqstack.base) exit(OVERFLOW);
     42     
     43     sqstack.top  = sqstack.base;
     44     sqstack.stackSize = DEFSIZE;
     45     sqstack.realSize = 0; 
     46     return OK;
     47 }
     48 
     49 //进栈
     50 Status Push(SqStack &sqstack,ElemType &e){
     51     if(sqstack.top-sqstack.base>=sqstack.stackSize){
     52         sqstack.base = (ElemType*)realloc(sqstack.base,(sqstack.stackSize+INCREAMSIZE)*sizeof(ElemType)); 
     53         //如果申请失败返回溢出 
     54         if(!sqstack.base) exit(OVERFLOW);
     55         sqstack.top = sqstack.base + sqstack.stackSize;
     56         sqstack.stackSize = sqstack.stackSize + INCREAMSIZE;
     57     }
     58     *sqstack.top++ = e;
     59     sqstack.realSize++;
     60     return OK;
     61 }
     62 
     63 //出栈
     64 Status Pop(SqStack &sqstack,ElemType &e){
     65     if(sqstack.base==sqstack.top){
     66         exit(ERROR);
     67     }
     68     e = *--sqstack.top;
     69     sqstack.realSize--;
     70     return OK;
     71 }
     72 
     73 //得到栈顶元素 
     74 Status GetTop(SqStack &sqstack,ElemType &e){
     75     if(sqstack.base==sqstack.top){
     76         exit(ERROR);
     77     }
     78     e = *(sqstack.top-1);
     79     return OK;
     80 }
     81 
     82 //判断栈是否为空
     83 bool IsEmpty(SqStack &sqstack){
     84     if(sqstack.realSize>0)
     85         return false;
     86     else
     87         return true;
     88 } 
     89 
     90 //销毁栈 
     91 Status DestroyStack(SqStack &sqstack){
     92     sqstack.top = sqstack.base;
     93     free(sqstack.base);
     94     sqstack.realSize = 0;
     95     sqstack.stackSize = DEFSIZE; 
     96     return OK;
     97 } 
     98  
     99 //得到栈的元素个数 
    100 int StackLength(SqStack &sqstack){
    101     return sqstack.realSize;
    102 } 
    103 
    104 /*******************************主函数************************************************/
    105 int main(int argc, char *argv[]){ 
    106     
    107     SqStack sqstack;
    108     int N = 0;         //用于记录输入栈的个数 
    109     int temp = 0;    //用于临时存栈 
    110     
    111     /****初始化栈***/
    112     InitStack(sqstack);
    113     printf("初始化时,堆的大小为:%d
    ",sqstack.stackSize);
    114     
    115     /**根据输入来填充栈**/
    116     printf("请入你想要输入几个数据进栈:");
    117     scanf("%d",&N) ;
    118     while(N--){
    119         scanf("%d",&temp);
    120         Push(sqstack,temp);
    121         printf("进栈的大小为:%d	",temp);
    122         printf("压栈后,栈的大小为:%d,%d
    ",temp,sqstack.stackSize);
    123         
    124     } 
    125     /**得到栈顶元素**/
    126     GetTop(sqstack,temp);
    127     printf("得到栈顶元素为:%d",temp); 
    128     
    129     /**将栈的元素逐一出栈**/
    130 /*    printf("现在开始逐一出栈:
    ");
    131     while(!IsEmpty(sqstack)){
    132     //如果栈不为空则进行出栈 
    133         Pop(sqstack,temp);
    134         printf("%d 	",temp);
    135     } 
    136     printf("
    栈输出完成!!
    ");
    137 */
    138     
    139     DestroyStack(sqstack);
    140     printf("销毁栈完成!!
    ");
    141     
    142     
    143     
    144     scanf("%d",&temp);
    145     return 0;
    146 }
    人生哪有不疯狂!
  • 相关阅读:
    2021.07.13 学习总结
    2021.07.12 学习总结
    2021.07.11 学习总结
    form表单提交失败
    url传参和解决中文乱码
    layui日期选择无效的问题
    Eclipse中快速生成Javabean的方法
    一种控制台输出效果的方法
    暑期周总结
    环境配置2
  • 原文地址:https://www.cnblogs.com/shuqi/p/3679119.html
Copyright © 2020-2023  润新知