码迷,mamicode.com
首页 > 其他好文 > 详细

栈实现

时间:2017-12-01 23:36:49      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:empty   删除   fine   div   sqs   error   base   实现   std   

  1 #include "stdio.h"
  2 #include "stdlib.h"
  3 
  4 #define OK 1
  5 #define ERROR 0
  6 #define OVERFLOW -2
  7 #define STACKINITSIZE 100
  8 #define STACKINCREASMENT 10
  9 
 10 typedef char elemType;
 11 typedef struct{
 12     elemType *top;
 13     elemType *base;
 14     int stacksize;
 15 }SqStack;
 16 
 17 int InitStack(SqStack *s){
 18     s->base = (elemType *)malloc(STACKINITSIZE*sizeof(elemType));
 19     if(!s->base)
 20         exit(OVERFLOW);
 21     s->top = s->base;
 22     s->stacksize = STACKINITSIZE;
 23 }
 24 
 25 int StackLength(SqStack *s){
 26     int length = 0;
 27     elemType *p = s->base;
 28     while(p != s->top){
 29         p++;
 30         length++;
 31     }
 32     return length;
 33 }
 34 
 35 void GetLength(SqStack *s){
 36     printf("栈的元素个数为:%d\n",s->top - s->base);
 37 }
 38 
 39 int StackTraverse(SqStack *s){
 40     elemType *p = s->top;
 41     printf("该栈自定向下的元素为:\n");
 42     while(p != s->base){
 43         p--;
 44         printf("%c ",*p);
 45     }
 46     printf("\n");
 47     return OK;
 48 }
 49 
 50 int GetTop(SqStack *s){
 51     if(s->top == s->base){
 52         printf("栈为空!\n");
 53         return ERROR;
 54     }
 55     printf("%c",*(s->top-1));
 56     return OK;
 57 }
 58 
 59 int Push(SqStack *s,elemType e){
 60     if(s->top - s->base == s->stacksize){
 61         s->base=(elemType *)realloc(s->base,(s->stacksize+STACKINCREASMENT)*sizeof(elemType));
 62         if(!s->base)
 63             exit(OVERFLOW);
 64         s->top = s->base+STACKINCREASMENT;
 65     }
 66     *(s->top) = e;
 67     s->top++;
 68     return OK;
 69 }
 70 
 71 int Pop(SqStack *s){
 72     if(s->top == s->base){
 73         printf("栈为空!\n");
 74         return ERROR;
 75     }
 76     s->top--;
 77     printf("已删除栈顶元素 %c\n",*(s->top));   //top指向栈顶元素的上方,先top--,再输出值  
 78     return OK;
 79 }
 80 
 81 void StackEmpty(SqStack *s){
 82     if(s->top == s->base)
 83         printf("栈为空!\n");
 84     else
 85         printf("栈不为空!\n");
 86 }
 87 
 88 int ClearStack(SqStack *s){
 89     if(s->base == s->top)
 90         printf("栈已为空");
 91     else{
 92         s->base = s->top;
 93         printf("栈已经清空!\n");
 94     }
 95 }
 96 
 97 void main(){
 98     SqStack *s = (SqStack*)malloc(sizeof(SqStack));  
 99     InitStack(s);  
100     StackEmpty(s);  
101     printf("插入栈顶元素...\n");  
102     Push(s,a);  
103     Push(s,b);  
104     Push(s,c);  
105     Push(s,d);  
106     StackEmpty(s);  
107     StackTraverse(s);  
108     printf("栈的元素个数为%d\n",StackLength(s));  
109     Pop(s);  
110     StackTraverse(s);  
111     GetLength(s);  
112     ClearStack(s);  
113     StackEmpty(s);  
114     printf("栈的元素个数为%d\n",StackLength(s)); 
115     getchar();
116 }

 

栈实现

标签:empty   删除   fine   div   sqs   error   base   实现   std   

原文地址:http://www.cnblogs.com/ixummer/p/7944505.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!