word for a line • A daily life example of a queue is a line at a movie theatre • First person in line is first to buy a ticket • Last person in line is last to buy a ticket • Breaking in line is NOT cool
people waiting in lines • Compute average waiting time over many runs with random arrivals, random service times, and one or more lines • For example, a bank might run a simulation to see how many tellers they need to keep lines to a minimum
list of operations on an organized set of data • In Java, an ADT is represented by an interface • See QueueInterface.java on the class web page for the queue interface
element in the front of queue • If queue is empty, returns null • T peek() • Retrieves, but does not remove, the element in front of queue • If queue is empty, returns null
using an array or linked nodes • Array implementation: • Use a circular array by using the modulus operator (%) • Modulus is the remainder of a division • 7 / 3 = 2, but 7 % 3 = 1 (remainder)
the main() method for an example of using the QueueInterface to declare a queue variable and ArrayQueue to instantiate a queue object QueueInterface<Integer> queue = new QueueStack<Integer>(5);
front of the queue, we have to remove it from the front of the array • To do this, we have 2 choices: 1. Slide (copy) all elements to the left in the array, so we have no empty spaces at the beginning of the array 2. Use a variable to keep track of index of the beginning element in the array
(copy) all elements to the left in the array will take O(n), as all elements have to be copied 2. Using a variable to keep track of the index will take O(1), because we are simply assigning a value to a variable
frontIndex to keep track of the index of the front element in queue • For the index of the element at the end of queue, we have integer variable endIndex
storing elements at the beginning of the array again, making this a “circular” array • The values for endIndex and frontIndex keep increasing until reaching a maximum size, and then return back to zero (0)
make endIndex and frontIndex “wrap around” the array endIndex = (endIndex+1) % maxSize; • If endIndex is 4 and maxSize is 5 • 4 + 1 = 5 • 5 % 5 = 0 (5/5 = 1 remainder 0) • So the index can wrap around to 0
cannot be type-checked, so it gives a compiler warning • This code in ArrayQueue constructor gives a compiler warning array = (T[])new Object[maxSize]; • You can ignore this warning, as this is the only way in Java to create generic arrays
methods as opposed to public methods • For example, class ArrayQueue has this private method full() private boolean full(){ boolean fullArray = (currentSize == maxSize); return fullArray; }
to methods of the same class • We can use method full() in class method offer() if(!this.full()){ //OK! • But using method full() in static method main() will NOT compile queue.full(); //ERROR!
interface for a class • This is because private methods are used only within the class methods of a class definition • The public methods listed in an interface can be used outside the class definition
a. Add 1 to endIndex and take modulus to wrap around the array b. Assign element to array at endIndex c. Add 1 to current size of array d. Return true 2. If the queue is full, return false
are O(1) 1. If the queue is not full: O(1) a. Add 1 to endIndex and take modulus to wrap around the array O(1) b. Assign element at endIndex O(1) c. Add 1 to current size of array O(1) d. Return true O(1) 2. If the queue is full, return false O(1)
If the queue is not empty: a. Assign the address to the element at the frontIndex to the element variable b. Add 1 to frontIndex and take modulus to wrap around the array c. Subtract 1 from current size of array 3. Return the element variable
algorithm are O(1), the Big-O is O(1) 1. Initialize element to null O(1) 2. If the queue is not empty: O(1) a. Assign frontIndex to element O(1) b. Add 1 to frontIndex O(1) c. Subtract 1 from size of array O(1) 3. Return the element variable O(1)
+ O(1) = O(1) • Initialize element to null O(1) • If the queue is not empty, assign the address to element at the frontIndex to the element variable O(1) • Return the element variable O(1)
same forwards and backwards • Example palindrome words are: radar, racecar, kook, ono, Ogopogo (a lake monster in British Columbia), tattarrattat (English for "knocking on a door"), saippuakauppias (Finnish for "soap vendor"), and aibohphobia
character of a string a. Offer (add) each character to a queue b. Push (add) each character to a stack 2.Loop for each character of a string a. Remove character from queue & stack b. If any characters different, return false c. Otherwise, return true
1.Do the assignment corresponding to this lecture 2.Email me any questions you may have about the material 3.Turn in the assignment before the next lecture