链式栈的实现(头文件及源程序)
2021/7/22 14:09:35
本文主要是介绍链式栈的实现(头文件及源程序),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
链式栈的实现(头文件及源程序)
Linkedstack.h
#ifndef __LINKEDSTACK_H__ #define __LINKEDSTACK_H__ //元素类型定义 typedef int ElemType_stack; //结点结构体类型定义 typedef struct Node_stack { ElemType_stack data; struct Node_stack *next; struct Node_stack *front; }Node_stack; //栈类型定义 typedef struct Linkedstack { int num; Node_stack *top; //栈顶指针 Node_stack *bottom; //栈底指针 }Linkedstack; //创建一个空栈 Linkedstack *Init_stack(); //创建一个新结点 Node_stack *Create_New_Node_stack(ElemType_stack value); //入栈 void push_stack(Linkedstack *linkedstack, ElemType_stack value); //出栈 void pop_stack(Linkedstack *linkedstack); //销毁一个栈 void destroy_stack(Linkedstack *linkedstack); //读取栈顶元素 ElemType_stack get_stack_top(Linkedstack *linkedstack); //判断栈空 int empty_stack(Linkedstack *linkedstack); //获取栈的元素个数 int length_stack(Linkedstack *linkedstack); #endif
Linkedstack.c
#include <stdio.h> #include <stdlib.h> #include "linkedstack.h" //栈初始化 Linkedstack *Init_stack( ) { Linkedstack *linkedstack = malloc(sizeof(Linkedstack)); linkedstack->num = 0; linkedstack->top = NULL; linkedstack->bottom = NULL; return linkedstack; } //创建一个新结点 Node_stack *Create_New_Node_stack(ElemType_stack value) { Node_stack *new_node = malloc(sizeof(Node_stack)); new_node->data = value; new_node->next = NULL; new_node->front = NULL; return new_node; } //入栈 void push_stack(Linkedstack *linkedstack, ElemType_stack value) { if (linkedstack == NULL) { printf("栈不存在!\n"); return; } Node_stack *new_node = Create_New_Node_stack(value); if (linkedstack->num == 0) { linkedstack->top = new_node; linkedstack->bottom = new_node; new_node->front = linkedstack->top; } else { linkedstack->top->next = new_node; new_node->front = linkedstack->top; linkedstack->top = new_node; } linkedstack->num++; } //出栈 void pop_stack(Linkedstack *linkedstack) { if (linkedstack == NULL) { printf("栈不存在!\n"); return; } if (linkedstack->num == 1) { linkedstack->top->front = NULL; free(linkedstack->top); linkedstack->top = NULL; linkedstack->bottom = NULL; } else { linkedstack->top = linkedstack->top->front; free(linkedstack->top->next); linkedstack->top->next = NULL; } linkedstack->num--; } //销毁一个栈 void destroy_stack(Linkedstack *linkedstack) { if (linkedstack == NULL) { printf("栈不存在!\n"); return; } else { while (linkedstack->num != 0) { pop_stack(linkedstack); } //free(linkedstack); linkedstack = NULL; } } //读取栈顶元素 ElemType_stack get_stack_top(Linkedstack *linkedstack) { if (linkedstack == NULL) { printf("栈不存在!\n"); return 0; } if (linkedstack->num == 0) { printf("栈已空,获取栈顶元素失败!"); return 000; } else { return linkedstack->top->data; } } //判断栈空 int empty_stack(Linkedstack *linkedstack) { if (linkedstack == NULL) { printf("栈不存在!\n"); return -1; } if (linkedstack->num == 0) { return 1; //栈空 } else { return 0; //栈非空 } } //获取栈的元素个数 int length_stack(Linkedstack *linkedstack) { if (linkedstack == NULL) { printf("链表不存在!\n"); return 0; } return linkedstack->num; }
这篇关于链式栈的实现(头文件及源程序)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-02springboot项目无法注册到nacos-icode9专业技术文章分享
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)