snowy
Uploaded by
17 SLIDES
369 VIEWS
170LIKES

线性表 2

DESCRIPTION

线性表 2. 上海金融学院信息管理系. 1.3.3 环形队列. MAXN-1. head tail. MAXN-2. 0. : :. 环形队列的概念 环形队列:将数组元素 q[0] 与 q[MAXN-1] 连接,形成一个存放队列的环形空间。. …. 1. 4. 3. 2. 初始状态: head=tail=0. 1.3.3 环形队列. 一般情况:. MAXN-1. 0. head. 8. 1. 7. 5. 30. tail. 2. 56. -3. 6. -9. 17. 3. 5. 4.

1 / 17

Download Presentation

线性表 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 线性表2 上海金融学院信息管理系

  2. 1.3.3 环形队列 MAXN-1 head tail MAXN-2 0 : : 环形队列的概念 环形队列:将数组元素q[0]与q[MAXN-1]连接,形成一个存放队列的环形空间。 … 1 4 3 2 初始状态:head=tail=0

  3. 1.3.3 环形队列 一般情况: MAXN-1 . . . 0 head 8 1 7 5 30 tail 2 56 -3 6 -9 17 3 5 4

  4. 1.3.3 环形队列 • 算法 #define MAXN 100 int q[MAXN]; int head, tail, tag;

  5. 1.3.3 环形队列 若队列未满时,新结点进队: tail=(tail+1)%MAXN,然后将新结点存放在tail所指位置; 若队列未空时,结点可出队: head=(head+1)%MAXN, 然后将head所指的结点数据送出。 为什么要进行模MAXN的运算?

  6. 1.3.3 环形队列 因为不论队列是满还是空,均有head=tail。 问题:如何判别队列是满,还是空? 引进标志tag, 当head赶上tail,队列空,则令tag=0; 当tail赶上head,队列满,则令tag=1。

  7. 1.3.3 环形队列 head=tail=tag=0; int en_queue(x) // 进队 int x; { if(tail==head&&tag==1) return(1); // 队满出错 tail=(tail+1)%MAXN; q[tail]=x; //新结点进队 if(tail==head) tag=1; // 队满标志 return(0); }

  8. 1.3.3 环形队列 int de_queue(p_y) //出队 int *p_y; { if(head==tail&&tag==0) return(1); //队空出错 head=(head+1)%MAXN; *p_y=q[head]; //队首结点出队 if(head==tail) tag=0; //队空标志 return(0); }

  9. 1.3.3 环形队列 • 改进的算法 不使用tag标志,但限制tail赶上head,即队尾结点与队首结点之间至少留有一个元素的空间。 head tail 5 8 -3 9 -1 6 -7

  10. 1.3.3 环形队列 初始状态:head=tail=0 队列空: head==tail 队列满: (tail+1)%MAXN==head 进队操作:若队列不满,则 tail= (tail+1)%MAXN,然后新结点存放 tail所指位置。 出队操作:若队列不空,则 head=(head+1)%MAXN,然后将head 所指结点送出。

  11. 1.3.3 环形队列 head=tail=0; int en_c_q(x) //进队 int x; { if((tail+1)%MAXN ==head) return(1); //队满 tail= (tail+1)%MAXN; q[tail]=x; return(0); }

  12. 1.3.3 环形队列 int de_c_q(p_y) //出队 int *p_y; { if(head==tail) return(1); //队空 head=(head+1)%MAXN; *p_y=q[head]; return(0); }

  13. 1.3.4 双向队列 双向队列:能在两端进行插入和删除的线性表。 0 1 2 3 4 5 6 7 8 ……MAXN-1 leftright #define MAXN 100 int q[MAXN]; int left, right;

  14. 1.3.4 双向队列 初始状态:m=(MAXN-1)/2; left=m+1; right = m 0 1 …… m-1 m m+1 ……… MAXN-1 left right

  15. 1.3.4 双向队列 当left>right时,队列空; 当left=0时, 左端满; 当right=MAXN-1时,右端满。 左端进队,left--, 然后将结点存入left所指位置。 右端进队,right++, 然后将结点存入right所指位置。 左端出队,将left所指结点送出,然后left++。 右端出队,将right所指结点送出,然后right--。

  16. 1.3.4 双向队列 讨论: (1)如固定某端,插入和删除在一端进行,则这个双向队列就退化为栈。 (2)如果将双向队列的数组首尾连接起来,则构成环形双向队列。

  17. 1.3.4 双向队列 思考: (1)编写双向队列进队和出队的算法。 (2)编写环形双向队列的进队和出队的算法。

More Related