41深入理解C指针之---指针与栈

  一、借助第40指针与链表的相关内容,稍微修改即可:

   1、定义头文件stack.h代码如下:

复制代码
 1 #include <stdlib.h>
 2 #include <stdio.h>
 3
 4 #ifndef stack_h
 5 #define stack_h
 6 typedef int DataType;
 7
 8 typedef struct _node{
 9     DataType data;
10     struct _node *next;
11 } Node;
12
13 typedef struct _stack{
14     Node *head;
15     Node *tail;
16 } Stack;
17
18 void initStack(Stack *);
19 void pushStack(Stack *, DataType);
20 void popStack(Stack *);
21 int getLength(Stack *);
22 void dispStack(Stack *);
23
24 #endif
复制代码

  头文件中依旧是完成数据类型的声明和数据的操作函数的声明。

   2、头文件对应的实现文件stack.c代码如下:

复制代码
 1 #include "stack.h"
 2
 3 //栈初始化
 4 void initStack(Stack *stack){
 5     stack->head = NULL;
 6     stack->tail = NULL;
 7 }
 8
 9 //栈入
10 void pushStack(Stack *stack, DataType iData){
11     Node *node = (Node *)malloc(sizeof(Node));
12     node->data = iData;
13     node->next = NULL;
14
15     if(stack->head == NULL){
16         stack->tail = node;
17     }else{
18         node->next = stack->head;
19     }
20     stack->head = node;
21
22     return;
23
24 }
25
26 //栈出
27 void popStack(Stack *stack){
28     if(stack->head->next == NULL){
29         stack->head = NULL;;
30     }else{
31         stack->head = stack->head->next;
32     }
33
34     return;
35 }
36
37 //栈长度
38 int getLength(Stack *stack){
39     Node *node = stack->head;
40     int i = 0;
41     while(node != NULL){
42         node = node->next;
43         i++;
44     }
45
46     return i;
47 }
48
49 //栈输出
50 void dispStack(Stack *stack){
51     Node *node = stack->head;
52     int i = 0;
53     while(node != NULL){
54         printf("the %dth node: %d\n", i + 1, node->data);
55         node = node->next;
56         i++;
57     }
58     printf("disp finished!\n");
59
60     return;
61 }
复制代码

  代码说明:

    1、入栈函数使用链表的头插法

    2、出栈函数直接将链表的头节点删除即可实现出栈功能

   3、stack.c对应的测试文件testStack.c代码如下:

复制代码
 1 #include "stack.h"
 2
 3 int main(int argc, char **argv)
 4 {
 5     Stack *stack1 = (Stack *)malloc(sizeof(Stack));
 6     printf("the first:\n");
 7     initStack(stack1);
 8     pushStack(stack1, 1);
 9     pushStack(stack1, 3);
10     pushStack(stack1, 5);
11     pushStack(stack1, 7);
12     pushStack(stack1, 9);
13     printf("The length: %d\n", getLength(stack1));
14     dispStack(stack1);
15     printf("the second:\n");
16     popStack(stack1);
17     printf("The length: %d\n", getLength(stack1));
18     dispStack(stack1);
19     popStack(stack1);
20     dispStack(stack1);
21     printf("The length: %d\n", getLength(stack1));
22     pushStack(stack1, 11);
23     dispStack(stack1);
24     printf("The length: %d\n", getLength(stack1));
25
26     return 0;
27 }
复制代码

  测试函数完成所有函数的功能测试,功能通过就OK!

posted @   叕叒双又  阅读(556)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示