c实现队列
2024-09-05 04:58:50
使用链表实现队列的入队和出队
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h> using namespace std; //节点
typedef struct student
{
int data;
struct student *next;
}node; //队列
typedef struct linkqueue
{
node *first; //队首节点
node *rear; //队尾节点
}queue; //入队
queue *insert(queue *headque, int x)
{
//1. 定义变量
node *s = NULL; //2. 新建节点
s = (node*)malloc(sizeof(node));
s->data = x;
s->next = NULL; //3. 插入队尾
if (headque->rear == NULL) //第一个节点
{
headque->first = s;
headque->rear = s;
}
else
{
headque->rear->next = s;
headque->rear = s;
} //4. 返回队列
return headque;
} //出队
queue *del(queue *headque)
{
//1. 定义变量
node *p = NULL;
int x = ; //2. 节点出队
if(headque->first == NULL) //队列为空
{
printf("队列为空\n");
}
else
{
x = headque->first->data;
printf("num:%d\n", x); p = headque->first;
if (headque->first == headque->rear) //到达队尾
{
headque->first = NULL;
headque->rear = NULL;
}
else //删除节点
{
headque->first = headque->first->next;
free(p);
p = NULL;
} //4. 返回队列
return headque;
}
} //显示队列所有节点信息
void show(queue *headque)
{
//1. 定义变量
node *p = NULL;
int x = ; //2. 遍历显示
p = headque->first;
while(p != NULL)
{
x = p->data;
printf("%d ", x);
p = p->next;
}
printf("\n");
} int main()
{
queue *headque = (queue*)malloc(sizeof(queue));
insert(headque, );
insert(headque, );
insert(headque, );
show(headque);
del(headque);
show(headque);
del(headque);
show(headque); }
最新文章
- tomcat共享lib里面的jar包
- Ubuntu16.04安装nginx
- Tomcat的目录结构、处理流程、主配置文件(server.xml)释义
- NoSQL之【MongoDB】学习(二):DML和查询操作说明
- bzoj1855: [Scoi2010]股票交易--单调队列优化DP
- Java动手实验及课后程序
- IOS缓存之NSCache缓存
- USB做Host的OTG原理
- JGUI源码:实现图标按钮及下拉菜单(16)
- Node.js_Buffer 缓冲区
- AI illustrator 如何裁剪图片(扣取局部区域)
- MUI学习03-滚动图(幻灯片)及菜单项(九宫格)
- node学习系列 搭建express
- 如何在Vue项目中使用vw实现移动端适配(转)
- CS229 6.4 Neurons Networks Autoencoders and Sparsity
- 简单聊聊SOA和微服务
- 在Windows Phone项目中调用C语言DLL
- EFM32 DMA/PRS例程
- 高级service之ipc ADIL用法
- [CLR via C#]基元类型
热门文章
- 阿里数据迁移DTS【otter】和阿里巴巴mysql数据库binlog的增量订阅&;消费组件 【canal】
- MySQL 子查询(二)
- Linux Mysql 备份与还原
- 与app交互因异步造成的坑记录
- liunx mkisofs 命令的使用(制作iso)
- 更优雅地关闭资源 - try-with-resource及其异常抑制--转载
- TLS1.3&;TLS1.2形式化分析(二)
- 第一课 IP通信
- 爬虫部署 --- scrapyd部署爬虫 + Gerapy 管理界面 scrapyd+gerapy部署流程
- Python借助argv和input()制作命令行工具