当前位置:网站首页>链栈
链栈
2022-07-23 05:42:00 【你看那是一个舔狗】
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef int T;
typedef struct StackNode
{
T data;//数据
StackNode* next;//指向下一结点
}*LinkStackNode;
typedef struct
{
LinkStackNode top;//栈顶
int count;//计数器
}*LinkStack;
bool InitStack(LinkStack s)//初始化
{
if (!s)
return false;
s->top = (LinkStackNode)malloc(sizeof(StackNode));
s->top = NULL;
s->count = 0;
return true;
}
bool Is_Empty(LinkStack s)//判空
{
return s->count == 0;
}
bool ClearStack(LinkStack s)//清栈
{
LinkStackNode p, q;
p = s->top;
while (p)
{
q = p;
p = p->next;
free(q);
}
s->count = 0;
return true;
}
int GetLength(LinkStack s)//栈的数据个数
{
return s->count;
}
T GetTop(LinkStack s)//获取栈顶元素
{
if (s->top==NULL)
return -1;
return s->top->data;
}
bool Push(LinkStack s,T val)//入栈
{
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)//出栈
{
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)//遍历
{
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;
}
边栏推荐
- 數倉4.0筆記——業務數據采集
- notepad++背景颜色调整选项中文释义
- Development of digital collection system / introduction of digital collection scheme
- [flick]flick on yarn's flick conf simplest configuration
- 1、MySQL初体验
- The difference between slice() and slice()
- [literature research] search PubMed for papers in journals with specific impact factors
- [doris] configure and basically use the contents system (continue to add content when you have time)
- 1. Initial experience of MySQL
- MySQL事务
猜你喜欢

Activiti7 Quick Start experience sharing

activiti7快速入门经验分享

3. DQL (data query statement)

Kubesphere haproxy+kept (I)

Image fuzzy processing batch production fuzzy data set

Data warehouse 4.0 notes - business data collection - sqoop

uni原生插件开发--友盟一键登录

NFT trading platform digital collection system | development and customization

Man in the middle attacks ARP Spoofing and its linkage with beef XSS

數倉4.0筆記——用戶行為數據采集四
随机推荐
数仓4.0笔记——业务数据采集——Sqoop
Project instances used by activiti workflow
数仓4.0笔记——业务数据采集
Sqli lab pass 17-22 notes
Cuda10.0 configuration pytorch1.7.0+monai0.9.0
[untitled]
用户连续登陆(允许中断)查询sql
规范数据库设计
Development of digital collection system / introduction of digital collection scheme
Unable to negotiate with port 51732: no matching host key type found. Their offer:
MySQL invalid conn troubleshooting
MySQL数据库
Man in the middle attacks ARP Spoofing and its linkage with beef XSS
Yarn capacity scheduler settings
數倉4.0筆記——業務數據采集
数仓4.0笔记——用户行为数据采集二
11、多线程
MySQL卸载
Using dynamic programming to solve the longest growing subsequence problem
Unable to negotiate with port 51732: no matching host key type found. Their offer: