数据结构和算法--栈的链式实现
来源:互联网 发布:王者荣耀网络不稳定 编辑:程序博客网 时间:2024/05/21 09:50
//链式栈的实现(类似与单链表)#include <stdio.h>#include <stdlib.h>//定义数据类型typedef int datatype_t;//定义结构体typedef struct node{datatype_t data;struct node *next;}linkstack_t;//创建一个空的栈linkstack_t *linkstack_create(){linkstack_t *s = (linkstack_t *)malloc(sizeof(linkstack_t));s->next = NULL;return s;}//判断栈是否为空int linkstack_empty(linkstack_t *s){return s->next == NULL ? 1 : 0;}//入栈(压栈)(单链表的头插法)int linkstack_push(linkstack_t *s, datatype_t value){linkstack_t *temp = (linkstack_t *)malloc(sizeof(linkstack_t));temp->data = value;temp->next = s->next;s->next = temp;return 0;}//打印数据int linkstack_show(linkstack_t *s){while(s->next != NULL){s = s->next;printf("%d ", s->data);}putchar(10);return 0;}//出栈(弹栈) (类似与单链表的头删法)datatype_t linkstack_pop(linkstack_t *s){linkstack_t *temp = NULL;datatype_t value = 0;if(linkstack_empty(s)){printf("linkstack is empty\n");return (datatype_t)-1;}temp = s->next;s->next = temp->next;value = temp->data;free(temp);temp = NULL;return value;}int main(int argc, const char *argv[]){linkstack_t *s = linkstack_create();linkstack_push(s, 10);linkstack_push(s, 20);linkstack_push(s, 30);linkstack_push(s, 40);linkstack_push(s, 50);linkstack_show(s);printf("%d ", linkstack_pop(s));printf("%d ", linkstack_pop(s));printf("%d ", linkstack_pop(s));printf("%d ", linkstack_pop(s));printf("%d ", linkstack_pop(s));printf("\n");return 0;}
0 0
- 数据结构和算法--栈的链式实现
- 数据结构和算法--队列的链式实现
- 数据结构与算法设计---链式栈的实现(C++)
- 数据结构-链式栈的实现
- 数据结构---栈的链式实现
- 数据结构-->栈的链式实现
- 《数据结构和算法》之栈的链式存储结构
- 队列的链式表示和实现(数据结构)
- 数据结构双语课->栈的链式实现
- 数据结构基础【06】栈的链式实现
- 数据结构与算法设计---链式队列的实现(C++)
- 重温数据结构:栈的顺序表示和实现与栈的链式表示和实现
- [数据结构]链式队列的实现。
- 数据结构-链式队列的实现
- 数据结构---队列的链式实现
- 数据结构 - 队列的链式实现
- 数据结构—线性表的链式表示和实现
- 线性表的链式表示和实现(数据结构)
- 利用Tag接口自定义标签扩展
- 饮水思源
- vcg数据结构1
- Atitit图片复制父目录给你设计的实现 基于win 图片浏览器
- 51nod 1205 流水线调度
- 数据结构和算法--栈的链式实现
- C语言性能优化——使用位运算
- 一个acca web前端开发工程师考试操作题
- 设计模式---观察者模式(C++实现)
- 【11.8】P73 T2
- SVN服务端安装
- Android清除单个域名的cookie
- [js点滴]JavaScript中Image对象操作实例
- Codeforces 401D Roman and Numbers【状压dp】