380 likes | 390 Views
CS2006 - Data Structures I. Chapter 8 Queue II. Topics. Implementation Array ADT List. Array-Based Queue Implementation. Array-Based Implementation Fix-size Have the following definition final int MAZ_QUEUE = maxinum-size-of-queue; Object [] items; Int front; Int back;.
E N D
CS2006 - Data Structures I Chapter 8 Queue II
Topics • Implementation • Array • ADT List
Array-Based Queue Implementation • Array-Based Implementation • Fix-size • Have the following definition final int MAZ_QUEUE = maxinum-size-of-queue; Object [] items; Int front; Int back;
0 1 2 3 4 MAX_QUEUE -1 0 0 4 -1 20 45 51 76 84 0 1 2 3 4 Front Back Front Back MAX_QUEUE -1 Array-Based Queue Implementation • 1. Linear Array implementation • Front & Back are indexes in the array • Initial condition: Front =0 & Back = -1 • Initial state • After 5 additions
Array-Based Queue Implementation • 1. Linear Array implementation • Insertion • Increment back • Insert item in item [ back ] • Deletion • Increment front • Queue Empty Condition • ? • Queue is Full • ?
Array-Based Queue Implementation • 1. Linear Array implementation • Problem: Rightward-Drifting: • After a sequence of additions & removals, items will drift towards the end of the array • Full if back==MAX_QUEUE-1, even only a few items • How to solve it?
Array-Based Queue Implementation • 1. Linear Array implementation • Rightward drifting solutions • Use a circular array: When Front or Back reach the end of the array, wrap them around to the beginning of the array • Problem: • Front & Back can't be used to distinguish between queue-full & queue-empty conditions
Array-Based Queue Implementation 2. Circular Array Implementation MAX_QUEUE -1 MAX_QUEUE -1 MAX_QUEUE -1 Front Front Front 45 45 45 51 51 51 76 58 76 76 Back Back Back Deletion and Insertion on the queue
Array-Based Queue Implementation 2. How to determine if queue is full or empty? MAX_QUEUE -1 MAX_QUEUE -1 0 0 1 76 Front Back Back Front Queue with one item Delete, queue becomes empty
Array-Based Queue Implementation 2. How to determine if queue is full or empty? MAX_QUEUE -1 MAX_QUEUE -1 0 0 1 1 89 89 7 7 5 5 45 45 Back 47 32 32 76 76 56 56 Back Front Front Queue with single empty After insertion, queue becomes full
Array-Based Queue Implementation • 2. Circular Arrays Implementation • That is why we need a count If count ==MAX_QUEUE Queue is FULL Else if count ==0 Queue is EMPTY
MAX_QUEUE -1 Front 20 45 51 76 Back Array-Based Queue Implementation 2. Circular Array Implementation final int MAZ_QUEUE = maxinum-size-of-queue; Object [] items; int front; int back; int count;
Array-Based Queue Implementation • 2. Circular Arrays Implementation • Initial condition: • Count = 0, Front = 0, Back = MAX_QUEUE – 1 • The Wrap-around effect is obtained by using modulo arithmetic (%-operator) • Insertion • Increment Back, using modulo arithmetic • Insert item • Increment Count back = ( back + 1 ) % MAX_QUEUE; items[back} = newItem; ++count;
Array-Based Queue Implementation • 2. Circular Arrays Implementation • Initial condition: • Count = 0, Front = 0, Back = MAX_QUEUE – 1 • The Wrap-around effect is obtained by using modulo arithmetic (%-operator) • Insertion • Increment Back, using modulo arithmetic • Insert item • Increment Count back = ( back + 1 ) % MAX_QUEUE; items[back} = newItem; ++count;
Array-Based Queue Implementation • 2. Circular Array Implementation • Deletion • Increment Front using modulo arithmetic • Decrement Count front = ( front + 1 ) % MAX_QUEUE; - - count;
Array-Based Queue Implementation public class ArrayQueue implements QueueInterface { private final int MAX_QUEUE = 50; // maximum size of queue private Object[ ] items; private int front, back, count; public ArrayQueue() { items = new Object[MAX_QUEUE]; front = 0; back = MAX_QUEUE-1; count = 0; } // end default constructor // queue operations: public boolean isEmpty() { return count == 0; } // end isEmpty
Array-Based Queue Implementation public class ArrayQueue implements QueueInterface { private final int MAX_QUEUE = 50; // maximum size of queue private Object[ ] items; private int front, back, count; public ArrayQueue() { items = new Object[MAX_QUEUE]; front = 0; back = MAX_QUEUE-1; count = 0; } // end default constructor // queue operations: public boolean isEmpty() { return count == 0; } // end isEmpty
Array-Based Queue Implementation (2) public boolean isFull() { return count == MAX_QUEUE; } // end isFull public void enqueue(Object newItem) { if (!isFull()) { back = (back+1) % (MAX_QUEUE); items[back] = newItem; ++count; } else throw new QueueException("QueueException on enqueue: "+ "Queue full"); } // end enqueue
Array-Based Queue Implementation (2) public boolean isFull() { return count == MAX_QUEUE; } // end isFull public void enqueue(Object newItem) { if (!isFull()) { back = (back+1) % (MAX_QUEUE); items[back] = newItem; ++count; } else throw new QueueException("QueueException on enqueue: "+ "Queue full"); } // end enqueue
Array-Based Queue Implementation (3) public Object dequeue() throws QueueException { if (!isEmpty()) { // queue is not empty; remove front Object queueFront = items[front]; front = (front+1) % (MAX_QUEUE); --count; return queueFront; } else throw new QueueException("QueueException on dequeue: "+ "Queue empty"); } // end dequeue
Array-Based Queue Implementation (4) public void dequeueAll() { items = new Object[MAX_QUEUE]; front = 0; back = MAX_QUEUE-1; count = 0; } // end dequeueAll public Object peek() throws QueueException { if (!isEmpty()) { // queue is not empty; retrieve front return items[front]; } else { throw new QueueException("Queue exception on peek: " + "Queue empty"); } // end if } // end peek } // end ArrayQueue
Array-Based Queue Implementation (4) public void dequeueAll() { items = new Object[MAX_QUEUE]; front = 0; back = MAX_QUEUE-1; count = 0; } // end dequeueAll public Object peek() throws QueueException { if (!isEmpty()) { // queue is not empty; retrieve front return items[front]; } else { throw new QueueException("Queue exception on peek: " + "Queue empty"); } // end if } // end peek } // end ArrayQueue
ADT List Queue Implementation • Use ADT List to implement the queue • The implementation is much simpler • Front is the position 1 of the list • Back is the item at the end of the list
ADT List Queue Implementation • Dequeue () List.remove(1); • Peek() List.get(1); • Enqueue (newItem) List.add(list.size()+1, newitem);
ADT List Queue Implementation • Dequeue () List.remove(1); • Peek() List.get(1); • Enqueue (newItem) List.add(list.size()+1, newitem);
ListQueue Implementation public class ListQueue implements QueueInterface { private ListInterface list; public ListQueue() { list = new ListReferenceBased (); } // end default constructor // queue operations: public boolean isEmpty() { return list.isEmpty(); } // end isEmpty public void dequeueAll() { list.removeAll(); } // end dequeueAll
ListQueue Implementation public class ListQueue implements QueueInterface { private ListInterface list; public ListQueue() { list = new ListReferenceBased (); } // end default constructor // queue operations: public boolean isEmpty() { return list.isEmpty(); } // end isEmpty public void dequeueAll() { list.removeAll(); } // end dequeueAll
ListQueue Implementation (2) public void enqueue(Object newItem) { list.add(list.size()+1, newItem); } } // end enqueue
ListQueue Implementation (3) public Object dequeue() throws QueueException { if (!isEmpty()) { // queue is not empty; remove front Object queueFront = list.get(1); list.remove(1); return queueFront; } else { throw new QueueException("QueueException on dequeue:" + "queue empty"); } // end if } // end dequeue
ListQueue Implementation (4) public Object peek() throws QueueException { if (!isEmpty()) { return list.get(1); } else { throw new QueueException("QueueException on peek:" + "queue empty"); } // end if } // end peek } // end ListQueue
Review • Which of the following is the code to insert a new node, referenced by newNode, into an empty queue represented by a circular linked list? • newNode.setNext(lastNode); • lastNode.setNext(lastNode);lastNode = newNode; • newNode.setNext(lastNode);newNode = lastNode; • newNode.setNext(newNode); lastNode = newNode;
Review • Which of the following methods of QueueInterface does NOT throw a QueueException? • enqueue • dequeue • dequeueAll • peek
Review • To initialize a queue that is represented by a circular array, back is set to ______. • 0 • 1 • MAX_QUEUE • MAX_QUEUE - 1
Review • To initialize a queue that is represented by a circular array, front is set to ______. • 0 • 1 • MAX_QUEUE • MAX_QUEUE - 1
Review • The Java ______ operator is used to obtain the wraparound effect of a circular array-based queue. • * • + • % • /
Review • If a queue is implemented as the ADT list, which of the following queue operations can be implemented as list.remove(1)? • enqueue() • dequeue() • isEmpty() • peek()
Review • If a queue is implemented as the ADT list, which of the following queue operations can be implemented as list.get(1)? • enqueue() • dequeue() • isEmpty() • peek()
Review • In an implementation of a queue uses the ADT list, which of the following can be used to implement the operation enqueue(newItem)? • list.add(list.size(), newItem) • list.add(list.size()+1, newItem) • list.add(newItem.size(), newItem) • list.add(newItem.size()+1, newItem)