使用链表实现队列的入队和出队

#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); }

最新文章

  1. tomcat共享lib里面的jar包
  2. Ubuntu16.04安装nginx
  3. Tomcat的目录结构、处理流程、主配置文件(server.xml)释义
  4. NoSQL之【MongoDB】学习(二):DML和查询操作说明
  5. bzoj1855: [Scoi2010]股票交易--单调队列优化DP
  6. Java动手实验及课后程序
  7. IOS缓存之NSCache缓存
  8. USB做Host的OTG原理
  9. JGUI源码:实现图标按钮及下拉菜单(16)
  10. Node.js_Buffer 缓冲区
  11. AI illustrator 如何裁剪图片(扣取局部区域)
  12. MUI学习03-滚动图(幻灯片)及菜单项(九宫格)
  13. node学习系列 搭建express
  14. 如何在Vue项目中使用vw实现移动端适配(转)
  15. CS229 6.4 Neurons Networks Autoencoders and Sparsity
  16. 简单聊聊SOA和微服务
  17. 在Windows Phone项目中调用C语言DLL
  18. EFM32 DMA/PRS例程
  19. 高级service之ipc ADIL用法
  20. [CLR via C#]基元类型

热门文章

  1. 阿里数据迁移DTS【otter】和阿里巴巴mysql数据库binlog的增量订阅&amp;消费组件 【canal】
  2. MySQL 子查询(二)
  3. Linux Mysql 备份与还原
  4. 与app交互因异步造成的坑记录
  5. liunx mkisofs 命令的使用(制作iso)
  6. 更优雅地关闭资源 - try-with-resource及其异常抑制--转载
  7. TLS1.3&amp;TLS1.2形式化分析(二)
  8. 第一课 IP通信
  9. 爬虫部署 --- scrapyd部署爬虫 + Gerapy 管理界面 scrapyd+gerapy部署流程
  10. Python借助argv和input()制作命令行工具