check_stack2.c
资源名称:C数据结构课程设计.rar [点击查看]
上传用户:janny_wxd
上传日期:2010-02-03
资源大小:261k
文件大小:1k
源码类别:
控制台编程
开发平台:
C/C++
- #include<stdio.h>
- //#include<string.h>
- #include<stdlib.h>
- #define OK 1
- #define TRUE 1
- #define FALSE 0
- #define ERROR 0
- #define OVERFLOW -2
- #define STACK_INIF_SIZE 50
- #define STACKINCREMENT 10
- typedef int status;
- typedef struct sqstack{
- int *base;
- int *top;
- int stacksize;
- }sqstack;
- status *initstack(sqstack *s){
- (s)->base = (int*)malloc(STACK_INIF_SIZE*sizeof(int));
- if((s)->base==NULL) exit(OVERFLOW);
- (s)->top=(s)->base;
- (s)->stacksize = STACK_INIF_SIZE;
- printf("initstack is finished!n");
- return OK;
- }
- int gettop(sqstack *s){
- int e;
- if(s->top==s->base){
- printf("NULL1n");
- return 0;
- }
- e=*(s->top-1);
- printf("gettop %d is OK!n",e);
- return e;
- }
- status push(sqstack *s,int e){
- if(s->top-s->base>=s->stacksize){
- s->base=(int*)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(int));
- if(!s->base) exit(OVERFLOW);
- s->stacksize+= STACKINCREMENT;
- }
- *(s->top++)=e;
- printf("push %d is OKn",e);
- return OK;
- }
- status pop(sqstack *s,int *e){
- if(s->top==s->base){
- printf("NULL2n");
- return ERROR;
- }
- *e=*(--(s->top));
- printf("pop %d is OK!n",*e);
- return OK;
- }
- void main(){
- int b,e;
- char a='a',ch;
- sqstack s;
- initstack(&s);
- push(&s,a);
- b=gettop(&s);
- pop(&s,&e);
- printf("b is %dn",b);
- ch=a;
- printf("%cn",ch);
- }