Presentation is loading. Please wait.

Presentation is loading. Please wait.

ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.

Similar presentations


Presentation on theme: "ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel."— Presentation transcript:

1 ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel Rodriguez – All rights reserved

2 Lecture Organization Part I – Introduction to the Queue ADT Part II – Design and implementation of queue using doubly linked lists Part III – Design and implementation of queue using dynamic arrays M. Rodriguez-MartinezICOM 4035 2

3 Objectives Introduce the concept of a Queue – First-In First-Out (FIFO) list Discuss the application of queues Understand the design and implementation of a queues using arrays and linked lists Provide motivating examples ICOM 4035M. Rodriguez-Martinez 3

4 Companion videos Lecture10 videos – Contains the coding process associated with this lecture – Shows how to build the interfaces, concrete classes, and factory classes mentioned here M. Rodriguez-MartinezICOM 4035 4

5 Part I Introduction to the Queue ADT M. Rodriguez-MartinezICOM 4035 5

6 Queue ADT Queue: – collection of things with restriction on access Elements are added at the end and removed from the front First element in must be first element out No notion of specific position for elements other than element at the front – repetitions are allowed M. Rodriguez-MartinezICOM 4035 6 Queue of people Queue of names Apu Ned Ron Queue of cars

7 Structure of a Queue M. Rodriguez-MartinezICOM 4035 7 Apu Ned Ron End of queue Front of queue Apu Ned Ron Adding Amy to Queue Amy End of queue Front of queue Removing Ron from Queue Apu Ned Ron End of queue Front of queue Element are always: Added at the end Removed from the front Queue: First-In First-Out (FIFO) List

8 Queue ADT Operations size() – number of elements in queue isEmpty() – is the queue empty front() – inspect element at the front of the queue without removing it enqueue() – add a new element at the end of the queue dequeue() – remove and returns element at the front of the queue makeEmpty() –remove all elements from queue M. Rodriguez-MartinezICOM 4035 8

9 Queue operations: front() M. Rodriguez-MartinezICOM 4035 9 Q.front() Returns: “Ron” front() – inspects and returns the element at the front of the queue. No modification is made on queue. Apu Ned Ron Q= Apu Ned Ron Q= End of queue Front of queue End of queue Front of queue

10 Queue operations: dequeue() M. Rodriguez-MartinezICOM 4035 10 Q.dequeue() Returns: “Ron” dequeue() – removes and returns the element at the front of the queue. Size is decreased by one. Apu Ned Ron Q= Apu Ned Ron Q= End of queue Front of queue End of queue Front of queue

11 Queue operations: enqueue() M. Rodriguez-MartinezICOM 4035 11 Q.enqueue(“Jil”) Returns: nothing enqueue() – adds a new element at the end of the queue. Size is increased by one. Apu Ned Ron Q= Apu Ned Ron Q= End of queue Front of queue End of queue Front of queue Jil

12 Uses for Queue (1) Networks – queues are used by routers to hold data packets until ready for forwarding M. Rodriguez-MartinezICOM 4035 12

13 Uses for Queue (2) Print queue for shared printers – hold documents until ready for printing M. Rodriguez-MartinezICOM 4035 13

14 Part II Design and implementation of queue using doubly linked list M. Rodriguez-MartinezICOM 4035 14

15 Queues and doubly linked lists M. Rodriguez-MartinezICOM 4035 15 Apu Ned Ron Q= End of queue Front of queue Ron Ned header End of queue Front of queue Apu tail

16 Key idea: use header and tail Use header to point to the front of queue Use tail to point to last element of queue currentSize – number of elements M. Rodriguez-MartinezICOM 4035 16 Ron Ned header End of queue Front of queue Apu tail currentSize = 4

17 Sample cases M. Rodriguez-MartinezICOM 4035 17 Ron Ned header End of queue Front of queue Apu tail currentSize = 4 header tail Empty queue currentSize = 0 header tail One element queue currentSize = 1 Apu Multi element queue

18 Queue operations: front() M. Rodriguez-MartinezICOM 4035 18 Q.front() Returns: “Ron” front() – inspects and returns the element at the front of the queue. No modification is made on queue. Apu Ned Ron Q= Apu Ned Ron Q= End of queue Front of queue End of queue Front of queue

19 Queue operations: front() (2) If not empty – return header.getNext().getValue() Null if empty Complexity: O(1) – Simply follow the references M. Rodriguez-MartinezICOM 4035 19 Ron Ned header End of queue Front of queue Apu tail currentSize = 4

20 Queue operations: enqueue() M. Rodriguez-MartinezICOM 4035 20 Q.enqueue(“Jil”) Returns: nothing enqueue() – adds a new element at the end of the queue. Size is increased by one. Apu Ned Ron Q= Apu Ned Ron Q= End of queue Front of queue End of queue Front of queue Jil

21 Queue operations: enqueue() (2) M. Rodriguez-MartinezICOM 4035 21 Ron Ned header End of queue Front of queue Apu tail currentSize = Jil 4 5

22 Queue operations: enqueue() (3) Add a new element before the tail – tail.setPrev(newNode); Increment with currentSize++ Complexity: O(1) – Simply manipulate the references and int value M. Rodriguez-MartinezICOM 4035 22 Ron Ned header End of queue Front of queue tail currentSize = 5 JilApu

23 Queue operations: dequeue() M. Rodriguez-MartinezICOM 4035 23 Q.dequeue() Returns: “Ron” dequeue() – removes and returns the element at the front of the queue. Size is decreased by one. Apu Ned Ron Q= Apu Ned Ron Q= End of queue Front of queue End of queue Front of queue

24 Queue operations: dequeue() (2) M. Rodriguez-MartinezICOM 4035 24 Ron Ned header End of queue Front of queue Apu tail currentSize= 4 3

25 Queue operations: dequeue() (3) If not empty – result = header.getNext().getValue() – Change header with header.setNext(header.getNext().getNext()) – Decrement current size with currentSize-- Null if empty Complexity: O(1) – Simply follow the references M. Rodriguez-MartinezICOM 4035 25 RonNed header End of queue Front of queue Apu tail currentSize = 3

26 Easy operations size() – Return value of variable currentSize – Complexity: O(1) isEmpty() – Return size() == 0 – Complexity: O(1) makeEmpty() – Call dequeue() while queue is not empty – Complexity: O(n), n = Q.size() M. Rodriguez-MartinezICOM 4035 26

27 Part III Design and implementation of queue using arrays M. Rodriguez-MartinezICOM 4035 27

28 Arrays and queue We have always used arrays as linear entities M. Rodriguez-MartinezICOM 4035 28 Ron NedApu 0 1 2 3 4 5 unused in use currentSize: 4

29 Problem: mapping queue to array M. Rodriguez-MartinezICOM 4035 29 Apu Ned Ron Q= End of queue Front of queue Ron NedApu 0 1 2 3 4 5 unused in use End of queue Front of queue This scheme appears to work until we dequeue

30 Problem: dequeue M. Rodriguez-MartinezICOM 4035 30 Apu Ned Ron Q= End of queue Front of queue RonNedApu 0 1 2 3 4 5 unused in use End of queue Front of queue No we get holes in the array unused

31 Problem: dequeue (2) M. Rodriguez-MartinezICOM 4035 31 Apu Ned Ron Q= End of queue Front of queue RonNedApu 0 1 2 3 4 5 unused in use End of queue Front of queue One option is to move all elements to the left, but it makes queue very inefficient

32 Notion of circular array M. Rodriguez-MartinezICOM 4035 32 Apu Ned Ron Q= End of queue Front of queue Ron NedApu 0 1 2 3 4 5 unused in use End of queue Front of queue end 0 1 2 3 Ron Ned Apu 4 front 5 Suppose we could bend the array to connect the end with the start We can now wrap around numbers! Go from 5 to 0 again

33 Notion of circular array (2) Front – Keep track of current element at front End – Keeps track of next available position for end currentSize – Current size of queue M. Rodriguez-MartinezICOM 4035 33 Apu Ned Ron Q= End of queue Front of queue end 0 1 2 3 Ron Ned Apu 4 front 5 currentSize = 4

34 Notion of circular array (3) As element are added or removed simply move the variables ahead (increment by one) Use modulo math for this Enables wrap around M. Rodriguez-MartinezICOM 4035 34 Apu Ned Ron Q= End of queue Front of queue end 0 1 2 3 Ron Ned Apu 4 front 5 currentSize = 3

35 Notion of circular array (4) As element are added or removed simply move the variables ahead (increment by one) Use modulo math for this Enables wrap around M. Rodriguez-MartinezICOM 4035 35 Li Mel Jil Q= End of queue Front of queue end 0 1 2 3 Mel Jil 4 front 5 currentSize = 3 Li

36 Modulo math Remember f(n,i) = n mod i – Remainder after dividing n by i – Range of function is set {0, 1, 2, …, i-1} Results are cyclic: – 0 mod 3 = 0 – 1 mod 3 = 1 – 2 mod 3 = 2 – 3 mod 3 = 0 – 4 mod 3 = 1 – 5 mod 3 = 2... M. Rodriguez-MartinezICOM 4035 36

37 Operating on circular array M. Rodriguez-MartinezICOM 4035 37 0 1 2 3 Ron Ned Apu 4 front end 0 1 2 3 Ron Ned Apu 4 front end Q.dequeue() dequeue moves the front forward

38 Notion of circular array (2) M. Rodriguez-MartinezICOM 4035 38 Enqueue moves the end forward Re-allocate array when size() = elements.length -1 0 1 2 3 Ron Ned Apu 4 front end 0 1 2 3 Ron Ned Apu 4 front end Q.enqueue(Li) Ron Li 5

39 Queue operations: front() M. Rodriguez-MartinezICOM 4035 39 Q.front() Returns: “Ron” front() – inspects and returns the element at the front of the queue. No modification is made on queue. Apu Ned Ron Q= Apu Ned Ron Q= End of queue Front of queue End of queue Front of queue

40 Queue operations: front (2) If not empty, simply return element at front Complexity: O(1) – Random access to array element M. Rodriguez-MartinezICOM 4035 40 end 0 1 2 3 Ron Ned Apu 4 front 5 currentSize = 4

41 Queue operations: enqueue() M. Rodriguez-MartinezICOM 4035 41 Q.enqueue(“Jil”) Returns: nothing enqueue() – adds a new element at the end of the queue. Size is increased by one. Apu Ned Ron Q= Apu Ned Ron Q= End of queue Front of queue End of queue Front of queue Jil

42 Queue operations: enqueue(2) Add element at end Update end as: – end = (end + 1) mod N, wher N = array length Complexity: O(n), n = Q.size() – If need to reallocate M. Rodriguez-MartinezICOM 4035 42 end 0 1 2 3 Ron Ned Apu 4 front 5 currentSize = 4 Jil end currentSize = 5

43 Queue operations: enqueue(3) Add Xi to this example causes wrap around end will become 0 – (5 +1) mod 6 = 0 M. Rodriguez-MartinezICOM 4035 43 0 1 2 3 Ron Ned Apu 4 front 5 currentSize = 4 Jil end Xi end currentSize = 5

44 Queue operations: dequeue() M. Rodriguez-MartinezICOM 4035 44 Q.dequeue() Returns: “Ron” dequeue() – removes and returns the element at the front of the queue. Size is decreased by one. Apu Ned Ron Q= Apu Ned Ron Q= End of queue Front of queue End of queue Front of queue

45 Queue operations: dequeue(2) remove element at front Update front as: – front = (front+ 1) mod N, wher N = array length Complexity: O(1) – Only need to manipulate numeric value M. Rodriguez-MartinezICOM 4035 45 end 0 1 2 3 Ron Ned Apu 4 front 5 currentSize = 4 Jil end front

46 Easy operations size() – Return value of variable top – Complexity: O(1) isEmpty() – Return size() == 0 Alternative: front == end – Complexity: O(1) makeEmpty() – Call pop() while queue is not empty – Complexity: O(n), n = Q.size() M. Rodriguez-MartinezICOM 4035 46

47 Summary Introduced the concept of a Queue – First-In First-Out (FIFO) list – Elements are added to the front – Elements are removed from the end Discuss the application of queues – Forwarding data in network routers – Print queues in shared printers Describe the design and implementation with – doubly linked lists – arrays M. Rodriguez-MartinezICOM 4035 47

48 Questions? Email: – manuel.rodriguez7@upr.edu ICOM 4035M. Rodriguez-Martinez 48


Download ppt "ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel."

Similar presentations


Ads by Google