comp 213
play

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue - PowerPoint PPT Presentation

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.) Recall: The Queue ADT A data structure in which elements enter at one end and are removed from the opposite end is called a queue . 2 main operations:


  1. COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)

  2. Recall: The Queue ADT • A data structure in which elements enter at one end and are removed from the opposite end is called a queue . • 2 main operations: • enqueue • dequeue

  3. Recall: Formal Specification • Our queues are generic – the type of object held by a particular queue is indicated by the client. • We provide observer operations isEmpty and isFull . • We create a QueueInterface that defines the signatures of the queue methods that do not depend on the boundedness of the queue. • We create a BoundedQueueInterface and an UnboundedQueueInterface , which extend QueueInterface .

  4. Array-Based Implementation for unbounded queue • Class ArrayUnbQueue that implements the Unb oundedQueueInterface . • The trick here is to create a new larger array, when needed, and copy the structure into the new array. • No need for isFull method.

  5. Array-Based Implementation for unbounded queue • Class ArrayUnbQueue that implements the Unb oundedQueueInterface . • The trick here is to create a new larger array, when needed, and copy the structure into the new array. • No need for isFull method.

  6. Array-Based Implementation for unbounded queue • Class ArrayUnbQueue that implements the Unb oundedQueueInterface . • The trick here is to create a new larger array, when needed, and copy the structure into the new array. • No need for isFull method.

  7. Unbounded vs. bounded implementation: what changes? • enqueue method – to increase the capacity of the array if it has run out of space. • we implement it as a separate method named enlarge . • Then, the enqueue method can start with: if (numElements == queue.length) enlarge();

  8. Unbounded vs. bounded implementation: what changes? • enqueue method – to increase the capacity of the array if it has run out of space. • we implement it as a separate method named enlarge . • Then, the enqueue method can start with: if (numElements == queue.length) enlarge();

  9. Unbounded vs. bounded implementation: what changes? • enqueue method – to increase the capacity of the array if it has run out of space. • we implement it as a separate method named enlarge . • Then, the enqueue method can start with: if (numElements == queue.length) enlarge();

  10. Options for the enlarge method • We could set a constant increment value or multiplying factor within the class. • We could allow the application to specify an increment value or multiplying factor when the queue is instantiated. • We could use the original capacity as the increment value.

  11. Options for the enlarge method • We could set a constant increment value or multiplying factor within the class. • We could allow the application to specify an increment value or multiplying factor when the queue is instantiated. • We could use the original capacity as the increment value.

  12. Options for the enlarge method • We could set a constant increment value or multiplying factor within the class. • We could allow the application to specify an increment value or multiplying factor when the queue is instantiated. • We could use the original capacity as the increment value.

  13. Brainstorming • The enlarge operation is costly ⇒ increment by large amount. • Increment too much ⇒ waste of both time & space. • Let us use henceforth the original capacity as the increment value: • Instantiate an array with a size equal to the current capacity plus the original capacity. • Remember the original capacity using an instance variable origiCap.

  14. Brainstorming • The enlarge operation is costly ⇒ increment by large amount. • Increment too much ⇒ waste of both time & space. • Let us use henceforth the original capacity as the increment value: • Instantiate an array with a size equal to the current capacity plus the original capacity. • Remember the original capacity using an instance variable origiCap.

  15. Brainstorming • The enlarge operation is costly ⇒ increment by large amount. • Increment too much ⇒ waste of both time & space. • Let us use henceforth the original capacity as the increment value: • Instantiate an array with a size equal to the current capacity plus the original capacity. • Remember the original capacity using an instance variable origiCap.

  16. Brainstorming • The enlarge operation is costly ⇒ increment by large amount. • Increment too much ⇒ waste of both time & space. • Let us use henceforth the original capacity as the increment value: • Instantiate an array with a size equal to the current capacity plus the original capacity. • Remember the original capacity using an instance variable origiCap.

  17. Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface { protected final int DEFAULTCAP = 100; // default capacity protected Object[] queue; // array that holds queue elements protected int origiCap; // original capacity protected int numElements = 0; // number of elements in the queue protected int front = 0; // index of front of queue protected int rear = -1; // index of rear of queue public ArrayUnbQueue() { queue = new Object[DEFAULTCAP]; rear = DEFAULTCAP - 1; origiCap = DEFAULTCAP; } public ArrayBoundedQueue(int origiCap) { queue = new Object[origiCap]; rear = origiCap- 1; this.origiCap = origiCap; } ... }

  18. Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface { protected final int DEFAULTCAP = 100; // default capacity protected Object[] queue; // array that holds queue elements protected int origiCap; // original capacity protected int numElements = 0; // number of elements in the queue protected int front = 0; // index of front of queue protected int rear = -1; // index of rear of queue public ArrayUnbQueue() { queue = new Object[DEFAULTCAP]; rear = DEFAULTCAP - 1; origiCap = DEFAULTCAP; } public ArrayBoundedQueue(int origiCap) { queue = new Object[origiCap]; rear = origiCap- 1; this.origiCap = origiCap; } ... }

  19. Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface { protected final int DEFAULTCAP = 100; // default capacity protected Object[] queue; // array that holds queue elements protected int origiCap; // original capacity protected int numElements = 0; // number of elements in the queue protected int front = 0; // index of front of queue protected int rear = -1; // index of rear of queue public ArrayUnbQueue() { queue = new Object[DEFAULTCAP]; rear = DEFAULTCAP - 1; origiCap = DEFAULTCAP; } public ArrayBoundedQueue(int origiCap) { queue = new Object[origiCap]; rear = origiCap- 1; this.origiCap = origiCap; } ... }

  20. Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface { protected final int DEFAULTCAP = 100; // default capacity protected Object[] queue; // array that holds queue elements protected int origiCap; // original capacity protected int numElements = 0; // number of elements in the queue protected int front = 0; // index of front of queue protected int rear = -1; // index of rear of queue public ArrayUnbQueue() { queue = new Object[DEFAULTCAP]; rear = DEFAULTCAP - 1; origiCap = DEFAULTCAP; } public ArrayBoundedQueue(int origiCap) { queue = new Object[origiCap]; rear = origiCap- 1; this.origiCap = origiCap; } ... }

  21. Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface { protected final int DEFAULTCAP = 100; // default capacity protected Object[] queue; // array that holds queue elements protected int origiCap; // original capacity protected int numElements = 0; // number of elements in the queue protected int front = 0; // index of front of queue protected int rear = -1; // index of rear of queue public ArrayUnbQueue() { queue = new Object[DEFAULTCAP]; rear = DEFAULTCAP - 1; origiCap = DEFAULTCAP; } public ArrayBoundedQueue(int origiCap) { queue = new Object[origiCap]; rear = origiCap- 1; this.origiCap = origiCap; } ... }

  22. Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface { protected final int DEFAULTCAP = 100; // default capacity protected Object[] queue; // array that holds queue elements protected int origiCap; // original capacity protected int numElements = 0; // number of elements in the queue protected int front = 0; // index of front of queue protected int rear = -1; // index of rear of queue public ArrayUnbQueue() { queue = new Object[DEFAULTCAP]; rear = DEFAULTCAP - 1; origiCap = DEFAULTCAP; } public ArrayBoundedQueue(int origiCap) { queue = new Object[origiCap]; rear = origiCap- 1; this.origiCap = origiCap; } ... }

  23. Implementation of our queue public class ArrayUnbQueue implements UnboundedQueueInterface { protected final int DEFAULTCAP = 100; // default capacity protected Object[] queue; // array that holds queue elements protected int origiCap; // original capacity protected int numElements = 0; // number of elements in the queue protected int front = 0; // index of front of queue protected int rear = -1; // index of rear of queue public ArrayUnbQueue() { queue = new Object[DEFAULTCAP]; rear = DEFAULTCAP - 1; origiCap = DEFAULTCAP; } public ArrayBoundedQueue(int origiCap) { queue = new Object[origiCap]; rear = origiCap- 1; this.origiCap = origiCap; } ... }

Recommend


More recommend