#include <bits/stdc++.h>
using namespace std;
typedef struct wjb
{
int data;
struct wjb *next;
} wjb, *Linkstack;
/*栈的初始化*/
Linkstack Initstack(Linkstack s)
{
s = NULL; //栈顶指向NULL
return s;
}
/*压栈*/
Linkstack Push(Linkstack s, int e)
{
wjb *p; //新结点
p = new wjb;
p->data = e; //新结点赋值
p->next = s;
s = p;
cout << s->data << "入栈" << endl;
return s;
}
/*出栈*/
Linkstack Pop(Linkstack s)
{
Linkstack p;
if (s == NULL)
{
return 0;
}
cout << s->data << "出栈" << endl;
p = s;
s = s->next;
delete p;
return s;
}
/*获取栈顶元素*/
int getTop(Linkstack s)
{
if (s != NULL)
{
return s->data;
}
}
/*输出栈中所有元素*/
void PrintAll(Linkstack s)
{
if (s == NULL)
{
cout << "栈为空!" << endl;
return;
}
else
{
cout << "顶端[";
while (s != NULL)
{
cout << "-" << s->data;
s = s->next;
}
cout << "]底端\n";
}
}
/*主函数*/
int main()
{
Linkstack s;
s = Initstack(s);
s = Push(s, 3);
s = Push(s, 4);
s = Push(s, 5);
PrintAll(s);
s = Pop(s);
s = Pop(s);
PrintAll(s);
return 0;
}
数据结构-链栈
最新推荐文章于 2024-02-08 16:53:30 发布