数据结构与算法——栈模拟队列(C语言)
2021/7/23 14:05:45
本文主要是介绍数据结构与算法——栈模拟队列(C语言),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
文章目录
- 前言
- 栈模拟队列
- 总结
前言
大二,刚刚开始学数据结构与算法,写得不好。。。。
栈模拟队列
使用栈实现队列的下列操作:
1.init():初始化队列
2.push(x):将1个元素放入队列的尾部。
3.pop(): 从队列首部移除元素。
4.display():展示队列元素。
#include <stdlib.h> #include <stdio.h> typedef struct stack { int data; struct stack *next; } Stack; Stack *Init(int n) { Stack *top = NULL; int i; Stack *node; for (i = 1; i < n+1; i++) { node = (Stack *) malloc(sizeof(Stack)); node->data = i; node->next = top; top = node; printf("入队: %d\n",node->data); } return top; } void Display(Stack *top) { Stack *p = top; while (p->next != NULL) { printf("%d ", p->data); p = p->next; } printf("%d ", p->data); printf("\n"); } Stack *Push(Stack *top, int data) { Stack *node = (Stack *) malloc(sizeof(Stack)); node->data = data; node->next = top; top = node; printf("入队: %d\n",top->data); return top; } Stack *Pop(Stack *top) { Stack *p = top; printf("出队:%d\n",p->data); top = top->next; free(p); return top; } Stack * Queue(Stack *top,int data){ Stack *node = (Stack *) malloc(sizeof(Stack)); node->data = data; node->next = top; top = node; return top; } int main() { Stack *z1, *z2,*p; z1 = Init(10); z2 = Init(0); p=z1; while (p->next!=NULL){ z2=Queue(z2,p->data); p=p->next; } z2=Queue(z2,p->data); z2=Pop(z2); z2=Pop(z2); z2=Pop(z2); Display(z2); }
总结
就离谱 算法也是很难。这篇关于数据结构与算法——栈模拟队列(C语言)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-29易优CMS安装常见问题汇总-icode9专业技术文章分享
- 2024-06-28易优新手必读安装教程-icode9专业技术文章分享
- 2024-06-28忘记eyoucms后台密码怎么办?-icode9专业技术文章分享
- 2024-06-26终极指南:Scrum中如何设置需求优先级
- 2024-06-26AI大模型企业应用实战(25)-为Langchain Agent添加记忆功能
- 2024-06-26小白家庭 nas 搭建方案-icode9专业技术文章分享
- 2024-06-23AI大模型企业应用实战(14)-langchain的Embedding
- 2024-06-23AI大模型企业应用实战(15)-langchain核心组件
- 2024-06-23AI大模型企业应用实战(16)-langchain核心组件
- 2024-06-23AI 大模型企业应用实战(06)-初识LangChain