C语言--模拟栈(使用链表)
来源:互联网 发布:大同集宁之战 知乎 编辑:程序博客网 时间:2024/06/07 08:56
使用链表模拟了栈,有压栈和弹栈
参照《程序员面试宝典》
#include <stdio.h>#include <stdlib.h>typedef struct student{ int data; struct student *next;} node;typedef struct stakqueue{ node *bottom,*top;}queue;// 压栈queue *push(queue *HQ,int x){ node *s,*p; s = (node *)malloc(sizeof(node)); s->data = x; s->next = NULL; if(HQ->bottom == NULL) // 栈为空的情况 { HQ->bottom = s; HQ->top = s; } else { HQ->top->next = s; HQ->top = s; } return HQ;}//弹栈queue *pop(queue *HQ){ node *p; int x; if(HQ->bottom == NULL) printf("\n No a node!\n"); else { x = HQ->bottom->data; p = HQ->bottom; if(HQ->bottom == HQ->top) // 只有一个元素 { HQ->bottom = NULL; HQ->top = NULL; free(p); } else { while(p->next != HQ->top) // 因为是链表实现的栈,所以出栈得找到链表尾 p = p->next; HQ->top = p; HQ->top->next = NULL; free(p->next); } } return HQ;}//输出栈中数据void print(queue *HQ){ node *p; p = HQ->bottom; printf("||"); while(p != NULL) { printf("%d => ",p->data); p = p->next; } printf("\n");}int main(){ queue *myst = new queue(); // 压栈 for(int i = 1; i < 6; i++) { myst = push(myst,i); print(myst); } // 弹栈 for(int i = 1; i < 6; i++) { myst = pop(myst); print(myst); } getchar(); return 0;}
阅读全文
0 0
- C语言--模拟栈(使用链表)
- C语言模拟栈
- C语言模拟单向链表
- 【C语言】模拟实现链表
- 使用两个栈模拟一个队列【C语言】
- C语言:使用两个栈模拟一个队列
- C/C++:使用数组模拟链表
- (C语言)简单明了的 数组模拟栈+ (C++)数组模拟栈
- C语言模拟学生管理系统链表学习
- c语言实现数据结构中的栈(数组模拟)
- 老码识途 创造面向对象语言(使用C模拟C++)
- 使用链表实现栈(C语言)
- 使用链表实现栈(C语言)
- C语言模拟栈存储结构笔记
- 进程调度模拟(C语言)
- RSA加密算法(C语言) 小数模拟
- 【C语言】模拟登陆程序(简易)
- c语言,电梯模拟
- maven持续集成本地搭建私有仓储
- 二分图最佳完美匹配——KM算法总结
- Eclipse报错: Failed to get the required ADT version number from the SDK
- 2017 计蒜之道 初赛 第五场 A. UCloud 机房的网络搭建
- Ubuntu下安装R,升级R版本,安装Rstudio,安装Rstudio Server以及安装Shiny Server
- C语言--模拟栈(使用链表)
- TCP长连接,短链接,双工,单工
- Python入门教程(更新中,日期:20170710)
- mariaDB语句详解
- 瀑布流分页数据去重问题
- 单点登录原理与简单实现
- eclipse(2)------Attribute "xmlns" was already specified for element "web-app"
- LeetCode (Binary Tree Zigzag Level Order Traversal)
- 史上最简单的 MySQL 教程(二十一)「数据的高级操作 之 蠕虫复制」