栈的简单基本操作(数据结构)
2012-11-01 15:36
477 查看
#include<stdio.h> #include<stdlib.h> #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 #define ERROR 0 #define OK 1 #define OVERFLOW -1 typedef char SElemType; typedef struct{ SElemType *base; SElemType *top; int stacksize; }SqStack; int InitStack(SqStack *S){ S->base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType)); if(!S->base) exit(OVERFLOW); S->top=S->base; S->stacksize=STACK_INIT_SIZE; return OK; } int GetTop(SqStack *S){ char e; if(S->top==S->base) return ERROR; e=*(S->top-1); printf("the top element is %c\n",e); return OK; } int Push(SqStack *S,SElemType e){ if(S->top-S->base>=S->stacksize){ S->base=(SElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType)); if(!S->base) exit(OVERFLOW); S->top=S->base+S->stacksize; S->stacksize+=STACKINCREMENT; } *S->top++=e; return OK; } SElemType Pop(SqStack *S){ SElemType e; if(S->top==S->base) printf("ERROR!!!"); e=*--S->top; return e; } void main(){ SqStack S; char ch[5]; int i; SElemType e; if(InitStack(&S)==1) printf("creat stack succeed!\n"); else printf("creat stack failed!\n"); for(i=0;i<5;i++){ printf("input the %d element\n",i); scanf("%c",&ch[i]); getchar(); } for(i=0;i<5;i++) Push(&S,ch[i]); if(GetTop(&S)==1) printf("get stack top succeed!\n"); else printf("get stack top failed!\n"); e=Pop(&S); printf("the pop element is %c\n",e); e=Pop(&S); printf("the top element is %c\n",e); }
栈的简单基本操作
相关文章推荐
- 【数据结构】二叉树的简单遍历及基本操作
- 【数据结构和算法】用java简单的实现单链表的基本操作
- asp.net MVC + linq to Entity简单教程(三)linq to Entity的基本操作
- STL中常用数据结构基本操作
- 【C++/数据结构】单链表的基本操作
- oracle 基本操作练习——简单的sql语句
- 【数据结构上机练习】5.栈的简单操作(2)
- C语言(数据结构) - 双向链表的基本操作
- PTA 数据结构 6-2 顺序表基本操作
- 数据结构之链表与数组(三)-单向链表上的简单操作
- python数据结构之链表——基本操作实现
- 数据结构查找之-单链表的基本操作
- 重学数据结构001――链表基本操作与一元多项式相加
- 数据结构-链表的基本操作
- 【数据结构】第2周 栈与队列 2:栈的基本操作
- 数据结构JAVA图的基本操作
- 三维CAD塑造——基于所述基本数据结构一半欧拉操作模型
- 数据结构链表的简单操作
- 基本数据结构学习总结: 二叉树的基本操作
- 简单了解JavaScript操作XPath的一些基本方法