顺序栈的基本操作实现
2012-03-23 10:37
381 查看
#include<stdlib.h> #include<iostream> using namespace std; #define StackSize 100 //顺序栈的初始分配空间 typedef struct { char data[StackSize]; //保存栈中元素 int top; //栈指针 }SqStack; void InitStack(SqStack *st) //*st为引用型参数 { st->top=-1; } int push(SqStack *st,char x) //进栈运算 { if(st->top==StackSize-1) //栈满 return 0; else { st->top++; //栈不满 st->data [st->top]=x; return 1; } } int pop(SqStack *st,char *x) //出栈运算,*st,*x都为引用型参数 { if(st->top=-1) //栈空 return 0; else { *x=st->data[st->top]; //栈不空 st->top--; return 1; } } int gettop(SqStack *st,char *x) //取栈顶元素,x为引用型参数 { if(st->top==-1) //栈空 return 0; else { *x=st->data[st->top]; return 1; } } int getall(SqStack *st,char *x) { if(st->top==-1) return 0; else { while(st->top!=-1) { *x=st->data[st->top]; cout<<*x; st->top--; } } } int StackEmpty(SqStack *st) //判断栈是否为空 { if(st->top==-1) return 1; else return 0; } void main() { SqStack st; char e; InitStack(&st); cout<<"栈"; if(StackEmpty(&st)==1) cout<<"空"<<endl; else cout<<"不空"<<endl; cout<<"a 进栈"<<endl;push(&st,'a'); cout<<"b 进栈"<<endl;push(&st,'b'); cout<<"c 进栈"<<endl;push(&st,'c'); cout<<"d 进栈"<<endl;push(&st,'d'); cout<<"e 进栈"<<endl;push(&st,'e'); cout<<"栈"; if(StackEmpty(&st)==1) cout<<"空"<<endl; else cout<<"不空"<<endl; gettop(&st,&e); cout<<"栈顶元素"<<endl; cout<<"出栈次序"; getall(&st,&e); while(!StackEmpty(&st)) { pop(&st,&e); cout<<e; } cout<<"\n"; }
相关文章推荐
- 利用顺序结构实现线性表的基本操作
- 顺序线性表的基本操作(C语言实现)
- 顺序队列及链队列的基本操作实现
- 顺序表的基本操作实现
- 实现基于静态数组的顺序表的以下基本操作(进阶部分)
- 静态数组顺序表的基本操作实现
- 顺序队列的基本操作实现c++
- C语言实现顺序表的插入、删除、查找、遍历等基本操作
- 实现一个栈(元素遵守先入后出顺序),能够通过 min 方法在 O(1)时间内获取栈中的最小元素。同时,栈的基本操作:入栈(Push)、出栈(Pop),也是在O(1)时间内完成的
- c语言实现顺序表的基本操作
- C语言实现----顺序表的基本操作(1)
- 数据结构-顺序线性表基本操作实现
- 顺序表----12个基本操作实现
- 顺序表实现栈的基本操作
- 1044 顺序栈基本操作的实现
- 顺序队列(循环队列)基本操作实现 C语言
- 数据结构笔记(一)线性表的顺序表示和基本操作及其顺序表实现的集合运算(A-B)U(B-A)实例
- 顺序表类的实现和基本操作
- 顺序栈的基本操作实现
- 顺序栈的基本操作实现