Queues - PowerPoint PPT Presentation

About This Presentation
Title:

Queues

Description:

Queues What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples? Queue ADT Operations enqueue(o ... – PowerPoint PPT presentation

Number of Views:154
Avg rating:3.0/5.0
Slides: 17
Provided by: usfcaEdu
Learn more at: https://www.cs.usfca.edu
Category:
Tags: queues

less

Transcript and Presenter's Notes

Title: Queues


1
Queues
2
What is a queue?
  • First-in first-out data structure (FIFO)
  • New objects are placed at rear
  • Removal restricted to front
  • Examples?

3
Queue ADT Operations
  • enqueue(o) Insert o at rear of queue
  • Input Object Output None
  • dequeue() Remove object at front error if empty
  • Input None Output Object removed
  • size() Return number of objects in queue
  • Input None Output Integer
  • isEmpty() Return a boolean indicating queue
    empty
  • Input None Output Boolean
  • first() Return object at front without removing
    error if empty
  • Input None Output Object

4
Example
  • enqueue(5)
  • enqueue(3)
  • dequeue()
  • enqueue(7)
  • dequeue()
  • front()
  • dequeue()
  • dequeue()
  • isEmpty()
  • enqueue(9)
  • enqueue(7)
  • size()
  • enqueue(3)
  • enqueue(5)
  • dequeue()

5
Queue Interface
  • int size()
  • bool isEmpty()
  • Object front() throws QueueEmptyException
  • void enqueue(Object obj)
  • Object dequeue() throws QueueEmptyException

6
Underlying Representation
  • Array versus Linked List
  • Pros and cons?
  • Running time?
  • size
  • isEmpty
  • enqueue
  • dequeue
  • front

7
Array Implementation
0
1
2
3
4
5
6

n-1
enqueue(5) enqueue(3)
5
3
0
1
2
3
4
5
6

n-1
dequeue() ?
8
Array Implementation
0
1
2
3
4
5
6

n-1
enqueue(5) enqueue(3)
5
3
0
1
2
3
4
5
6

n-1
3
dequeue() ?
0
1
2
3
4
5
6

n-1
9
Circular Array
f
r
0
1
2
3
4
5
6

n-1
  • f stores index of cell which stores first
    element of queue
  • r stores index of next available cell in queue
  • Initially, f r 0
  • How do you add a new element?
  • How do you remove an element?

10
Circular Array
f
r
0
1
2
3
4
5
6

n-1
  • How do you add a new element?
  • insert at arrayr
  • increment r
  • How do you remove an element?
  • return arrayf
  • increment f
  • What happens when r gt n-1?

11
Circular Array
f
r
0
1
2
3
4
5
6

n-1
  • Need to be able to wrap around
  • Modulo
  • increment f using (f1)n
  • increment r using (r1)n

12
Circular Array
r
f
f
dequeue
0
1
2
0
1
2
r
f
r
f
r (21)3 0
enqueue
enqueue
0
1
2
0
1
2
r
f
enqueue
0
1
2
13
Algorithms
  • size
  • return (N-fr) mod N
  • isEmpty
  • return (f r)
  • front
  • if isEmpty then throw QueueEmptyException
  • return arrayf
  • dequeue
  • if isEmpty then throw QueueEmptyException
  • temp arrayf
  • f (f1)N
  • return temp
  • enqueue
  • if size N-1 then throw QueueFullException SIZE
    MUST BE lt N-1
  • arrayr object
  • r (r1)N

14
Deque
  • Double-ended queue
  • insertFirst
  • insertLast
  • removeFirst
  • removeLast
  • first
  • last
  • size
  • isEmpty

15
Example
  • insertFirst(3)
  • insertFirst(5)
  • first()
  • removeFirst()
  • insertLast(7)
  • last()
  • removeFirst()
  • removeLast()

16
Doubly Linked List
Object3
Object2
Object1
header
prev
prev
prev
trailer
next
next
next
  • Algorithms
  • insertFirst
  • removeLast
Write a Comment
User Comments (0)
About PowerShow.com