#include <iostream>
#include <windows.h>
using namespace std;
typedef struct zhan
{
int data;
struct zhan *next;
}zhan;
typedef struct
{
struct zhan *first;
struct zhan *last;
}pointer;
//构造一个空栈
void Creat_zhan(pointer &Z)
{
Z.first = Z.last = (zhan *)malloc(sizeof(zhan));
if (Z.first == NULL) exit(0);
Z.first->next = NULL;
}
//进栈
void Push(pointer &Z, int e)
{
zhan *p = Z.first;
p->data = e;
Z.first = (zhan *)malloc(sizeof(zhan));
if (Z.first == NULL) exit(0);
Z.first->next = p;
}
//出栈
void Pop(pointer &Z, int &e)
{
zhan *q = Z.first;
e = (q->next)->data;
Z.first = Z.first->next;
free(q);
}
//栈空判断
int zhan_emp(pointer &Z)
{
if (Z.first == Z.last) return 0;
else return 1;
}
//主函数
int main()
{
int e;
pointer Z;
Creat_zhan(Z);
for (int i = 0; i < 5; i++)
{
cout << "Please iput data to push:";
cin >> e;
Push(Z, e);
}
cout << endl;
cout << "The data pop:";
while (zhan_emp(Z))
{
Pop(Z, e);
cout << e << " ";
}
system("pause");
return 0;
}
链栈
最新推荐文章于 2024-05-03 13:48:12 发布