堆栈的顺序存储结构的简单实现

来源:互联网 发布:excel随机数据生成 编辑:程序博客网 时间:2024/05/17 23:05
<span style="font-size:18px;">#include<iostream>using namespace std;//定义一个大小是100的栈typedef int elemtype;//栈的数据结构typedef struct{elemtype *base;elemtype *top; int size;} sqstack;//初始化堆栈void InitStack(sqstack &S,int size){S.base=(elemtype *)malloc(S.size*sizeof(elemtype));S.top=S.base;S.size=size;}void push(sqstack &s,int e){s.top++;*(s.top)=e;}int pop(sqstack &s){elemtype e;e=*(s.top);s.top--;return e;}int main(){sqstack Stack;cout<<"请输入堆栈的大小:";int size;cin>>size;//初始化堆栈InitStack( Stack,size);cout<<"push data:";int data;while(cin>>data){   push(Stack,data);}//出栈while(Stack.base!=Stack.top){ elemtype data=pop(Stack);  cout<<data<<" ";}return 0;}</span>

0 0