Download presentation
Presentation is loading. Please wait.
Published byDarleen Parks Modified over 8 years ago
1
Queue ADT for lining up politely
2
COSC 2006 queue2 Queue – simple collection class first-in first-out (FIFO) structure insert new elements at one end (rear) remove elements from opposite end (front) no access to interior of collection 7812332680 frontrear insert new element remove element 40
3
COSC 2006 queue3 Queue applications local area network files waiting for printing are in a queue multi-tasking operating system programs waiting for processor pipes between processes simulations people in line for service – Tim Hortons, store checkout, bank aircraft taxiing for take-off
4
COSC 2006 queue4 Queues and Stacks queue stack 7812332680 frontrear insert new element remove element 40 6342587128 front / top insert new element remove element 94
5
COSC 2006 queue5 Queue methods public void insert (Object item) add new item to rear of queue (enqueue) public Object getFront() remove item from front of queue (dequeue) public boolean isEmpty() true when no items in queue public int size() number of items in queue
6
COSC 2006 queue6 Linked list implementation of a queue public class Queue { int manyNodes; Node front, rear; } manyNodes front rear 4 aaaxxxttttmm
7
COSC 2006 queue7 Linked list queue invariants public class Queue { //1. number of items stored in manyNodes //2. items stored in linked list with: // front of queue at head node // rear of queue at final node //3. front and rear reference // head and tail of queue; // both null if queue is empty int manyNodes; Node front, rear; }
8
COSC 2006 queue8 (new) empty queue Queue q = new Queue(); manyNodes front rear 0 q public Queue() { manyNodes = 0; front = null; rear = null; }
9
COSC 2006 queue9 Insertion into queue q.insert(“bbb”); manyNodes front rear 4 aaaxxxttttmm q bbb 5
10
Insertion into queue q.insert(“bbb”); manyNodes front rear 4 aaaxxxttttmm q bbb 5 public void insert(Object item) { if(isEmpty()) { front = new Node(Item,null); rear = front; } else { rear.addNodeAfter(item); rear = rear.getLink(); } manyNodes++; }
11
COSC 2006 queue11 Insertion into queue - performance public void insert(Object item) { if(isEmpty()) { front = new Node(Item,null); rear = front; } else { rear.addNodeAfter(item); rear = rear.getLink(); } manyNodes++; } efficient O(1) independent of number of items (queue length)
12
COSC 2006 queue12 Queue – the java class class Queue.java as linked listQueue.java adapted from Main classes for queues
13
COSC 2006 queue13 Array implementation of a queue public class Queue { Object[] data; int manyItems; int front, rear; } data front rear 2 5 aaaxxxttttmm 2 5 manyItems 4 4
14
COSC 2006 queue14 Array implementation of a queue queue ‘drifts’ along the array but insertion and deletion are efficient O(1) data front rear 2 5 aaaxxxttttmm 2 5 manyItems 4 4
15
COSC 2006 queue15 Array drift of a queue (1) to avoid interruption at the end of the array, use a circular array data front rear 2 5 aaa xxx tttt mm 2 5 manyItems 4
16
COSC 2006 queue16 Array drift of a queue (2) after more insertion and deletion… data front rear 12 14 ww dd sss 14 manyItems 3 12
17
COSC 2006 queue17 Array drift of a queue (3) after one more insertion data front rear 12 0 ww dd sss 0 manyItems 4 12 nn
18
COSC 2006 queue18 Coding the array Queue java code example Queue.java.arrayQueue.java.array invariant nextIndex insert getFront ensureCapacity
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.