C语言数据结构队列
#include<stdio.h>
#include<stdlib.h>
struct Node {
int data;
Node *next;
};
struct Queue {
Node* head;
Node* rear;
};
Queue* creatQueue() {
Queue *Q = new Queue;
Node *node = (Node*)malloc(sizeof(Node));
node->next = NULL;
Q->head = Q->rear = node;
return Q;
}
void enQueue(Queue *Q, int d) {
Node *node = (Node*)malloc(sizeof(Node));
node->data = d;
node->next = NULL;
Q->rear->next = node;
Q->rear = node;
}
void output(Queue *Q) {
int data;
if (Q->head == Q->rear) {
printf("队列下溢!");
}
Node *node = Q->head->next;
data = node->data;
Q->head->next = node->next;
if (node->next == NULL) {
Q->rear = Q->head;
}
free(node);
}
void printQueue(Queue *Q) {
Node *node = Q->head->next;
puts("队列成员如下\n\n");
while (node != NULL) {
printf("%d\n", node->data);
node = node->next;
}
putchar('\n');
}
void destroyQueue(Queue *Q) {
Node *node;
while (Q->head) {
node = Q->head->next;
free(Q->head);
Q->head = node;
}
free(Q);
}
int main(int argc,char** argv) {
int select,d;
Queue *Q = creatQueue();
while (1)
{
puts("1,入队\n2,出队\n3,遍历\n0,退出\n\n请输入操作号:");
scanf("%d", &select);
switch (select)
{
case 1:
puts("请输入需要入队的数字:");
scanf("%d", &d);
enQueue(Q, d);
break;
case 2:
output(Q);
break;
case 3:
printQueue(Q);
break;
case 0:
printf("程序已退出\n");
exit(1);
break;
default:
puts("输入错误!\n");
break;
}
}
destroyQueue(Q);
}
最新文章
- 光驱SSD安装Win7+ubuntu系统双系统
- dedecms为文档页增加动态点击
- hibernate save,update,saveorupdate方法有什么区别
- JavaScript中的Array
- (四)ubuntu学习前传—uboot中对Flash和DDR的管理
- HTML5自学笔记[ 11 ]canvas绘图基础1
- C++数据结构之Linked Stack(链式栈)
- Cache
- JS request函数 用来获取url参数
- Matlab 之 im2col
- 【转】如何删除一个repository(仓库)
- ZigBee研究之旅(一)
- Android开发(24)---安卓中实现多线程下载(带进度条和百分比)
- Aliyun OSS Nginx proxy module(阿里云OSS Nginx 签名代理模块)
- Maven Tomcat7+ 实现自动化部署
- vue里使用create、mounted调用方法的正确姿势
- VSCode 打开文件tab键空格数量异常问题
- 一条sql语句引发的遐想:select t.*, t.rowid from STUDENT t
- 【Ctrl】 + 【Alt】 + 【F1~F6】 和 【Ctrl】 + 【Alt】 + 【T】打开的终端有什么不同?
- linux 软件编译问题汇总
热门文章
- TCP/IP协议基本知识
- (转)GNU风格ARM汇编语法指南(非常详细)5
- ssh 公钥 下载选择的时候 下拉选择 ssh 然后 git clone
- 如何使用Postman编写Testlink测试用例
- Integration of Metabolomics and Transcriptomics To Reveal Metabolic Characteristics and Key Targets Associated with Cisplatin Resistance in Nonsmall Cell Lung Cancer(解读人:林山云)
- hdu1253胜利大逃亡(城堡的怪物太狠,主角难免天天逃亡)
- [模板] LCA-最近公共祖先-倍增法
- SQL 分组内求最大N个或最小N个
- MySQL5.7 import表结构报错超出表空间界限
- [Intervention] Ignored attempt to cancel a touchmove event with cancelable=false, for example because scrolling is in progress and cannot be interrupted