Queue and Its Implementation Tessema M. Mengistu Department of - - PowerPoint PPT Presentation

queue and its implementation
SMART_READER_LITE
LIVE PREVIEW

Queue and Its Implementation Tessema M. Mengistu Department of - - PowerPoint PPT Presentation

Queue and Its Implementation Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale tessema.mengistu@siu.edu Room - 3131 1 Outline Queue ADT Linked List Based Implementation Array Based


slide-1
SLIDE 1

Queue and Its Implementation

1

Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale tessema.mengistu@siu.edu Room - 3131

slide-2
SLIDE 2

Outline

  • Queue ADT
  • Linked List Based Implementation
  • Array Based Implementation
  • Vector Based Implementation
  • Variants of Queue

– Double Ended Queue - Deque – Priority Queue

slide-3
SLIDE 3

Queue ADT

  • Another name for a waiting line
  • Organizes its entries according to the order in

which they were added

  • Has a characteristics of First in, first out (FIFO)

– The first element entered the queue is the first element to be processed

  • Has two ends – back (rear) and front
slide-4
SLIDE 4

Queue

  • All additions to a queue are at its back (rear)

– Called enqueue – The recent item added

  • All removal from the queue is at its front

– Called dequeue

  • The earliest item added
slide-5
SLIDE 5

Queue

  • Used within operating systems
  • Simulate many real world events
slide-6
SLIDE 6

ADT Queue

  • Data

– A collection of objects in chronological order and having the same data type

  • Operations

– enqueue(newEntry):void – dequeue():T – getFront():T – isEmpty():boolean – clear():void

slide-7
SLIDE 7
slide-8
SLIDE 8

Example

slide-9
SLIDE 9
slide-10
SLIDE 10

Java Class Library

  • Interface Queue

– public boolean add(T newEntry) – public boolean offer(T newEntry) – public T remove() – public T poll() – public T element() – public T peek() – public boolean isEmpty() – public void clear() – public int size()

slide-11
SLIDE 11

Linked Implementation of a Queue

  • Consider chain of linked nodes

– Head reference insufficient – Must also have tail reference

  • Which should be front of queue?

– Head easier to be front of queue for entry removal – Adding entries at tail/back of queue easily done

slide-12
SLIDE 12
slide-13
SLIDE 13

enqueue Method

  • If empty
slide-14
SLIDE 14

enqueue Method

  • If not empty
slide-15
SLIDE 15

enqueue Method

slide-16
SLIDE 16

dequeue Method

  • Only one element
slide-17
SLIDE 17

dequeue Method

  • More than one Elements
slide-18
SLIDE 18

dequeue Method

slide-19
SLIDE 19

Other Methods

slide-20
SLIDE 20

Array-Based Implementation

  • f a Queue
  • Array named queue

– queue[0] is front – frontIndex, backIndex are indices of front and back of queue

slide-21
SLIDE 21

Array Based Implementation …

  • What happens during dequeue?

– With queue[0] always as front, must shift elements

  • Not efficient

– Instead, move frontIndex

slide-22
SLIDE 22

Array Based Implementation …

  • Then we run off the end of the array!?
  • Solution ?
  • Expand?

– left many spaces unoccupied

  • Use unoccupied spaces
slide-23
SLIDE 23

Array Based Implementation …

  • Once the queue reaches the end of the array,,

we can add subsequent entries to the queue at the beginning of the array.

  • The array behave as circular

– Its first location follows its last one

slide-24
SLIDE 24

Array Based Implementation …

  • Increment indices with modulo operator

backIndex = (backIndex + 1) % queue.length; frontIndex =(frontIndex + 1)% queue.length;

slide-25
SLIDE 25

Array Based Implementation …

slide-26
SLIDE 26

Array Based Implementation …

  • How do we know the queue is full?

fronIndex = backIndex + 1

slide-27
SLIDE 27

Array Based Implementation …

  • How do we know the queue is Empty?

frontIndex = backIndex + 1

slide-28
SLIDE 28

Array Based Implementation …

  • Problem

– No way to decide whether the queue is empty or full using index

  • Solution

– Have a counter variable and test the variable

  • The enqueue and dequeue methods should manipulate

this variable – inefficient

– Leave one array location unused

slide-29
SLIDE 29

Circular Array with One Unused Element

  • Allows detection of empty Vs. full queue

– Examine frontIndex, backIndex

slide-30
SLIDE 30

Circular Array …

  • Any pattern?

– full frontIndex = (backIndex + 2) % queue.length – Empty frontIndex = (backIndex + 1) % queue.length

slide-31
SLIDE 31

Circular Array …

slide-32
SLIDE 32

dequeue Method

slide-33
SLIDE 33

dequeue Method

slide-34
SLIDE 34

getFront Method

slide-35
SLIDE 35

enqueue Method

  • ensureCapacity() – reading assignment
slide-36
SLIDE 36

Other Methods

public void clear() { while(!isEmpty()) dequeue(); }

slide-37
SLIDE 37

Vector Based Implementation of a Queue

  • Front of queue at beginning of vector
  • Vector add method used at back of queue
  • Remove from front of queue

– Vector takes care of moving elements – No indices needed

  • Vector manages additional space as needed
slide-38
SLIDE 38
slide-39
SLIDE 39

Vector Based Implementation …

  • enqueue method
  • getFront method
slide-40
SLIDE 40

Vector Based Implementation …

  • dequeue method
  • isEmpty method
  • clear method
slide-41
SLIDE 41

Efficiency of Vector Based Implementation

  • Since we add entries to one end of a queue

and remove them from the other end, the vector implementation inherently moves its entries after each removal.

– dequeue() is O(n) – Other methods O(1)

slide-42
SLIDE 42
  • Exercise

– Create a queue that can contain Strings – Add 5 strings to the queue – Remove the first two strings from the queue – Add additional three strings – Display the content of the queue

slide-43
SLIDE 43

Double Ended Queue

  • Allows add, remove, or retrieve entries at

both the front and back of a queue

  • In short called deque – pronounced as “deck”
  • Has queue like operations and stack like
  • perations

– addToBack() and removeFront() – queue – addToFront() and removeFront() – stack – getFront(), getBack(), and removeBack()

slide-44
SLIDE 44

Deque ADT

slide-45
SLIDE 45

Deque …

slide-46
SLIDE 46

Deque …

  • Output?
slide-47
SLIDE 47

Doubly Linked Implementation of a Deque

  • We need a way to traverse the liked nodes

from both ends

– Doubly linked list

slide-48
SLIDE 48

Doubly Linked …

slide-49
SLIDE 49

addToBack() Method

slide-50
SLIDE 50

addToBack() Method

slide-51
SLIDE 51

addToFront() Method

slide-52
SLIDE 52

removeFront() Method

slide-53
SLIDE 53

removeFront() Method

slide-54
SLIDE 54

removeBack() Method

slide-55
SLIDE 55

Other Methods

  • getFront()
  • getBack()?
slide-56
SLIDE 56

Other Methods

  • Better clear() implementation??
slide-57
SLIDE 57

Java Class Library

  • Interface Deque
  • extends Queue

– public void addFirst(T newEntry) – public boolean offerFirst(T newEntry) – public void addLast(T newEntry) – public boolean offerLast(T newEntry) – public T removeFirst() – public T pollFirst() – public T removeLast() – public T pollLast() – public T getFirst() – public T peekFirst() – public T getLast() – Public T peekLast() – public boolean isEmpty() – public void clear() – public int size()

slide-58
SLIDE 58

Java Class Library

  • Class ArrayDeque

– Implements Deque

  • Note – has methods appropriate for deque,

queue, and stack

– Could be used for instances of any of these

  • Constructors

– public ArrayDeque() – public ArrayDeque(int initialCapacity)

slide-59
SLIDE 59

Priority Queue

  • Organizes objects according to their priorities
  • Example

– Bank Vs Hospital ER

  • What exactly is a priority depends on the

context of the application

  • By making the objects Comparable, we can

hide this detail in the objects’ method compareTo

slide-60
SLIDE 60
slide-61
SLIDE 61

Priority Queue

  • Example
slide-62
SLIDE 62

Priority Queue

  • Priority can be implemented using Array,

linked List, or Vector

  • If a linked chain contains the entries in a

priority queue, the entry with the highest priority should occur at the beginning of the chain, where it is easy to remove

slide-63
SLIDE 63

Java Class Library

  • Class PriorityQueue constructors and methods

– public PriorityQueue() – public PriorityQueue(int initialCapacity) – public boolean add(T newEntry) – public boolean offer(T newEntry) – public T remove() – public T poll() – public T element() – public T peek() – public boolean isEmpty() – public void clear() – public int size()