Download presentation

Presentation is loading. Please wait.

Published byBrianna Gonzalez Modified over 3 years ago

3
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received

4
Properties of queues First In First Out (FIFO) Data added at one end only (the Tail of the queue) Data removed at other end only (the Head of the queue)

5
Queue operations Initialise queue Add item (to tail of queue) Remove item (from head of queue) Check if queue empty Check if queue full

6
Queues using arrays Use array to store queue elements Define a data item Head which identifies the item at Head of queue Define a data item Tail which identifies the first empty location after last item in queue Tail identifies location where next item is placed in queue

7
Queues using arrays (first try) Head Tail Empty queue (Tail == Head)

8
Queues using arrays (first try) Head Tail Add item T at location Tail T

9
Queues using arrays (first try) Head Tail Add item H at location Tail TH

10
Queues using arrays (first try) Head Tail Add item I at location Tail THI

11
Queues using arrays (first try) Head Tail Remove item T from Head HI

12
Queues using arrays (first try) Head Tail Continue until Tail == ArraySize Queue full? AQUEUE

13
Queues using arrays (first try) Head Tail Must shift queue contents back to start of array - inefficient! AQUEUE

14
Circular Queue Use a circular queue Consider (perceive?) the array as a circular structure (i.e. as if the last element of the array is connected/joined to the first element of the array) The benefit of this insight is that we never have to shift data

15
A circular array 0 1 2 MaxSize - 1 MaxSize - 2 MaxSize - 3

16
A queue using a circular array MaxSize - 1 Tail Head Empty queue Tail == Head 0

17
A queue using a circular array T MaxSize - 1 Tail Head Add T at Tail Tail = (Tail + 1) % MaxSize 0

18
A queue using a circular array T H MaxSize - 1 Tail Head Add H at Tail Tail = (Tail +1) % MaxSize 0

19
A queue using a circular array T H MaxSize - 1 Tail Head Add I at Tail Tail = (Tail +1) % MaxSize 0 I

20
A queue using a circular array H MaxSize - 1 Tail Head Remove T from Head Head = (Head +1) % MaxSize 0 I

21
A queue using a circular array MaxSize - 1 Tail Head Continue until Tail == MaxSize - 1 0 E U E U Q A

22
A queue using a circular array MaxSize - 1 Tail Head 0 E U E U Q A Add Z at Tail Tail = (Tail +1) % MaxSize i.e. [(MaxSize - 1) + 1] % MaxSize = MaxSize % MaxSize = 0 Z

23
Empty and Full Queue Tests Empty queue condition: Head = = Tail Full queue condition: (Tail + 1) % MaxSize = = Head

24
Queue ADT in Java Constructor isempty isfull Join Leave

25
Constructor public QueueOfInts() {Queue = new int[10] ; Head = Tail = 0 ; } public QueueOfInts(int capacity) {Queue = new int[capacity] ; Head = Tail = 0 ; }

26
Test for empty queue public boolean isempty() { return Head == Tail) } }

27
Test for full queue public boolean isfull() { if ((Tail + 1) % Queue.length == Head) { return true; } else { return false; }

28
Adding an element public void Join(int val) {Queue[Tail] = val ; Tail = (Tail + 1) % Queue.length; }

29
Remove an element public int Leave() {int Removed ; Removed = Queue[Head]; Head = (Head + 1) % Queue.length ; return Removed ; }

Similar presentations

OK

1 Queues – Chapter 3 A queue is a data structure in which all additions are made at one end called the rear of the queue and all deletions are made from.

1 Queues – Chapter 3 A queue is a data structure in which all additions are made at one end called the rear of the queue and all deletions are made from.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on tamper resistant screws Ppt on biotics supplements English ppt on figures of speech Converter pub to ppt online shopping Ppt on california academy of sciences Ppt on pizza hut india Maths ppt on algebraic identities Ppt on nature and history of democracy in nepal s Ppt on needle stick injury treatment Ppt on school library management system