当前位置:网站首页>Linked list implementation of C language stack
Linked list implementation of C language stack
2022-07-18 06:55:00 【Meatballs w】
Self use
#include <stdio.h>
#include <stdlib.h>
#define DataType int // Data field of stack node , This is an integer
#define bool int
#define ERROR 0
typedef struct node
{
// Define linked list nodes Data field and pointer field
DataType data;
struct node *next;
} *StackNodePtr, StackNode;
typedef struct stack
{
StackNodePtr top; // top Pointer to the top of the stack , Empty stack top==NULL,
int size; // Set a size It can reduce the time complexity of calculating the length of the chain stack
} LinkStack;
LinkStack *InitStack();
void StackPushStack(LinkStack *p, DataType dt);
bool StackPopStack(LinkStack *p);
void StackClear(LinkStack *p);
DataType StackGetTop(LinkStack *p);
int StackGetSize(LinkStack *p);
int StackIsEmpty(LinkStack *p);
void PrintStack(LinkStack *p);
int main()
{
DataType e;
LinkStack *p = InitStack();
printf(" Please enter an integer ( Input -1 end ):");
scanf("%d", &e);
while (e != -1)
{
StackPushStack(p, e);
printf(" Please enter an integer ( Input -1 end ):");
scanf("%d", &e);
}
PrintStack(p);
printf(" The stack size is %d\n", StackGetSize(p));
printf(" Out of stack testing :");
StackPopStack(p);
PrintStack(p);
printf("\n");
printf(" Stack top test :");
e = StackGetTop(p);
printf(" The top of the stack taken out is %d\n", e);
}
// Initialization stack
LinkStack *InitStack()
{
LinkStack *p = (LinkStack *)malloc(sizeof(LinkStack));
p->top = NULL;
p->size = 0;
return p;
}
// Push The chain stack does not need to judge whether it is full The insertion method is similar to
void StackPushStack(LinkStack *p, DataType dt)
{
StackNodePtr temp; //temp Used to generate new nodes
temp = (StackNodePtr)malloc(sizeof(StackNode)); // Randomly allocated space
temp->next = p->top; // Modify the insertion node pointer field , Point to the original stack top element ( After inserting, it becomes a dick )
temp->data = dt; // Modify the insert node data field to dt
p->top = temp; // top Point to the insertion node
++p->size; // size+1
}
// Out of the stack Check whether the stack is empty
bool StackPopStack(LinkStack *p)
{
if (p->size == 0)
return ERROR; // Stack empty return error
StackNodePtr temp = p->top; // Save the stack top pointer to temp
p->top = temp->next; // Let the pointer at the top of the stack point to the second node
free(temp); // Release the previous stack top pointer
--p->size; // size-1
return 1;
}
// Empty stack
void StackClear(LinkStack *p)
{
while (!StackIsEmpty(p))
{
// As long as the stack is not empty, it will always be out of the stack
StackPopStack(p);
}
p->top = NULL; // Change the pointer at the top of the stack to null
}
// Get stack top element
DataType StackGetTop(LinkStack *p)
{
return p->top->data; // p->top Pointer to the top of the stack , Point to the top node ,-> Is the node data field
}
// Get stack size
int StackGetSize(LinkStack *p)
{
return p->size;
}
// Judge whether the stack is empty
int StackIsEmpty(LinkStack *p)
{
return !StackGetSize(p);
}
// Print stack
void PrintStack(LinkStack *p)
{
StackNodePtr temp = p->top;
printf(" To the top of the stack :");
while (temp)
{
printf("%d->", temp->data);
temp = temp->next;
}
printf("NULL");
}
边栏推荐
- Type-C application OTG function while charging and transferring data (ldr6028s)
- Redis介绍和安装
- YOLOv3训练数据处理解析
- CCTV hosts are using this wireless Lavalier microphone with fire
- 数据库笔记整理
- How to break the magic spell with a good hand in the strong B & B industry?
- TCP 三次握手、四次挥手图解
- FFmpeg 简介
- Reading notes: review of process consulting I II III
- flex&bison 高级计算器
猜你喜欢
随机推荐
H264编码器的一种率失真优化(1)
MySQL数据库安装&问题
rhcsa笔记二
示波器使用概念记录
【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(四)
H264-解码顺序 显示顺序 参考顺序
flex&bison 高级计算器
Unet++ 网络tensorflow版(slim实现)
CDQ分治与整体二分 学习笔记
读书笔记:《过程咨询 I II III》 回顾
FFmpeg 音视频解封装
How to break the magic spell with a good hand in the strong B & B industry?
PD-QC-AFC多协议诱骗芯片《LDR6328S》
直播神器领夹式无线麦克风-LDR6028助力充电OTG方案
YOLACT结构图
TyPora更换文字颜色和换行
sensor 原理文章
MP4文件简介
PD QC诱骗取电应用IC《乐得瑞LDR6328S》广泛应用于各大小家电
PD QC leurre application IC ledry ldr6328s est largement utilisé dans les appareils ménagers de toutes tailles









