Presentation is loading. Please wait.

Presentation is loading. Please wait.

COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.

Similar presentations


Presentation on theme: "COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find."— Presentation transcript:

1 COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find out how to do it. Theodore Roosevelt

2 COSC 1P03 Data Structures and Abstraction 9.2 Queue  a list (initially empty) of items (of some type) to which items may be added at one end (called the rear ) and from which items may be removed at the other end (called the front )  examples  waiting lines  print queues  process queue  behaviour  FIFO ordering  error conditions:  underflow  overflow

3 COSC 1P03 Data Structures and Abstraction 9.3 Queue Interface  generic ( Stores objects of type E )  no requirements  operations:  enter (enqueue, add, insert)  leave (dequeue, remove, delete)  front (head, first)  length (count, size)  empty  exceptions  NoItemException  NoSpaceException

4 COSC 1P03 Data Structures and Abstraction 9.4 Queue ADT Contiguous Implementation  based on variable-sized array  two indices: front & rear  add at rear, remove at front  queue moves towards rear  repositioning on delete: O(n)  circular array  at end of array reuse front  index modulo array size

5 COSC 1P03 Data Structures and Abstraction 9.5 Queue ADT.  implementation  instance variables  count  constructors  empty state  methods  enter  overflow  increment  leave, front  underflow  length, empty  compute?  empty vs full

6 COSC 1P03 Data Structures and Abstraction 9.6 Queue ADT Linked Implementation  sequentially-linked structure of items  deletion from front  insertion at end  keep pointer to rear O(1)  length?  keep count else O(n)  comparison with contiguous  all operations O(1)  space tradeoffs

7 COSC 1P03 Data Structures and Abstraction 9.7

8

9

10 COSC 1P03 Data Structures and Abstraction 9.10 Queue Example Input: 1 2 3 4 5Output: 1 2 3 4 5 12345

11 COSC 1P03 Data Structures and Abstraction 9.11 Process Queue

12 COSC 1P03 Data Structures and Abstraction 9.12 Circular Queue 123...N123...N Front Rear N-1 0 1

13 COSC 1P03 Data Structures and Abstraction 9.13 Queue ADT Interface package Collections; public interface Queue { public void enter ( E item ); public E leave ( ); public E front ( ); public boolean empty ( ); public int length ( ); }// Queue Part of the Collections Package Formal type definitionAccept an item of type E Both return an item of type E, much like Pop and Top do for a Stack.

14 COSC 1P03 Data Structures and Abstraction 9.14 Array Queue package Collections; public class ConQueue implements Queue, Serializable { private E[]items;// the items of the queue private intfront;// index of the front item private intrear;// index of next available element private intcount;// number of items in queue public ConQueue ( ) { this(100); };// constructor public ConQueue ( int size ) { items = (E[]) new Object[size]; front = 0; rear = 0; count = 0; };// constructor public void enter ( E item ) { if ( count >= items.length ) { throw new NoSpaceException(); } else { items[rear] = item; rear = (rear + 1) % items.length; count = count + 1; }; };// enter Constructor creates the array and initializes front, rear and count Check for overflow, is count >= to the capacity of the array Add the item into the array indexed by rear Perform modular addition to increment rear index. We have added 1 item, so increment count. Instance variables, items is an array of E data types ConQueue implements an interface call Queue of the same type

15 COSC 1P03 Data Structures and Abstraction 9.15 Array Queue. public E leave ( ) { Ei;// item removed if ( count <= 0 ) { throw new NoItemException(); } else { i = items[front]; items[front] = null; front = (front + 1) % items.length; count = count - 1; return i; }; };// leave public E front ( ) { if ( count <= 0 ) { throw new NoItemException(); } else { return items[front]; }; };// front public boolean empty ( ) { return count <= 0; };// empty public int length ( ) { return count; };// length }// ConQueue Check for underflow, is count 0 Extract the item Set pointer to null so garbage collection will reclaim the memory. Modular addition to increment front. Removed a item so reduce count. Return the extracted item Return the front item if it exists. Leave it in the queue. If count is 0 then it is empty. How many items are in the queue

16 COSC 1P03 Data Structures and Abstraction 9.16 The End


Download ppt "COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find."

Similar presentations


Ads by Google