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: • enqueue • dequeue
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 .
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.
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.
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.
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();
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();
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();
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.
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.
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.
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.
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.
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.
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.
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; } ... }
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; } ... }
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; } ... }
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; } ... }
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; } ... }
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; } ... }
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