当前位置: 首页 > news >正文

队列的运行算法

1.链队:

插入 删除 打印 取队顶
#include <stdio.h>
#include <stdlib.h>typedef struct Qnode{int data;struct Qnode *next;
}Qnode,*QuenePtr;typedef struct {QuenePtr front;QuenePtr rear;
}LinkQueue;
//初始化
void InitQueue(LinkQueue *q){(*q).front=(QuenePtr)malloc(sizeof (Qnode));if(!(*q).front) exit(0);(*q).front->next=NULL;(*q).rear=(*q).front;
}
//销毁
void DestroyQueue(LinkQueue *q){while((*q).front){(*q).rear=(*q).front->next;free((*q).front);(*q).front=(*q).rear;}
}
//判空
int IsEmpty(LinkQueue q){if(q.rear==q.front) return 1;else return 0;
}
//入队
void EnQueue(LinkQueue *q,int e){QuenePtr p=(QuenePtr)malloc(sizeof(Qnode));if(!p)exit(0);p->data=e;p->next=NULL;(*q).rear->next=p;(*q).rear=p;}
//出队
void DeQueue(LinkQueue *q,int *e){if((*q).rear==((*q).front)) printf("Empty !");QuenePtr p=(*q).front->next;*e=p->data;(*q).front->next=p->next;if((*q).rear ==p) (*q).rear=(*q).front;free(p);
}
//打印
void Show(LinkQueue *q){for(Qnode *p=q->front->next ;p!=NULL;p=p->next){printf("%d ",p->data);}printf("\n");
}int main(){LinkQueue q;InitQueue(&q);for(int i=0;i<5;i++){int e; scanf("%d",&e);EnQueue(&q,e);}Show(&q);if(IsEmpty(q)) printf("empty!");else printf("not empty");int e;DeQueue(&q,&e); printf("出队元素是:%d \n",e);Show(&q);int val;scanf("%d",&val);DestroyQueue(&q);return 0;
}

2.循环队列

wenti

#include<stdio.h>
#include<stdlib.h>
#define Maxsize 100typedef struct {int *base;int front ;int rear;
}SqQueue;//初始化
void InitQueue (SqQueue *q){q->base=(int *)malloc (Maxsize *sizeof (int));if(!q->base) exit(0);q->front=0;q->rear=0;
}
//入队
void EnterQueue(SqQueue *q,int e){if((q->rear+1)%Maxsize==q->front) printf("Full!\n");q->base[q->rear]=e;q->rear=(q->rear +1)%Maxsize;
}
//取队头元素
void getElem(SqQueue q,int *e){if(q.front!=q.rear){*e=q.base[q.front];}else {printf("Empty! \n");}
}
//求队长
int Getlength(SqQueue q){int e;e=(q.rear-q.front+Maxsize)%Maxsize;return e;
}
//出队
void PopQueue(SqQueue *q,int *e){if(q->front==q->rear) printf("Empty! \n");*e=q->base[q->front];q->front=(q->front+1)%Maxsize;
}
//打印
void Print(SqQueue q){for (int i=q.front;i!=q.rear;){printf("%d ",q.base[i]);i=(i+1)%Maxsize;}printf("\n");
}int main(){SqQueue q;int e;for(int i=0;i<5;i++){scanf("%d",&e);EnterQueue(&q,e);}Print(q);getElem(q,&e);printf("队顶元素是 %d \n",e);int length=Getlength(q);printf("队长是%d \n",length);PopQueue(&q,&e);printf("出队元素是%d \n",e);Print(q);return 0;
}

http://www.lryc.cn/news/195979.html

相关文章:

  • KVM/qemu安装UOS 直接让输入用户密码
  • 画一条0.5px的线、设置小于12px的字体、解决 1px 问题
  • Unity中Shader的深度写入ZWrite
  • Jetson nano 系列之7—jetson 通过rtp将视频发给远程host
  • 有哪些值得推荐的优秀 HTMLCSS 网站前端设计的网络资源(博客、论坛)?
  • RTSP/Onvif安防视频平台EasyNVR级联至EasyNVS系统不显示通道,是什么原因?
  • 点云处理【三】(点云降采样)
  • GB/T 41510-2022 起重机械安全评估规范 通用要求 摘要
  • 【vr】【unity】白马VR课堂系列-VR开发核心基础05-主体设置-手柄对象的引入和设置
  • UE5发布Android屏幕适配实践(Blueprint)
  • Spanner: Google’s Globally Distributed Database
  • Java基础——了解进制和位运算
  • mybatisplus 自定义mapper加多表联查结合分页插件查询时出现缺失数据的问题
  • 陪诊系统|陪诊软件革新陪诊体验解决病患难题
  • [Tkinter 教程08] Canvas 图形绘制
  • ES6 Symbol 数据结构
  • Redis常用数据类型、Redis常用命令
  • ERP系统是如何运作的?erp管理系统操作流程
  • springBoot复杂对象表示和lombok的使用
  • 如何选择最适合你的LLM优化方法:全面微调、PEFT、提示工程和RAG对比分析
  • Jenkins实现CI/CD发布(Ansible/jenkins共享库/gitlab)
  • 使用navicat查看类型颜色
  • iOS 中,Atomic 修饰 NSString、 NSArray,也会线程不安全
  • 2023医药微信公众号排名榜top100汇总合集
  • 基于YOLO算法的单目相机2D测量(工件尺寸和物体尺寸)三
  • Cython编译文件出错
  • WPF 用户控件依赖注入赋值
  • leetcode-48.旋转图像
  • antd的RangePicker设置默认值,默认近七天(andt+react)
  • 大数据可视化模块竞赛Vue项目文件结构与注意事项