#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
//定义链栈
typedef struct LNode {
int data;
struct LNode *next;
} LNode , *LiStack;
//初始化一个带头结点的链栈
bool InitStack(LiStack &S)
{
S = (LNode*)malloc(sizeof(LNode));
if(S==NULL)
return false;
S->next = NULL;
return true;
}
//进栈
LNode *Push(LiStack &S)
{
int x,z;
printf("请输入进栈的元素个数:");
scanf("%d",&x);
LNode *p;
LNode *g;
g = S;
for(int i=0; i<x; i++)
{
printf("请输入您要进栈的第%d个元素:",i+1);
scanf("%d",&z);
p = (LNode*)malloc(sizeof(LNode));
p->data = z;
p->next = g->next;
g->next = p;
}
return S;
}
//打印栈
void PrintStack(LiStack S)
{
LNode *p;
p = S->next;
while(p!=NULL)
{
printf("%3d",p->data);
p = p->next;
}
printf("\n");
}
//出栈
bool Pop(LiStack &S,int *x)
{
if (S->next == NULL)
retu
链栈(C语言)
最新推荐文章于 2023-03-22 19:59:02 发布
本文详细介绍了如何使用C语言来实现链栈的数据结构,包括链栈的创建、入栈、出栈和查看栈顶元素等基本操作。通过实例代码解析,帮助读者深入理解链栈的工作原理。
摘要由CSDN通过智能技术生成