Presentation is loading. Please wait.

Presentation is loading. Please wait.

FIFO Queues CSE 2320 – Algorithms and Data Structures Alexandra Stefan

Similar presentations


Presentation on theme: "FIFO Queues CSE 2320 – Algorithms and Data Structures Alexandra Stefan"— Presentation transcript:

1 FIFO Queues CSE 2320 – Algorithms and Data Structures Alexandra Stefan
University of Texas at Arlington

2 FIFO Queues First-in first-out (FIFO) queues.
Examples of usage 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 Main operations: insert, put - puts an item "at the end of the line". delete, get - removes the item from "the head of the line". 2 implementations for FIFO queues: lists & arrays

3 List Implementation for FIFO Queues
A FIFO queue is essentially a list. put(queue, item) inserts that item at the end of the list. - O(1) Assumption: the list data type contains a pointer to the last element. get(queue) removes (and returns) the item at the beginning of the list O(1) struct queue_list { link firstD; // dummy link last; }; my_queue 3000 1000 first (link) 3000 last (link) 1000 7 3 .... 1

4 Array Implementation for FIFO Queues
typedef struct queue_struct * queue; struct queue_struct { int max_size; int start_index; int end_index; int * items; }; end_index tells us where to put a new item. start_index tells us where to remove an item from.

5 Array-Based Queue: Example
typedef struct queue_struct * queue; struct queue_struct { int max_size; int start_index; int end_index; int * items; }; Conventions: place where the new item will be added (end_index). underline: first item in the queue (start_index). x – put(x) * – get() put(15) put(20) get() put(30) put(7) put(25) put(12) 15 20 15 20 15 20 30 7 25 12 * * * * 15 20 30 7

6 Array-Based Queue: Example
typedef struct queue_struct * queue; struct queue_struct { int max_size; int start_index; int end_index; int * items; }; Conventions: place where the new item will be added (end_index). underline: first item in the queue (start_index). x – put(x) * – get() put(15) put(20) get() put(30) put(7) put(25) put(12) empty full full 15 20 15 20 30 20 7 30 20 7 30 20 25 7 30 25 7 30 12 25 7 12 25 12 25 15 20 30 7 25 12 * * * * 15 20 30 7


Download ppt "FIFO Queues CSE 2320 – Algorithms and Data Structures Alexandra Stefan"

Similar presentations


Ads by Google