队列的基本操作
2015-11-28 11:59
351 查看
队列特性:先进先出(FIFO)——先进队列的元素先出队列。来源于我们生活中的队列(先排队的先办完事)。
队列有下面几个操作:
InitQueue() 初始化队列
EnQueue() 进队列
DeQueue() 出队列
QueLength( ) 队列元素个数
DestroyQueue( ) 销毁队列
队列可以由数组和链表两种形式实现队列操作;
下边是队两种实现的基本操作:
队列的一个使用例子为:离散时间模拟(比如银行业务处理)!
队列有下面几个操作:
InitQueue() 初始化队列
EnQueue() 进队列
DeQueue() 出队列
QueLength( ) 队列元素个数
DestroyQueue( ) 销毁队列
队列可以由数组和链表两种形式实现队列操作;
下边是队两种实现的基本操作:
#include "stdafx.h" #include<iostream> using namespace std; #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 #define OVERFLOW -2 #define INFEASIALE -1 typedef int status; typedef int QElemType; //链队列,需要带头节点 struct LQnode{ QElemType data; struct LQnode *next; }; typedef struct{ LQnode *front; LQnode *rear; }LinkQueue; status InitQueue(LinkQueue &q) { LQnode *s = new LQnode; s->next = NULL; q.front = q.rear = s; return OK; } status GetFront(LinkQueue q, QElemType &e) { if (q.front == q.rear) { return ERROR; } e = q.front->next->data; return OK; } status DestroyQueue(LinkQueue &q) { while (q.front) { q.rear = q.front->next; delete q.front; q.front = q.rear; } return OK; } status EnQueue(LinkQueue &q, QElemType x) { LQnode *s = new LQnode; s->data = x; s->next = NULL; q.rear->next = s; q.rear = s; return OK; } status DeQueue(LinkQueue &q, QElemType &e) { if (q.front == q.rear) { return ERROR; } e = q.front->next->data; LQnode *s = q.front->next; q.front->next = q.front->next->next; if (q.rear == s)//这里是因为若队列只有一个元素时,出队一个元素后,尾指针应该指向q.front, { //若不修改,则会变成悬垂指针 q.rear = q.front; } delete s; return OK; } //循环队列,判断队列空和队列满时都有front == rear,需要区分队列空和队列满的状态 //方法1:另设一个标志位以区别“空”和“满” //方法2:少用一个元素空间,约定以队列头指针在队列尾指针的下一位置(环状)上作为队列满状态 //下面用第二种方法 #define MAXQSIZE 10 typedef struct{ QElemType *base; int front; int rear; }SqQueue; status Initqueue(SqQueue &q) { q.base = new QElemType[MAXQSIZE]; if (!q.base) { exit(OVERFLOW); } q.front = q.rear = 0; return OK; } int QueLength(SqQueue q) { return (q.rear - q.front + MAXQSIZE) % MAXQSIZE; } status EnQueue(SqQueue &q, QElemType x) { if ((q.rear + 1) % MAXQSIZE == q.front) { return ERROR; } q.base[q.rear] = x; q.rear = (q.rear + 1) % MAXQSIZE; return OK; } status DeQueue(SqQueue &q, QElemType &e) { if (q.front == q.rear) { return ERROR; } e = q.base[q.front]; q.front = (q.front + 1) % MAXQSIZE; return OK; } int _tmain(int argc, _TCHAR* argv[]) { LinkQueue q; InitQueue(q); SqQueue s; Initqueue(s); for (int i = 0; i < 10; i++) { EnQueue(q, i); EnQueue(s, i+1); } int e; GetFront(q, e); cout << "链队的头元素为:" << endl; cout << e << endl;; cout << "链队全部出队为:" << endl; for (int j = 0; j < 10; j++) { DeQueue(q, e); cout << e << ' '; } cout << endl; cout << "循环队列现在长度为:" << endl; int count = QueLength(s); cout << count << endl; cout << "循环队列全部出队为:" << endl; for (int k = 0; k < count; k++) { DeQueue(s, e); cout << e << ' '; } system("pause"); return 0; }
队列的一个使用例子为:离散时间模拟(比如银行业务处理)!
相关文章推荐
- JAVA中的包(Package)
- opencv中的BFMatcher和FlannBasedMatcher的区别
- 项目经理需要具备四种基本素质及八大管理技能
- 在eclipse中,如何添加现有java类文件或包文件到工程中?
- 手把手入门神经网络系列(1)_从初等数学的角度初探神经网络
- [DIY与计算机]推荐配件的一些依据
- POJ3267 DP
- 手把手入门神经网络系列(1)_从初等数学的角度初探神经网络
- java泛型总结2-2 面试题总结
- Android成长(三)——页面布局
- CCBPM 同表单分合流说明
- 调出 macbook pro第一次出现的“设置助理”
- 1049. Counting Ones
- 有用的导航栏代码
- React使用笔记1-React的JSX和Style
- NodeJS(三) Mac下 require 路径访问
- 项目管理四个核心要素
- 谈谈cookie和webStorage
- 一家公司要了你后,凭什么给你开高工资?
- if语句优化