#include<iostream>
using namespace std;
typedef struct Node{
int data;
struct Node *next;
}LinkstackNode, *Linkstack;
void Init(LinkstackNode *S)
{
S = new LinkstackNode;
S->next = NULL;
}
bool Push(Linkstack S, int x)
{//头插法
LinkstackNode *t;
t = new LinkstackNode;
if(t == NULL)
return false;
t->data = x;
t->next = S->next;
S->next = t;
return true;
}
bool Pop(Linkstack S, int *x)
{
LinkstackNode *t;
t = S->next;//指向栈顶元素
if(t == NULL)//栈为空
return false;
S->next = t->next;//跳过一个栈中元素
*x = t->data;
free(t);
return true;
}
void outs(LinkstackNode S)
{
int x;
while(S.next != NULL)
{
Pop(&S, &x);
cout << x <<" ";
}
}
int main()
{
LinkstackNode S;
Init(&S);
int a;
cin >> a;
while(a != -1)
{
Push(&S,a);
cin >> a;
}
outs(S);
return 0;
}
链栈的基本操作
最新推荐文章于 2024-11-26 21:30:34 发布
1649

被折叠的 条评论
为什么被折叠?



