1 / 25

FIFO Queues

FIFO Queues. CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington. FIFO Queues. Stacks are last-in first-out queues. Another widely used model is first-in first-out (FIFO) queues. Examples of uses of FIFO queues:. FIFO Queues.

gomer
Download Presentation

FIFO Queues

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. FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

  2. FIFO Queues • Stacks are last-in first-outqueues. • Another widely used model is first-in first-out (FIFO) queues. • Examples of uses of FIFO queues:

  3. FIFO Queues • Stacks are last-in first-outqueues. • Another widely used model is first-in first-out (FIFO) queues. • Examples of uses of FIFO queues: • Program execution: • Requests for access to memory, disk, network... • Resource allocation: • Forwarding network traffic in network switches and routers. • Search algorithms. • More details later in the course

  4. Put and Get • The FIFO queue supports insert and delete as follows: • insert, push, put: This is what we call the insert operation when we talk about FIFO queues. It puts an item "at the end of the line". • delete, pop, get: This is what we call the delete operation when we talk about FIFO queues. It removes the item that is "at the head of the line". • How can we implement FIFO queues?

  5. FIFO Queues Using Lists A FIFO queue is essentially a list. put(queue, item) inserts that item at the END of the list. get(queue) removes (and returns) the item at the beginning of the list. What is the running time of these operations?

  6. FIFO Queues Using Lists • A FIFO queue is essentially a list. • put(queue, item) inserts that item at the END of the list. • get(queue) removes (and returns) the item at the beginning of the list. • Both operations take O(1) time. • Assumption: the list data type contains a pointer to the last element. • Our new implementation at lists.c satisfies that assumption.

  7. Queue Versus Stack Implementation • Implementation-wise, compare: • list-based queue implementation. • list-based stack implementation. • What are the key differences?

  8. Queue Versus Stack Implementation • Implementation-wise, compare: • list-based queue implementation. • list-based stack implementation. • The only difference is in insertions. • Queues insert at the end of the list. • Stacks insert at the beginning of the list. • It is very easy to implement queues starting from our list-based stack implementation. • Rename push to put. • Rename pop to get. • Change the get function to insert at the end.

  9. FIFO Queues Using Arrays • How do we implement queues using arrays? • The stack definition looked like this: typedefstructstack_struct * Stack; structstack_struct { intmax_size; inttop_index; void ** items; }; • What changes do we need to accommodate queues?

  10. FIFO Queues Using Arrays • How do we implement queues using arrays? • A queue can be defined like this: typedefstructqueue_struct* queue; structqueue_struct { intmax_size; intstart_index; intend_index; void ** items; }; • end_indextells us where to put a new item. • start_index tells us where to remove an item from.

  11. Examples of Put and Get queue position - 3 end_index - 2 start_index - 1 - 0 put(15) put(20) get() put(30) put(7) put(25) get() put(12) get() get()

  12. Examples of Put and Get queue position - 3 end_index - 2 start_index - 1 15 0 put(15) put(20) get() put(30) put(7) put(25) get() put(12) get() get()

  13. Examples of Put and Get queue position end_index - 3 - 2 start_index 20 1 15 0 put(15) put(20) get() put(30) put(7) put(25) get() put(12) get() get()

  14. Examples of Put and Get queue position end_index - 3 start_index - 2 20 1 - 0 put(15) put(20) get(): returns 15 put(30) put(7) put(25) get() put(12) get() get()

  15. Examples of Put and Get queue position end_index - 3 start_index 30 2 20 1 - 0 put(15) put(20) get() put(30) put(7) put(25) get() put(12) get() get()

  16. Examples of Put and Get queue position 7 3 start_index 30 2 end_index 20 1 - 0 put(15) put(20) get() put(30) put(7) put(25) get() put(12) get() get()

  17. Examples of Put and Get queue position 7 3 start_index 30 2 end_index 20 1 25 0 put(15) put(20) get() put(30) put(7) put(25) get() put(12) get() get()

  18. Examples of Put and Get queue position start_index 7 3 end_index 30 2 - 1 25 0 put(15) put(20) get() put(30) put(7) put(25) get(): returns 20 put(12) get() get()

  19. Examples of Put and Get queue position start_index 7 3 end_index 30 2 12 1 25 0 put(15) put(20) get() put(30) put(7) put(25) get() put(12) get() get()

  20. Examples of Put and Get queue position start_index end_index 7 3 - 2 12 1 25 0 put(15) put(20) get() put(30) put(7) put(25) get() put(12) get(): returns 30 get()

  21. Examples of Put and Get queue position end_index - 3 - 2 start_index 12 1 25 0 put(15) put(20) get() put(30) put(7) put(25) get() put(12) get() get(): returns 7

  22. Book Implementation (Array-Based) static Item *q; static int N, head, tail; void QUEUEinit(intmaxN) { q = malloc((maxN+1)*sizeof(Item)); N = maxN+1; head = N; tail = 0; } intQUEUEempty() { return head % N == tail; } void QUEUEput(Item item) { q[tail++] = item; tail = tail % N; } Item QUEUEget() { head = head % N; return q[head++]; }

  23. Limitations of This Implementation ???

  24. Limitations of This Implementation • Same as for stacks. • Only one queue object can be used in the entire code. • Queues can only store objects of a single data type: • Specified in Item.h, where type Item is defined using a typedef. • It is easy to adapt the stacks_arrays.c code to obtain an array-based implementation of queues that does not have these two limitations. • Possible homework...

  25. More on Queues … Later Done with queues for now. We will see queues and queue-related topics quite a bit more in this course.

More Related