#define _CRT_SECURE_NO_WARNINGS//必须要放在第一行
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct Linknode {
ElemType data;
struct Linknode* next;
}*LiStack;
//头插法实现插入(带头)
LiStack CreatList1(LiStack& S) {
Linknode* s;
int x;
S = (LiStack)malloc(sizeof(Linknode)); //带头结点的链表
S->next = NULL; //L->data里边没放东西
scanf("%d", &x); //从标准输入读取数据
while (x != 9999) {
s = (Linknode*)malloc(sizeof(Linknode)); //申请一个新空间给s,强制类型转换
s->data = x;
s->next = S->next; //让新结点的next指针指向链表的第一个元素
S->next = s;
scanf("%d", &x); //标准输入读取
}
return S; //可以不写
}
//头部删除法
bool DeleteList(LiStack&S,ElemType &x){
if (S->next == NULL)
return false;
x = S->next->data;
S->next = S->next->next;
return true;
}
//读栈顶元素
bool GetTop(LiStack S, ElemType& x) {
if (S->next==NULL)
return false;
x = S->next->data;
return true;
}
int main() {
LiStack S;
ElemType x;
bool TF;
CreatList1(S);
if (TF = GetTop(S, x))
printf("栈顶元素的值为%d\n", x);
if (TF=DeleteList(S,x))
printf("弹栈的值为%d\n",x);
}
链栈---增删查
最新推荐文章于 2022-08-01 11:36:55 发布