刊首寄语title71.pptVIP

  • 2
  • 0
  • 约4.64千字
  • 约 31页
  • 2018-03-29 发布于广东
  • 举报
刊首寄语title71

Queue C and Data Structures Baojian Hua bjhua@ustc.edu.cn Linear List Recall that a linear list has the form: The delete and insert operations may insert or delete an arbitrary element e_i If the delete is restricted at one end and insert at the other end, we get a queue Example: Queue of Char 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: Operations: “new” Queue_t Queue_new () { Queue_t q = malloc (sizeof (*q)); q-l = Array_new (); return q; } Operations: “size” int Queue_size (Queue_t q) { return Array_length (q-l); } Operations: “isEmpty” int Queue_isEmpty (Queue_t q) { return Array_isEmpty (q-l); } Operations: “enQueue” void Queue_enQueue (Queue_t q, poly x) { Array_insertLast (stk-l, x); return; } Operations: “deQueue” poly Queue_deQueue (Queue_t q) { if (Array_isEmpty (q-l)) error (“empty queue”); return Array_deleteFirst (q-l); } Operations: “deQueue” Operations: “deQueue” Analysis What’s the complexity? enQueue: O(1) deQueue: O(n) data movement Can we do better? Lazy approach better amortized performance Circular queue Lazy Approach Instead of moving data when “deQueue”, we move data only when “enQueue” reaching the tail of the queue O(n) on n operations which has O(1) amortized cost Lazy Approach What’s necessary modification? Leave this as a programming assignment Circular Queue A refinement of the lazy approach is the circular queue Circular Queue A refinement of the lazy approach is the circular queue Circular Queue A refinement of the lazy approach is the circular queue Circular Queue A refinement of the lazy ap

文档评论(0)

1亿VIP精品文档

相关文档