我是靠谱客的博主 凶狠花卷,这篇文章主要介绍C语言实现链栈的步骤,现在分享给大家,希望可以做个参考。

链栈图解

链栈的常规操作

复制代码
1
2
3
4
5
6
7
8
9
10
/********************* 链栈的常规操作 ****************************/ LinkStack InitLinkStack(); // 初始化链栈 int StackEmpty(); // 判断链栈空 int StackLength(); // 求链栈长(链栈元素个数) int Push(); // 入栈 压栈 ElemType Pop(); // 出栈 弹栈 void DestroyStack(); // 销毁链栈 /***************************************************************/

定义链栈结构体

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "stdio.h" #include "malloc.h" #define TRUE 1 #define FALSE 0 typedef int ElemType; // 链栈存储元素的数据类型 /* * 定义链栈结构体 */ typedef struct Node{ ElemType data; // 栈结点数据域 struct Node *next; // 栈结点指针域 }*LinkStack, Node;

初始化链栈

复制代码
1
2
3
4
5
6
// 初始化链栈(带头结点的链栈) LinkStack InitLinkStack(){ LinkStack s = (LinkStack)malloc(sizeof(struct Node)); s -> next = NULL; return s; }

链栈判空

复制代码
1
2
3
4
5
6
7
8
9
10
/* * 判断链栈是否空 * s 链栈 */ int StackEmpty(LinkStack s){ if(s == NULL){ return FALSE; } return s -> next == NULL; }

因为是链式存储结构,无需链栈判满。

计算链栈的长度

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* * 求链栈长度(栈中元素个数) * s 链栈 */ int StackLength(LinkStack s){ LinkStack p; int len = 0; if(StackEmpty(s)){ return FALSE; } p = s -> next; // 带头结点的链栈要先移动一下 while(p != NULL){ len ++; p = p -> next; } return len; }

链栈入栈(Push)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* * 入栈 压栈 * s 链栈 * data 入栈数据 */ int Push(LinkStack s, ElemType data){ // 分配入栈结点 Node *new_node = (Node *)malloc(sizeof(struct Node)); if (new_node == NULL) return FALSE; // 结点分配失败 // 跟单链表一样使用头插法 new_node -> data = data; new_node -> next = s -> next; s -> next = new_node; return TRUE; }

链栈出栈(Pop)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* * 出栈 弹栈 * s 链栈 */ ElemType Pop(LinkStack s){ LinkStack top; ElemType data; // 判栈空 if(StackEmpty(s)){ return FALSE; } top = s -> next; // 访问栈顶结点 data = top -> data; // 取出栈顶元素 s -> next = top -> next; free(top); // 释放栈顶空间 return data; }

链栈各操作测试

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// 程序主入口 int main(int argc, char const *argv[]) { LinkStack s = InitLinkStack(); printf("StackEmpty():%dn", StackEmpty(s)); printf("StackLength():%dnn", StackLength(s)); // 入栈元素 ElemType datas[] = {1, 3, 5, 7, 9}; // 动态计算入栈元素个数 int len = sizeof(datas) / sizeof(datas[0]); // for循环依次入栈 printf("Push():"); for(int i = 0; i < len; i++){ printf("%dt", datas[i]); Push(s, datas[i]); } printf("nStackEmpty():%dn", StackEmpty(s)); printf("StackLength():%dnn", StackLength(s)); // 出栈 弹栈 printf("Pop(): "); while(!StackEmpty(s)){ printf("%dt", Pop(s)); } printf("nStackEmpty():%dn", StackEmpty(s)); printf("StackLength():%dnn", StackLength(s)); return 0; }

结果如下:

复制代码
1
2
3
4
5
6
7
8
9
10
StackEmpty():1 StackLength():0 Push():1 3 5 7 9 StackEmpty():0 StackLength():5 Pop(): 9 7 5 3 1 StackEmpty():1 StackLength():0

源代码

源代码已上传到 GitHub Data-Structure-of-C,欢迎大家来访。

以上就是C语言实现链栈的步骤的详细内容,更多关于C语言实现链栈的资料请关注靠谱客其它相关文章!

最后

以上就是凶狠花卷最近收集整理的关于C语言实现链栈的步骤的全部内容,更多相关C语言实现链栈内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(116)

评论列表共有 0 条评论

立即
投稿
返回
顶部