已阅读5页,还剩26页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Queue C and Data Structures Baojian Hua Linear List nRecall that a linear list has the form: nThe delete and insert operations may insert or delete an arbitrary element e_i nIf the delete is restricted at one end and insert at the other end, we get a queue Example: Queue of Char a insert b a insert insert b a c delete b c Abstract Data Types in C: Interface / in file “queue.h” #ifndef QUEUE_H #define QUEUE_H typedef struct Queue_t *Queue_t; Queue_t Queue_new (); int Queue_size (Queue_t q); int Queue_isEmpty (Queue_t q); void Queue_enQueue (Queue_t q, poly x); poly Queue_deQueue (Queue_t q); poly Queue_getHead (Queue_t q); #endif Implementation Using Extensible Array / in file “arrayQueue.c” #include “queue.h” struct Queue_t Array_t l; ; / Recall the box strategy: l q Operations: “new” Queue_t Queue_new () Queue_t q = malloc (sizeof (*q); q-l = Array_new (); return q; 0 n-1 array max tail l q Operations: “size” int Queue_size (Queue_t q) return Array_length (q-l); 0 n-1 array max tail l q Operations: “isEmpty” int Queue_isEmpty (Queue_t q) return Array_isEmpty (q-l); 0 n-1 array max tail l q Operations: “enQueue” void Queue_enQueue (Queue_t q, poly x) Array_insertLast (stk-l, x); return; 0 n-1 array max tail l q Operations: “deQueue” poly Queue_deQueue (Queue_t q) if (Array_isEmpty (q-l) error (“empty queue”); return Array_deleteFirst (q-l); Operations: “deQueue” 0 n-1 array max tail l q Operations: “deQueue” 0 n-1 array max tail l q Analysis nWhats the complexity? nenQueue: O(1) ndeQueue: O(n) ndata movement nCan we do better? nLazy approach nbetter amortized performance nCircular queue Lazy Approach nInstead of moving data when “deQueue”, we move data only when “enQueue” reaching the tail of the queue nO(n) on n operations nwhich has O(1) amortized cost Lazy Approach nWhats necessary modification? nLeave this as a programming assignment 0 n-1 array max tail l q Circular Queue nA refinement of the lazy approach is the circular queue head = tail = 0; 0 1 2 3 4 5 tail head enQueue (q, a); Circular Queue nA refinement of the lazy approach is the circular queue head = tail = 0; 0 1 2 3 4 5 tail head enQueue (q, a); enQueue (q, b); a Circular Queue nA refinement of the lazy approach is the circular queue head = tail = 0; 0 1 2 3 4 5 tail head enQueue (q, a); enQueue (q, b); enQueue (q, c); a b Circular Queue nA refinement of the lazy approach is the circular queue head = tail = 0; 0 1 2 3 4 5 tail head enQueue (q, a); enQueue (q, b); enQueue (q, c); enQueue (q, d); a b c Circular Queue nA refinement of the lazy approach is the circular queue head = tail = 0; 0 1 2 3 4 5 tail head enQueue (q, a); enQueue (q, b); enQueue (q, c); enQueue (q, d); enQueue (q, e); a b c d Circular Queue nA refinement of the lazy approach is the circular queue head = tail = 0; 0 1 2 3 4 5tail head enQueue (q, a); enQueue (q, b); enQueue (q, c); enQueue (q, d); enQueue (q, e); a b c d e enQueue (q, f); ? Circular Queue nA refinement of the lazy approach is the circular queue Empty: head = tail; Full: tail+1 = head ? General Equations: head = (head+1)%N; tail = (tail+1)%N; 0 1 2 3 4 5tail head a b c d e Circular Queue / Cook these together, we can implement the / input buffer using queue: struct Buffer_t char buf128; int head; int tail; ; struct Buffer_t Buffer_t; / Rethink the key pressing, and “getchar ()”? Implementation Using Linked List / in file “linkedQueue.c” #include “queue.h” struct Queue_t List_t l; ; l q data next data next data next Operations: “new” Queue_t Queue_new () Queue q = malloc (sizeof (*q); q-l = LinkedList_new (); return q; l q / / Operations: “size” int Queue_size (Queue_t q) return LinkedList_length (q-l); l q data next data next data next Operations: “isEmpty” int Queue_isEmpty (Queue_t q) return LinkedList_isEmpty (q-l); l q data next data next data next Operations: “enQueue” void Queue_enQueue (Queue_t q, poly x) / note the difference with extensible array- / based representation LinkedList_insertLast (q-l, x); return; l q data next data next data next Operations: “deQueue” poly Queue_deQueue (Queue_t q) if (LinkedList_isEmpty (q-l) error (“empty queue”); return LinkedList_deleteFirst (q-l); l q data next data next data next Analysis nWhats the complexity of these operations? nenQueue: O(n) nsearch the last element ndeQueue: O(1) nImprovement: Circular linked list nleave as programming assignment (y0B3E6I9LdOgRjVmYp!t&w-z1C4G7JaM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下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025湖南怀化市靖州苗族侗族自治县九苓经济建设投资有限公司招聘5人考试笔试备考题库及答案解析
- 2025年绍兴市越才人力资源服务有限责任公司招聘笔试考试参考试题及答案解析
- 消渴病肾病糖尿病肾病健康教育与康复指导
- 外贸出口钢材的实习报告
- 2026福建泉州市惠安县公办学校赴华中师范大学招聘编制内新任教师20人考试笔试参考题库附答案解析
- 《JBT9045-1999 刷筛》(2026年)实施指南
- 《JBT8774.13-1998 机床电气系统辅件双管固定座》(2026年)实施指南
- 2025安徽合肥演艺集团有限公司舞台技术人员招聘1人考试笔试参考题库附答案解析
- 2025中国中煤华东分公司及所属公司第三批社会招聘56人笔试考试备考试题及答案解析
- 2025年新能源行业网络安全防护与信息安全防护措施
- (完整版)“十五五”护理工作发展规划详解
- 2025版酒店承包经营合同(含客房、餐饮及会议设施)
- 呼吸科新进展课件
- 医用三类射线装置辐射工作人员考试题型(附答案)
- 2025年医疗卫生系统招聘考试(财务会计)模拟试题及答案(宁波)
- T-HNTI 021-2020 湘西黄金茶 生态茶园建设技术规范
- 2025年铁路会计考试题库
- 班组长转正述职汇报
- 信息沟通管理办法
- 2025年从村干部工人农民和服务基层项目人员中考试录用乡镇机关公务员《公共基础知识》真题(含答案)
- 苗寨干栏式建筑生态智慧-洞察及研究
评论
0/150
提交评论