Presentation is loading. Please wait.

Presentation is loading. Please wait.

Queue C and Data Structures Baojian Hua

Similar presentations


Presentation on theme: "Queue C and Data Structures Baojian Hua"— Presentation transcript:

1 Queue C and Data Structures Baojian Hua bjhua@ustc.edu.cn

2 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

3 Example: Queue of Char ‘a’‘a’ insert ‘b’‘b’ ‘a’‘a’ ‘b’‘b’ ‘a’‘a’ ‘c’‘c’ delete ‘b’‘b’ ‘c’‘c’

4 Abstract Data Types in C: Interface // in file “queue.h” #ifndef QUEUE_H #define QUEUE_H typedef struct queueStruct *queue; queue newQueue (); int size (queue q); int isEmpty (queue q); void enQueue (queue q, poly x); poly deQueue (queue q); poly getHead (queue q); #endif

5 Implementation Using Extensible Array // in file “arrayQueue.c” #include “queue.h” struct queueStruct { arrayList l; }; // Recall the box strategy: l q

6 Operations: “ new ” queue newQueue () { queue q = (queue)malloc (sizeof (*q)); q->l = newArrayList (); return q; } 0 n-1 array max tail l q

7 Operations: “ size ” int size (queue q) { return arrayListLength (q->l); } 0 n-1 array max tail l q

8 Operations: “ isEmpty ” int isEmpty (queue q) { return arrayListIsEmpty (q->l); } 0 n-1 array max tail l q

9 Operations: “ enQueue ” void enQueue (queue q, poly x) { arrayListInsertLast (stk->l, x); return; } 0 n-1 array max tail l q

10 Operations: “ deQueue ” poly deQueue (queue q) { if (arrayListIsEmpty (q->l)) error (“empty queue”); return arrayListDeleteFirst (q->l); }

11 Operations: “ deQueue ” 0 n-1 array max tail l q

12 Operations: “ deQueue ” 0 n-1 array max tail l q

13 Analysis What ’ s the complexity? enQueue: O(1) deQueue: O(n) data movement Can we do better? Lazy approach better amortized performance Circular queue

14 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

15 Lazy Approach What ’ s necessary modification? Leave this as a programming assignment 0 n-1 array max tail l q

16 Circular Queue A refinement of the lazy approach is the circular queue head = tail = 0; 0 1 2 3 4 5 tail head enQueue (q, ‘a’);

17 Circular Queue A 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’‘a’

18 Circular Queue A 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’‘a’ ‘b’‘b’

19 Circular Queue A 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’); ‘a’‘a’ ‘b’‘b’ ‘c’‘c’

20 Circular Queue A 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’‘a’ ‘b’‘b’ ‘c’‘c’ ‘d’‘d’

21 Circular Queue A 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’‘a’ ‘b’‘b’ ‘c’‘c’ ‘d’‘d’ ‘e’‘e’ enQueue (q, ‘f’); ???

22 Circular Queue A 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 5 tail head ‘a’‘a’ ‘b’‘b’ ‘c’‘c’ ‘d’‘d’ ‘e’‘e’

23 Circular Queue // Cook these together, we can implement the // input buffer using queue: struct bufferStruct { char buf[128]; int head; int tail; }; struct bufferStruct inputBuffer; // Rethink the key pressing, and “getchar ()”?

24 Implementation Using Linked List // in file “linkedQueue.c” #include “queue.h” struct queueStruct { linkedList l; }; l q data next data next data next …

25 Operations: “ new ” queue newQueue () { queue q = (queue)malloc (sizeof (*q)); q->l = newLinkedList (); return q; } l q /\

26 Operations: “ size ” int size (queue q) { return linkedListLength (q->l); } l q data next data next data next …

27 Operations: “ isEmpty ” int isEmpty (queue q) { return linkedListIsEmpty (q->l); } l q data next data next data next …

28 Operations: “ enQueue ” void enQueue (queue q, poly x) { // note the difference with extensible array- // based representation linkedListInsertLast (q->l, x); return; } l q data next data next data next …

29 Operations: “ deQueue ” poly deQueue (queue) { if (linkedListIsEmpty (q->l)) error (“empty queue”); return linkedListDeleteFirst (q->l); } l q data next data next data next …

30 Analysis What ’ s the complexity of these operations? enQueue: O(n) search the last element deQueue: O(1) Improvement: Circular linked list leave as programming assignment


Download ppt "Queue C and Data Structures Baojian Hua"

Similar presentations


Ads by Google