当前位置:网站首页>Chain stack
Chain stack
2022-07-23 11:59:00 【Look, that's a licking dog】
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef int T;
typedef struct StackNode
{
T data;// data
StackNode* next;// Point to the next node
}*LinkStackNode;
typedef struct
{
LinkStackNode top;// To the top of the stack
int count;// Counter
}*LinkStack;
bool InitStack(LinkStack s)// initialization
{
if (!s)
return false;
s->top = (LinkStackNode)malloc(sizeof(StackNode));
s->top = NULL;
s->count = 0;
return true;
}
bool Is_Empty(LinkStack s)// Sentenced to empty
{
return s->count == 0;
}
bool ClearStack(LinkStack s)// Clear the stack
{
LinkStackNode p, q;
p = s->top;
while (p)
{
q = p;
p = p->next;
free(q);
}
s->count = 0;
return true;
}
int GetLength(LinkStack s)// Number of stack data
{
return s->count;
}
T GetTop(LinkStack s)// Get stack top element
{
if (s->top==NULL)
return -1;
return s->top->data;
}
bool Push(LinkStack s,T val)// Push
{
if (!s)
return NULL;
LinkStackNode p = (LinkStackNode)malloc(sizeof(LinkStackNode));
p->data = val;
p->next = s->top;
s->top = p;
s->count++;
return true;
}
T Pop(LinkStack s)// Out of the stack
{
if (Is_Empty(s))
return -1;
T tmp = s->top->data;
LinkStackNode p = s->top;
s->top = s->top->next;
//free(p);
s->count--;
return tmp;
}
void Show(LinkStack s)// Traverse
{
if (!s)
return;
LinkStackNode q = s->top;
while (q)
{
printf("%d ", q->data);
q = q->next;
}
printf("\n");
}
int main()
{
LinkStack s = (LinkStack)malloc(sizeof(LinkStack));
InitStack(s);
for (int i = 1; i <= 5; i++)
{
Push(s, i);
}
printf("Len: %d\n", GetLength(s));
Show(s);
printf("%d\n", GetTop(s));
printf("%d\n", Pop(s));
Show(s);
return 0;
}
边栏推荐
- 8、 Collection framework and generics
- ninja介绍及使用
- 链队列
- paddle.nn.BCELoss的使用问题
- Upload pictures to qiniu cloud through the web call interface
- Activiti7 Quick Start experience sharing
- MySQL modify function permission is not effective
- Wordcount of the first Flink program
- 1. Know the database
- Understanding of the decoder in the transformer in NLP
猜你喜欢

數倉4.0筆記——業務數據采集
![[metric] use Prometheus to monitor flink1.13org.apache.flink.metrics](/img/9a/f6ef8de9943ec8e716388ae6620600.png)
[metric] use Prometheus to monitor flink1.13org.apache.flink.metrics

2. MySQL data management - DML (add, modify, delete data)

八、集合框架和泛型

高德定位---权限弹框不出现的问题

Lecturer solicitation order | Apache dolphin scheduler meetup sharing guests, looking forward to your topic and voice!

Data warehouse 4.0 notes - business data collection

Data warehouse 4.0 notes - data warehouse environment construction - Yan configuration

Data warehouse 4.0 notes - Data Warehouse Modeling

MySQL index
随机推荐
循环队列
Yarn capacity scheduler settings
二叉树
Quartz2.2 simple scheduling job
MySQL卸载
ninja介绍及使用
Object类
Scala II process control
[metric] use Prometheus to monitor flink1.13org.apache.flink.metrics
8、 Collection framework and generics
2、MySQL数据管理--DML(添加、修改、删除数据)
Ffmpeg audio coding
Print right angle triangle, isosceles triangle, diamond
数仓4.0笔记——用户行为数据采集四
1. Initial experience of MySQL
ChaosLibrary·UE4开坑笔记
规范数据库设计
MySQL事务
Kubesphere haproxy+kept (I)
[radiology] bugfix: when GLCM features: indexerror: arrays used as indexes must be of integer (or Boolean) type