fifo queues cse 2320 – algorithms and data structures vassilis athitsos university of texas at...

25
FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

Upload: hadley-godard

Post on 30-Mar-2015

220 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

1

FIFO Queues

CSE 2320 – Algorithms and Data StructuresVassilis Athitsos

University of Texas at Arlington

Page 2: FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

2

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:

Page 3: FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

3

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:• 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

Page 4: FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

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?

Page 5: FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

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?

Page 6: FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

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.

Page 7: FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

7

Queue Versus Stack Implementation

• Implementation-wise, compare:– list-based queue implementation.– list-based stack implementation.

• What are the key differences?

Page 8: FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

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.

Page 9: FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

9

FIFO Queues Using Arrays· How do we implement queues using arrays?· The stack definition looked like this:typedef struct stack_struct * Stack;struct stack_struct{ int max_size; int top_index; void ** items;};

· What changes do we need to accommodate queues?

Page 10: FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

10

FIFO Queues Using Arrays· How do we implement queues using arrays?· A queue can be defined like this:typedef struct queue_struct * queue;struct queue_struct{ int max_size; int start_index; int end_index; void ** items;};

· end_index tells us where to put a new item.· start_index tells us where to remove an item from.

Page 11: FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

11

Examples of Put and Get

• put(15)• put(20)• get()• put(30)• put(7)• put(25)• get()• put(12)• get()• get()

----

queue position

0123

start_index

end_index

Page 12: FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

12

Examples of Put and Get

• put(15)• put(20)• get()• put(30)• put(7)• put(25)• get()• put(12)• get()• get()

15---

queue position

0123

start_index

end_index

Page 13: FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

13

Examples of Put and Get

• put(15)• put(20)• get() • put(30)• put(7)• put(25)• get()• put(12)• get()• get()

1520--

queue position

0123

start_index

end_index

Page 14: FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

14

Examples of Put and Get

• put(15)• put(20)• get(): returns 15• put(30)• put(7)• put(25)• get()• put(12)• get()• get()

-20--

queue position

0123

start_indexend_index

Page 15: FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

15

Examples of Put and Get

• put(15)• put(20)• get()• put(30)• put(7)• put(25)• get()• put(12)• get()• get()

-2030-

queue position

0123

start_index

end_index

Page 16: FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

16

Examples of Put and Get

• put(15)• put(20)• get()• put(30)• put(7)• put(25)• get()• put(12)• get()• get()

-20307

queue position

0123

start_index

end_index

Page 17: FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

17

Examples of Put and Get

• put(15)• put(20)• get()• put(30)• put(7)• put(25)• get()• put(12)• get()• get()

2520307

queue position

0123

start_index

end_index

Page 18: FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

18

Examples of Put and Get

• put(15)• put(20)• get()• put(30)• put(7)• put(25)• get(): returns 20• put(12)• get()• get()

25-

307

queue position

0123

start_index

end_index

Page 19: FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

19

Examples of Put and Get

• put(15)• put(20)• get()• put(30)• put(7)• put(25)• get()• put(12)• get()• get()

2512307

queue position

0123

start_index

end_index

Page 20: FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

20

Examples of Put and Get

• put(15)• put(20)• get()• put(30)• put(7)• put(25)• get()• put(12)• get(): returns 30• get()

2512-7

queue position

0123start_index

end_index

Page 21: FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

21

Examples of Put and Get

• put(15)• put(20)• get()• put(30)• put(7)• put(25)• get()• put(12)• get()• get(): returns 7

2512--

queue position

0123

start_index

end_index

Page 22: FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

22

Book Implementation (Array-Based)static Item *q;static int N, head, tail;

void QUEUEinit(int maxN) { q = malloc((maxN+1)*sizeof(Item)); N = maxN+1; head = N; tail = 0; }int QUEUEempty() { return head % N == tail; }void QUEUEput(Item item) { q[tail++] = item; tail = tail % N; }Item QUEUEget() { head = head % N; return q[head++]; }

Page 23: FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

23

Limitations of This Implementation

• ???

Page 24: FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

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...

Page 25: FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1

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.