Presentation is loading. Please wait.

Presentation is loading. Please wait.

Queues CSC212.

Similar presentations


Presentation on theme: "Queues CSC212."— Presentation transcript:

1 Queues CSC212

2 Specifications for the ADT Queue
Queue organizes entries according to order of entry Exhibits first-in, first-out behavior Contrast to stack which is last-in, first-out All additions are at the back of the queue Front of queue has items added first Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X

3 Specifications for the ADT Queue
Fig Some everyday queues. Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X

4 Specifications for the ADT Queue
View interface for a queue of objects Note methods enqueue dequeue getFront isEmpty clear Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X

5 Specifications for the ADT Queue
Fig Queue of strings after (a) enqueue adds Jim; (b) Jess; (c) Jill; (d) Jane; Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X

6 Specifications for the ADT Queue
Fig Queue of strings after (e) enqueue adds Joe; (f) dequeue retrieves, removes Jim; (g) enqueue adds Jerry; (h) dequeue retrieves, removes Jess. Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X

7 Using a Queue to Simulate a Waiting Line
Fig A line, or queue of people. Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X

8 Java Class Library: The Interface Queue
Similar to our QueueInterface Specifies more methods Methods provided Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X

9 import java.util.LinkedList;
import java.util.Queue; public class queu { public static void main(String[] args) { // Create and initialize a Queue using a LinkedList Queue<String> waitingQueue = new LinkedList<>(); // Adding new elements to the Queue (The Enqueue operation) waitingQueue.add("Rajeev"); waitingQueue.add("Chris"); waitingQueue.add("John"); waitingQueue.add("Mark"); waitingQueue.add("Steven"); System.out.println("WaitingQueue : " + waitingQueue); }

10 Output: WaitingQueue : [Rajeev, Chris, John, Mark, Steven] What is the output after executing the following line ? waitingQueue.remove(); Output: WaitingQueue : [Chris, John, Mark, Steven]

11 Queues Front Tail

12 Adding an item to a non-empty queue
public void enqueue (T newEntry) { Node newNode = new Node (newEntry, null); if ( isEmpty()) firstNode = newNode; else lastNode.setNextNode(newNode); lastNode = newNode; } // end enqueue ONLY lastNode needs adjustment Adapted from Pearson Education, Inc.

13 Removing an item from a queue with more than one item
ONLY firstNode needs adjustment Adapted from Pearson Education, Inc.

14 Removing last item from a queue
public T dequeue () { T front = null; if (!isEmpty ()) front = firstNode.data; firstNode = firstNode.next; if (firstNode == null) lastNode = null; } // end if return front; } // end dequeue BOTH firstNode AND lastNode need adjustment Adapted from Pearson Education, Inc.

15 ADT Queue: Specification
Elements: The elements are of a variable type <Type>. In a linked implementation elements are placed in nodes. public class Node<T> extends Object { public T data; public Node<T> next; public Node () { data = null; next = null; } public Node (T val) { data = val; next = null; } }

16 ADT Queue: Specification
Structure: the elements are linearly arranged, and ordered according to the order of arrival, most recently arrived element is called the tail and least recently arrived element the front or head. Domain: the number of elements in the queue is bounded therefore the domain is finite. Type of elements: Queue

17 ADT Queue: Specification
Operations: Method Enqueue (Type e) requires: Queue Q is not full. input: Type e. results: Element e is added to the queue at its tail. output: none. Method Serve (Type e) requires: Queue Q is not empty. input: results: the element at the head of Q is removed and its value assigned to e. output: Type e. Method Length (int length) input: results: The number of element in the Queue Q is returned. output: length.

18 ADT Queue: Specification
Operations: Method Full (boolean flag). requires: input: results: If Q is full then flag is set to true, otherwise flag is set to false. output: flag.

19 ADT Queue (Linked Implementation)
public class LinkQueue <Type> { private Node<Type> head, tail; private int size; /** Creates a new instance of LinkQueue */ public LinkQueue() { head = tail = null; size = 0; }

20 ADT Queue (Linked Implementation)
public boolean full() { return false; } public int length (){ return size;

21 ADT Queue (Linked Implementation)
public void enqueue (Type e) { if (tail == null){ head = tail = new Node(e); } else { tail.next = new Node(e); tail = tail.next; size++;

22 ADT Queue (Linked Implementation)
public Type serve() { Node<Type> tmp; Type x; tmp = head; x = head.data; head = head.next; size--; if (size == 0) tail = null; return x; }

23 Priority Queue Each data element has a priority associated with it.Highest priority item is served first. Real World Priority Queues: hospital emergency rooms…most sick patients treated first, events in a computer system, etc. Priority Queue can be viewed as: View 1: Priority queue as an ordered list. View 2: Priority queue as a set.

24 ADT Priority Queue Specification:
Elements: The elements are of type PQNode. Each node has in it a data element of variable type <Type> and priority of type Priority ( which could be int type). public class PQNode<T> { private T data; private Priority priority; public PQNode<T> next; public PQNode() { next = null; } public PQNode(T e, Priority p) { data = e; priority = p; }

25 ADT Priority Queue Structure: the elements are linearly arranged, and may be ordered according to a priority value, highest priority element is called the tail and least priority element the front or head. Domain: the number of nodes in the queue is bounded therefore the domain is finite. Type of elements: PriorityQueue

26 ADT Priority Queue Operations: Method Enqueue (Type e, Priority p)
requires: PQ is not full. input: e, p. results: Element e is added to the queue according to its priority. output: none. Method Serve (Type e, Priority p) requires: PQ is not empty. input: results: the element at the head of PQ is removed and returned. output: e, p. Method Length (int length) input: results: The number of element in the PQ is returned. output: length.

27 ADT Priority Queue Operations: Method Full (boolean flag).
requires: input: results: If PQ is full then flag is set to true, otherwise flag is set to false. output: flag.

28 ADT Priority Queue Array Implementation el 10 8 7 5 Head Tail el el el
Linked Implementation Insert Where? el 10 8 7 5 Head Tail

29 ADT Priority Queue (Linked)
public class LinkPQ<T> { private int size; private PQNode<T> head, tail; /* tail is of no use here. */ public LinkPQ() { head = tail = null; size = 0; } public int length (){ return size; }

30 ADT Priority Queue (Linked)
public int length (){ return size; } public boolean full () { return false;

31 ADT Priority Queue (Linked)
public void enqueue(T e, int pty) { PQNode<T> p, q, tmp; if ((size == 0) || (pty > head.Priority())) { tmp = new PQNode<T>(e, pty); tmp.next = head; head = tmp; } else { p = head; q = null; while ((p != null) && (p.Priority() > pty)) { q = p; p = p.next; } tmp.next = p; q.next = tmp; } }

32 ADT Priority Queue (Linked)
public T serve (Priority pty){ T e = head.get_data(); pty.set_value(head.get_priority().get_value()); head = head.next; size--; return(e); }

33 ADT Priority Queue Implementations
Array Implementation: Enqueue is O(n), Serve is O(1). Linked List: Enqueue is O(n), Serve is O(1). Heap: Enqueue is O(log n), Serve is O(log n)  Heaps to be discussed later.


Download ppt "Queues CSC212."

Similar presentations


Ads by Google