数据结构-桟与队列.ppt

  1. 1、本文档共104页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
数据结构-桟与队列

POJ 3278 Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting. * Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute * Teleporting: FJ can move from any point X to the point 2 × X in a single minute. If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it? Input Line 1: Two space-separated integers: N and K Output Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow. Sample Input 5 17 Sample Output 4 Hint 5-10-9-18-17 DeQueue(Q, e) 初始条件:Q为非空队列。 操作结果:删除Q的队头元素,并用e返回其值。 a1 a2 an … … 链队列 结点定义 typedef struct QNode { QElemType data; struct QNode *next; }Qnode,*QueuePtr; 头结点 ^ …... front 队头 队尾 rear 设队首、队尾指针front和rear, front指向头结点,rear指向队尾 typedef struct{ QueuePtr front; QueuePtr rear; }LinkQueue; 链表定义 front rear x入队 ^ x front rear y入队 x ^ y front rear x出队 x ^ y front rear 空队 ^ front rear y出队 ^ 入队算法 出队算法 构造空队列算法 销毁队列算法 队列的顺序存储结构 实现: front=0 rear=0 1 2 3 4 5 0 队空 1 2 3 4 5 0 front J1,J1,J3入队 J1 J2 J3 rear J4 J5 J6 rear 1 2 3 4 5 0 J4,J5,J6入队 front 设两个游标front,rear,约定: rear指示队尾元素后一个下标; front指示队头元素 初值front=rear=0 空队列条件:front==rear 入队列:Q.base[ rear++] =x; 出队列:x= Q.base[front ++]; rear rear rear J1 J2 1 2 3 4 5 0 J1,J2,J3出队 J3 front front front front 约定:每当插入新的队列尾元素,“尾游标增1”;每当删除队列头元素,“头游标增1” 存在问题 设数组大小为M,则: 当front=0,rear=M时,再有元素入队发生溢出——真溢出 当front?0,rear=M时,再有元素入队发生溢出——假溢出 解决方案 队首固定,每次出队剩余元素向下移动——浪费时间 循环队列 基本思想:把队列设想成环形,让Q[0]接在Q[M-1]之后,若rear+1==M,则令rear=0; 0 M-1 1 front rear …... …... 实现:利用“模”运算 入队: Q.base[rear]=x; rear=(rear+1)%M; 出队: x= Q.base[front]; front=(front+1)%M; 队满、队空判定条件 0 1 2 3 4 5 rear front J5 J6 J7 0 1 2 3 4 5 rear front J4 J9

文档评论(0)

317960162 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档