1 / 38

CS2006 - Data Structures I

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;.

Download Presentation

CS2006 - Data Structures I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS2006 - Data Structures I Chapter 8 Queue II

  2. Topics • Implementation • Array • ADT List

  3. 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;

  4. 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

  5. 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 • ?

  6. 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?

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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;

  13. 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;

  14. 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;

  15. Array-Based Queue Implementation • 2. Circular Array Implementation • Deletion • Increment Front using modulo arithmetic • Decrement Count front = ( front + 1 ) % MAX_QUEUE; - - count;

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. ADT List Queue Implementation • Dequeue () List.remove(1); • Peek() List.get(1); • Enqueue (newItem) List.add(list.size()+1, newitem);

  25. ADT List Queue Implementation • Dequeue () List.remove(1); • Peek() List.get(1); • Enqueue (newItem) List.add(list.size()+1, newitem);

  26. 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

  27. 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

  28. ListQueue Implementation (2) public void enqueue(Object newItem) { list.add(list.size()+1, newItem); } } // end enqueue

  29. 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

  30. 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

  31. 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;

  32. Review • Which of the following methods of QueueInterface does NOT throw a QueueException? • enqueue • dequeue • dequeueAll • peek

  33. Review • To initialize a queue that is represented by a circular array, back is set to ______. • 0 • 1 • MAX_QUEUE • MAX_QUEUE - 1

  34. Review • To initialize a queue that is represented by a circular array, front is set to ______. • 0 • 1 • MAX_QUEUE • MAX_QUEUE - 1

  35. Review • The Java ______ operator is used to obtain the wraparound effect of a circular array-based queue. • * • + • % • /

  36. 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()

  37. 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()

  38. 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)

More Related