栈的链式存储的实现与操作
来源:互联网 发布:网页视频剪辑软件 编辑:程序博客网 时间:2024/06/06 10:38
栈的链式存储的实现与操作
栈的链式存储成为链栈。可以采用一个带头结点的单链表来表示一个链栈,其结点结构和链表相同。栈的插入和删除操作都在头结点后进行。
示例代码
#include <stdio.h>#include <stdlib.h>typedef struct Node{ int data; struct Node *next;}StackNode;StackNode* InitLinkStack(void);StackNode* PushLinkStack(StackNode *top, int x);StackNode* PopLinkStack(StackNode *top, int *x);int main(void){ StackNode *top; if((top = InitLinkStack()) == NULL){ printf("链栈初始化失败!\n"); exit(1); } else printf("链栈初始化成功!\n"); int end = 0; int ope; int n; while(!end){ print_hyphen(15); printf("\n"); printf("请输入指令来执行操作\n"); print_hyphen(15); printf("\n"); printf("1、入栈\n2、出栈\n3、退出\n"); print_hyphen(15); printf("\n"); printf("输入要使用的功能的序号: "); scanf("%d", &ope); switch(ope){ case 1: printf("请输入要入栈的元素: "); scanf("%d", &n); PushLinkStack(top, n); break; case 2: if(!PopLinkStack(top, &n)) printf("栈为空!\n"); else printf("出栈的元素为: %d\n", n); break; case 3: printf("再见!\n"); end = 1; break; default: printf("无此序号,请重新输入!\n"); } } return 0;}StackNode* InitLinkStack(void){ StackNode *top = NULL; top = (StackNode*)malloc(sizeof(StackNode)); top->next = NULL; return top;}StackNode* PushLinkStack(StackNode *top, int x){ StackNode *p; p = (StackNode*)malloc(sizeof(StackNode)); p->data = x; p->next = top->next; top->next = p; return top;}StackNode* PopLinkStack(StackNode *top, int *x){ StackNode *p; if(top->next == NULL) return NULL; else{ p = top->next; *x = p->data; top->next = p->next; free(p); return top; }}void print_hyphen(int n){ while(n--) printf("-");}
阅读全文
1 0
- 栈的链式存储的实现与操作
- 栈的链式存储结构与操作
- 栈的链式存储结构与操作
- 链式栈的基本操作与实现
- 集合的链式存储及操作实现
- 队列操作的实现-链式存储
- 队列的链式存储结构与操作
- 队列的链式存储结构与操作
- 栈的链式存储实现
- 栈的链式存储实现
- 栈的链式存储实现
- 栈的链式存储结构操作及实现
- 串的链式存储表示与实现
- 线性表的链式存储与实现
- 栈的顺序存储与链式存储
- 企业级数据结构-栈的链式存储设计与实现
- 【线性表】顺序存储、链式存储的实现及操作
- 栈的链式存储及常用操作
- 个人博客索引贴2
- 前端学习指南
- robot framework-requests库安装过程问题解决
- ES6——块级作用域(let)
- 抛物线动效
- 栈的链式存储的实现与操作
- 贪心算法--邮局选址,输油管道问题
- nginx+tomcat部署笔记
- Go语言解析YAML
- 模板模式-抽象模板
- tensorflow相关函数及其功能(留着自己备忘)
- Unity回调函数
- Log4J学习记录
- LeetCode530. Minimum Absolute Difference in BST