Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 3 Queues Queues1. queue: – Retrieves elements in the order they were added. – First-In, First-Out ("FIFO") – Elements are stored in order of insertion.

Similar presentations


Presentation on theme: "Lecture 3 Queues Queues1. queue: – Retrieves elements in the order they were added. – First-In, First-Out ("FIFO") – Elements are stored in order of insertion."— Presentation transcript:

1 Lecture 3 Queues Queues1

2 queue: – Retrieves elements in the order they were added. – First-In, First-Out ("FIFO") – Elements are stored in order of insertion but don't have indexes. – Client can only add to the end of the queue, and can only examine/remove the front of the queue.

3 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front of the queue Main queue operations: – enqueue(object): inserts an element at the end of the queue – object dequeue(): removes and returns the element at the front of the queue Auxiliary queue operations: – object front(): returns the element at the front without removing it – integer size(): returns the number of elements stored – boolean isEmpty(): indicates whether no elements are stored Exceptions – Attempting the execution of dequeue or front on an empty queue throws an EmptyQueueException Queues3

4 Queue Example Linked Lists4 front 5 53 3 37 7 7 9 97 97 973 9735 735 enqueue(5)- OperationOutput enqueue(3)- dequeue()5 enqueue(7)- dequeue()3 front()7 dequeue()7 dequeue()“error” isEmpty()true enqueue(9)- enqueue(7)- size()2 enqueue(3)- enqueue(5)- dequeue()9

5 Applications of Queues Direct applications – Waiting lists – Access to shared resources (e.g., printer) – Multiprogramming Indirect applications – Auxiliary data structure for algorithms – Component of other data structures Queues5

6 Array-based Queue Use an array of size N in a circular fashion Two variables keep track of the front and rear f index of the front element r index immediately past the rear element Array location r is kept empty Queues6 Q 012rf normal configuration Q 012fr wrapped-around configuration

7 Queue Operations We use the modulo operator (remainder of division) Queues7 Algorithm size() return (N  f + r) mod N Algorithm isEmpty() return (f  r) Q 012rf Q 012fr

8 Queue Operations (cont.) Operation enqueue throws an exception if the array is full This exception is implementation- dependent Queues8 Algorithm enqueue(o) if size() = N  1 then throw FullQueueException else Q[r]  o r  (r + 1) mod N Q 012rf Q 012fr

9 Queue Operations (cont.) Operation dequeue throws an exception if the queue is empty This exception is specified in the queue ADT Queues9 Algorithm dequeue() if isEmpty() then throw EmptyQueueException else o  Q[f] f  (f + 1) mod N return o Q 012rf Q 012fr

10 Queue Interface in Java Java interface corresponding to our Queue ADT Requires the definition of class EmptyQueueException No corresponding built-in Java class Queues10 public interface Queue { public int size(); public boolean isEmpty(); public Object front() throws EmptyQueueException; public void enqueue(Object o); public Object dequeue() throws EmptyQueueException; }

11 Application: Round Robin Schedulers We can implement a round robin scheduler using a queue, Q, by repeatedly performing the following steps: 1. e = Q.dequeue() 2. Service element e 3. Q.enqueue(e) Queues11 The Queue Shared Service 1.Deque the next element 3.Enqueue the serviced element 2.Service the next element

12 Summary of Queues Definition of Queues (basic) Array implementation Assignment 1: (due on Tuesday of Week 6) Implement the array-based Queue in Java assuming the interface is already given in Queue.java. Write a main method to have a Queue with the operations on side 4 of lecture 3. Hint: Please look at ArrayStack.java. Queues12

13 Part-B3 Linked Lists Linked Lists13

14 Introducing Linked Lists (continued) Think of each element in a linked list as being an individual piece in a child's pop chain. To form a chain, insert the connector into the back of the next piece © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

15 Introducing Linked Lists (continued) Inserting a new piece into the chain – breaking a connection and – reconnecting the chain at both ends of the new piece. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

16 Introducing Linked Lists (continued) Removal of a piece – breaking its two connections – removing the piece, and then reconnecting the chain. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

17 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores – element – link to the next node Linked Lists17 next elem node ABCD 

18 Inserting at the Head 1.Allocate a new node Linked Lists18  head 2. Update new element 3. Have new node point to old head 4. Update head to point to new node Baltimore Rome ScattleToronto  head Rome ScattleToronto Baltimore  head Rome ScattleToronto (a) (b) (c)

19 Removing at the Head 1.Update head to point to next node in the list Linked Lists19  head Baltimore Rome ScattleToronto  head Rome ScattleToronto Baltimore  head Rome ScattleToronto (c) (b) (a) 2.Allow garbage collector to reclaim the former first node

20 Inserting at the Tail Linked Lists20 1.Allocate a new node 2. Insert new element 3. Have new node point to null 4. Have old last node point to new node 5. Update tail to point to new node  head Rome ScattleToronto (a) tail  head Rome ScattleToronto (b) tail Zurich  head Rome ScattleToronto (c) tail  Zurich

21 Removing at the Tail Removing at the tail of a singly linked list is not efficient! There is no constant- time way to update the tail to point to the previous node Linked Lists21 The interface of data structure list is in List.java. The implementation is in NodeList.java. But it uses DNode.java. Actually, it is doubly linked list.

22 bat  cat  sat  vat NULL Linked Lists

23 bat  cat  sat  vat NULL mat  Insertion Compare this with the insertion in arrays!

24 bat  cat  sat  vat NULL mat  dangling reference Deletion

25 The Node Class for List Nodes (the file is source/Node.java) public classNode{ // Instance variables: private Object element; private Node next; /** Creates a node with null references to its element and next node. */ public Node(){ this(null, null); } /** Creates a node with the given element and next node. */ public Node(Object e, Node n) { element = e; next = n; } // Accessor methods: public Object getElement() { return element; } public Node getNext() { return next; } // Modifier methods: public void setElement(Object newElem) { element = newElem; } public void setNext(Node newNext) { next = newNext; } Linked Lists25

26 Stack with a Singly Linked List We can implement a stack with a singly linked list The top element is stored at the first node of the list The space used is O(n) and each operation of the Stack ADT takes O(1) time Linked Lists26  t nodes elements

27 Queue with a Singly Linked List We can implement a queue with a singly linked list – The front element is stored at the first node – The rear element is stored at the last node The space used is O(n) and each operation of the Queue ADT takes O(?) time Linked Lists27 f r  nodes elements

28 List ADT (§ 5.2.3) The List ADT models a sequence of positions storing arbitrary objects It establishes a before/after relation between positions Generic methods: – size(), isEmpty() Accessor methods: – first(), last() – prev(p), next(p) Update methods: – replace(p, e) – insertBefore(p, e), insertAfter(p, e), – insertFirst(e), insertLast(e) – remove(p) Linked Lists28

29 Doubly Linked List A doubly linked list provides a natural implementation of the List ADT Nodes implement Position and store: – element – link to the previous node – link to the next node Special trailer and header nodes Linked Lists29 prevnext elem trailer header nodes/positions elements node

30 Insertion We visualize operation insertAfter(p, X), which returns position q Linked Lists30 ABXC ABC p ABC p X q pq

31 Insertion Algorithm Algorithm insertAfter(p,e): Create a new node v v.setElement(e) v.setPrev(p){link v to its predecessor} v.setNext(p.getNext()){link v to its successor} (p.getNext()).setPrev(v){link p’s old successor to v} p.setNext(v){link p to its new successor, v} return v{the position for the element e} Linked Lists31

32 Deletion We visualize remove(p), where p = last() Linked Lists32 ABCD p ABC D p ABC

33 Deletion Algorithm Algorithm remove(p): t = p.element{a temporary variable to hold the return value} (p.getPrev()).setNext(p.getNext()){linking out p} (p.getNext()).setPrev(p.getPrev()) p.setPrev(null){invalidating the position p} p.setNext(null) return t Linked Lists33

34 Insertion of an Element at the Head

35 Have a new node: header RomeSeattle trailer Baltimore Toronto

36 Update the links: header RomeSeattle trailer Baltimore Toronto

37

38 Deleting an Element at the Tail

39 Update the links: header Rome Seattle trailer BaltimoreToronto

40

41 Performance In the implementation of the List ADT by means of a doubly linked list – The space used by a list with n elements is O(n) – The space used by each position of the list is O(1) – All the operations of the List ADT run in O(1) time – Operation element() of the Position ADT runs in O(1) time Linked Lists 41


Download ppt "Lecture 3 Queues Queues1. queue: – Retrieves elements in the order they were added. – First-In, First-Out ("FIFO") – Elements are stored in order of insertion."

Similar presentations


Ads by Google