/*
*栈的顺序存储结构基本实现
*/
#include<iostream>
using namespace std;
#define max 100
typedef char elemtype;
typedef struct
{
elemtype data[max];
int top;
}stack;
void gettop(stack*&s)//取栈顶元素
{
if(s->top==-1)
cout<<"栈空!"<<endl;
char e=s->data[s->top];
cout<<"栈顶元素为:"<<"e"<<endl;
}
void pop(stack*&s)//出栈
{
int i=0;
while(s->top!=-1)
{
char e=s->data[s->top];
s->top--;
i++;
cout<<"出栈元素:"<<e<<"序号为:"<<i<<endl;
}
}
void push(stack*&s,elemtype e)//进栈
{
if(s->top==max-1)
cout<<"栈已满!"<<endl;
s->top++;
s->data[s->top]=e;
cout<<"元素"<<e<<"入栈成功!"<<endl;
}
void empty(stack*s)//判断栈是否为空
{
if(s->top==-1)
cout<<"栈空!"<<endl;
else
cout<<"非空!"<<endl;
}
void destroy(stack*&s)//销毁栈
{
free(s);
}
void init(stack*&s)//初始化栈
{
s=(stack*)malloc(sizeof(stack));
s->top=-1;
}
int main()
{
stack*s;
init(s);
empty(s);
push(s,'a');
push(s,'b');
push(s,'c');
push(s,'d');
push(s,'e');
empty(s);
gettop(s);
pop(s);
empty(s);
destroy(s);
return 0;
}
数据结构-栈的顺序存储
最新推荐文章于 2021-11-25 22:44:27 发布