数据结构——队列——C++实现队列及其操作
来源:互联网 发布:unity3d粒子系统爆炸 编辑:程序博客网 时间:2024/06/07 09:47
队列的存储结构有两种:顺序存储结构和链式存储结构,称为顺序队列和链队列,在顺序队列中,队列满时进行入队操作产生“上溢”,为解决“上溢”问题,可以使用循环队列。
C++实现队列的构建和操作:
1:队列的结构体定义
2:置空队列
3:判断是否为空队列
4:进队
5:出队
6:显示整个队中元素
切记亲力亲为,动手实践写代码
Queue.h
#define MAXSIZE 100typedef int datatype;typedef struct {datatype data[MAXSIZE];int front,rear; //表示队列的头尾位置}Queue;//置空队列bool Set_NULL(Queue &Q);//判断队列是否为空bool Is_NULL(Queue Q);//入队bool En_Queue(Queue &Q,datatype a);//出队bool De_Queue(Queue &Q);//取队列头元素datatype front_element(Queue Q);//显示队列元素bool show(Queue Q);
Queue.cpp
#include "Queue.h"#include <iostream>using std::cin;using std::cout;using std::endl;//队列状态说明://front = -1 rear = -1 空队列//front = -1 rear != -1 有元素队列//front != -1 rear != -1 有元素出队列//置空队列bool Set_NULL(Queue &Q){Q.front = -1;Q.rear = -1;return true;}//判断队列是否为空bool Is_NULL(Queue Q){if (Q.front == Q.rear){return true; //队头等于队尾,为空}return false;}//入队bool En_Queue(Queue &Q,datatype a){if ((Q.rear - Q.front) >= MAXSIZE-1){cout<<"The queue is full~";return false;}Q.rear += 1;Q.data[Q.rear] = a;return true;}//出队bool De_Queue(Queue &Q){if (Is_NULL(Q)){cout<<"The queue is empty~";return false;}Q.front += 1;return true;}//取队列头元素datatype front_element(Queue Q){if (Is_NULL(Q)){cout<<"The queue is empty~";return NULL;}return Q.data[Q.rear];}//显示队列元素bool show(Queue Q){if (Is_NULL(Q)){cout<<"The queue is empty~";return false;}for (int i = Q.front;i < Q.rear;++i){cout<<Q.data[i+1]<<" ";}return true;}
main.cpp
#include "Queue.h"#include <iostream>using namespace std;int main(int argc,char** argv){Queue Q;Set_NULL(Q); //置空if (Is_NULL(Q)) //判断是否为空{cout<<"The queue is empty"<<endl;}else{cout<<"The queue is not empty"<<endl;}En_Queue(Q,2);En_Queue(Q,5);En_Queue(Q,9);En_Queue(Q,3);En_Queue(Q,2);show(Q);De_Queue(Q);cout<<endl;cout<<"After the De_queue:";show(Q);cout<<endl;cout<<"The rear element is:"<<front_element(Q);system("pause");return 0;}
阅读全文
1 0
- 数据结构——队列——C++实现队列及其操作
- 数据结构——队列、循环队列、链式队列主要操作函数的实现
- 数据结构(c)——队列
- 常用数据结构——队列及其应用
- 实验4:栈和队列的基本操作实现及其应用——循环队列
- 实验4:栈和队列的基本操作实现及其应用——链队列
- 实验3:栈和队列的基本操作实现及其应用——顺序队列和链队列
- 数据结构C语言实现系列[4]——队列
- 数据结构C语言实现系列——队列
- 数据结构C语言实现系列——队列
- 数据结构C语言实现系列——队列
- 数据结构C语言实现系列——队列
- 数据结构——队列的链式实现(C语言)
- [150422][C++]数据结构复习——队列实现源码
- 数据结构(四)——队列(C语言实现)
- C++——数据结构之 顺序队列 链式队列?
- 数据结构 ——队列操作的算法
- 数据结构——队列的基本操作
- 搭建一个简易的ftp服务器
- BZOJ 2152 聪聪可可
- Python 正则表达式 re模块
- tensorflow入门7 softmax函数和交叉熵损失函数
- RabbitMQ 常用命令
- 数据结构——队列——C++实现队列及其操作
- HDU3530(单调队列)
- LCS
- [学习笔记]Application用途
- java实现从上往下打印出二叉树的每个节点,同层节点从左至右打印。
- XCode里的模拟器到底在哪里?我的App被放到哪里了?程序员需要知道的XCODE相关路径
- Django 内建 中间件组件
- Navicat Premium for Mac 中文破解版
- opencv3 特征检测与匹配及寻找目标