您的位置:首页 > 理论基础 > 数据结构算法

数据结构 链栈的基本操作

2016-12-13 21:16 459 查看
链栈

#include <iostream>
#include<cstring>
#include <cstdlib>
using namespace std;
typedef struct selemtype{
selemtype *next;
int data;
}selemtype;

selemtype *initstack()
{
selemtype *top;
top = (selemtype *)malloc(sizeof(selemtype));
top->next=NULL;
return top;
}

selemtype *push(selemtype *top)
{
int e;
selemtype *p;
while(1){
p = (selemtype *)malloc(sizeof(selemtype));
cin >> e;
if(e!=1001)
{
top->data = e;
p->next = top;
top = p;}
else
break;
}
return top;
}

int gettop(selemtype *s)
{
s=s->next;
while(s!=NULL)
{
cout << s->data << " ";
s = s->next;
}
cout << endl;
return 0;
}

selemtype *pop(selemtype *s, selemtype *top)
{
if (s != top)
top = top->next;
else cout<<"该栈为空栈"<<endl;
return top;
}

int main()
{
selemtype *base,*top,*s;
top = initstack();
cout << "首先进行入栈操作:"<<endl;
base = top;
s=push(top);
s=pop(base, s);
cout << "若该栈不为空,弹出栈顶元素:" << endl;
cout<<s->data<<endl;
cout << "若该栈不为空,删除栈顶元素后弹栈:" << endl;
gettop(s);
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: