当前位置:文档之家› 数据结构第四章队列教案

数据结构第四章队列教案


Q.front==Q.rear=0
循环队列的基本操作:
将新元素item的值插入到队尾 void Qinsert(QueueType& Q, const ElemType& item);
从队列Q中删除队首元素并返回之 ElemType Qdelete (QueueType& Q);
进队 (将新元素item的值插入到队尾)
2. 队列的链接存储结构
struct LNode { ElemType data; LNode *next; };
链队列类型 struct LinkQueue{ LNode *front; LNode *rear; };
Q.front Q.rear
//队头指针 //队尾指针
a1
a2

an ∧
1.初始化
delete p; p=Q.front;
2.清除链队为空
void ClearQueue(LinkQueue& HQ) //清除链队中的所有结点,使之成为空队 { LNode* p=Q.front; while(p!=NULL) { Q.front=Q.front->next; delete p; p=Q.front; } Q.rear=NULL; } //置队尾指针为空
24 24 24 24 24 24
程序运行: 5 进队,
24 出队
i值 5
队中元素 78 58 62 5
78 58 62 5
24
while 循环 78 出队
58 出队 62 出队 5 出队
程序2:
#include<iostream.h> #include<stdlib.h> typedef int ElemType; struct LNode { ElemType data; LNode* next; }; struct LinkQueue { LNode* front; LNode* rear; };
4.4 队列 4.4.1 队列的定义
循环队列—Leabharlann 顺序映象链队列——链式映象
队列的顺序存储结构
struct Queue
{
ElemType queue[QueueMaxSize];
int front, rear; };
队头
0 1 2 3 4 5 6 7 8
QueueMaxSize-1
(a) (b)(c) d e f g h Q.front
3.进队 ( 向链队中插入一个元素)
Q.front Q.rear
a1
a2

an ∧
an+1
/\
Q.rear->next=newptr; Q.rear=newptr; newptr
void Qinsert (LinkQueue &Q, const ElemType &item) { LNode *newptr = new LNode; …… //分配新结点 newptr->data = item; p->next = NULL; if (Q.rear==NULL; //空队列 Q.front=Q.rear=newptr; //第一个结点 else { Q.rear=newptr; Q.rear->next=newptr;} }
Q.rear=NULL;
delete p;
ElemType QDelete(LinkQueue& Q) { if(Q.front==NULL) //判空 { cerr<<"queue is empty!"<<endl; exit(1); } ElemType temp=Q.front->data; LNode* p=Q.front; Q.front=p->next; if(Q.front==NULL) Q.rear=NULL; delete p; return temp; }
出队 ( 删除队首元素并返回之)
ElemType QDelete(Queue& Q)
{ if(Q.front==Q.rear) //判空 { cerr<<"Queue is empty!"<<endl; exit(1); } Q.front=(Q.front+1)%QueueMaxSize; //移动尾指针 return Q.queue[Q.front]; } //返回队首元素
进队: 出队:
Q.rear
队尾
Q.rear++; Q. queue[Q.rear]=i; Q.front++; x=Q. queue[Q.front];
10
9 8
11
0 1 2
7
6 5 4 3
求余: X %12 结果在 0~11之间
Q.rear++; 改为 Q.front++; 改为 Q.rear=(Q.rear+1)%QueueMaxSize; Q.front=(Q.front+1)%QueueMaxSize;
程序1功能: #include //结构定义…… #include …… void main() { …… for(int i=0; i<6; i++) { //两个随机数进队 } //一个出队 while(!QueueEmpty(q)) { // 队中元素出队 } }
程序运行:
i值 队中元素( 6 ) 41 进队, 67 进队 0 41 67 41 出队 67 34 进队,0 进队 1 67 34 0 67 出队 34 0 69 进队, 24 进队 2 34 0 69 34 出队 0 69 78 进队, 58 进队 3 78 58 0 69 0 出队 78 58 69 62 进队, 4 78 58 62 69 69 出队 78 58 62
cout<<endl; while(!QueueEmpty(q)) {
cout<<QDelete(q)<<" 出队"<<endl;
}
程序1:
#include<iostream.h> #include<stdlib.h> const int QueueMaxSize=6; typedef int ElemType; struct Queue { ElemType queue[QueueMaxSize]; int front, rear; }; #include"queue.h"
41 67 34 0 69 24 78 58 62 64 5 45 81 27 61 91 95 42 27 36 41 34 67 0 69 24 5 78 45 58 81 62 27 64 61 42 91 36
循环队列判定条件:
Q.rear 9 10
判空条件:
i
8
7 6
j
Q.front Q.front==Q.rear 11 Q.rear 初始空表: 0 k
h g f
e d
a 1 a,b,c,d, e,f,g,h b 2 c 判满条件: 11个元素 i,j,k,
3 k=(Q.rear+1)%QueueMaxSize 进队列 5 4 k == Q.front
void InitQueue(LinkQueue& Q) //把队首和队尾指针置为空 { Q.front=Q.rear=NULL;
}
空队列
Q.front Q.rear
NULL
2.清除链队为空
Q.front Q.rear
a1
p
a2
p

an ∧
p=Q.front
循环
Q.front=Q.front->next;
void Qinsert (Queue& Q, const ElemType& item) { int k=(Q.rear+1)%QueueMaxSize; //求出队尾的下一个位置 if(k==Q.front) //判满 { cerr<<"Queue overflow!"<<endl; exit(1); } Q.rear=k; //移动尾指针 Q.queue[k]=item; //赋值给队尾 }
for(int i=0; i<20; i++) { int x=rand()%100; cout<<x<<" "; if(x%2==1) QInsert(q1,x); //用q1链队存放奇数 else QInsert(q2,x); //用q2链队存放偶数 }
while(!QueueEmpty(q1) &&!QueueEmpty(q2)) { //每一行输出一个奇数和 一个偶数,直到任一队列空为止 cout<<QDelete(q1)<<“ "<<QDelete(q2)<<endl; }
4.出队 ( 从链头取出一个元素)
Q.front Q.rear
a1
p
a2
an ∧
p=Q.front;
Q.front=p->next;
delete p;
出队的一种特殊情况
( 只有一个结点时)
Q.front Q.rear
a1 ∧
p
p=Q.front; Q.front=p->next; if Q.front=NULL
4.4.5 使用队列的程序举例
程序1: #include …… #include …… void main() { …… for(int i=0; i<6; i++) { …… } while(!QueueEmpty(q)) { …… } }
相关主题