Share
Embed
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
54 slides
-
Memory Upload • Array-based queue • Methods that are private
• Algorithms for queue methods • Big-O for queue methods • A queue object in memory • Palindromes -
What Is a Queue? • A queue is a fancy
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 -
What Is a Queue? • A computer science example of
a queue is a data structure with a FIFO (first-in, first-out) property • For example, documents waiting in line to be printed on a printer -
What Is a Queue? • Queues are useful in simulating
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 -
Queue ADT • An ADT (Abstract Data Type) is a
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 -
Queue Methods • boolean empty() • Is the queue empty?
• boolean offer(T element) • Adds an element to the end of the queue • Returns true if added successfully, otherwise false -
Queue Methods • T poll() • Retrieves and removes the
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 -
Implementing a Queue • We can either implement a queue
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) -
Example Code • See files QueueInterface.java and ArrayQueue.java • See
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); -
Array-Based Queue • To offer (add) a new element to
the end of the queue, we place the element at the end of the array -
Array-Based Queue • To poll (remove) an element from the
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 -
Big-O Break • So which do we choose? 1. Slide
(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 -
Big-O Break • Since O(1) is much faster than O(n),
we choose to implement choice #2 -
Front and End Indexes • The ArrayQueue has integer 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 -
Problem with Arrays • As elements are polled (added) to
and offered (removed) from the array, we will have empty spaces at the beginning of the array and run out of empty spaces at the end of the array -
Solution to Arrays • However, this is easily solved by
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) -
Circular Arrays and Modulus • We can use modulus to
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 -
Compiler Warning • An array instantiation of generic type T
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 -
Private Methods • Sometimes we may want to use private
methods as opposed to public methods • For example, class ArrayQueue has this private method full() private boolean full(){ boolean fullArray = (currentSize == maxSize); return fullArray; } -
Private Methods • The scope of private methods is limited
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! -
Private Methods • Private methods are NOT listed in the
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 -
Offer (Add) Algorithm 1. If the queue is not full:
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 -
Offer (Add) Big-O • Big-O is O(1), as all steps
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) -
ArrayQueue Object • Instantiate a queue of Integers QueueInterface<Integer> queue
= new ArrayQueue<Integer>(5); queue 0 1 2 3 4 frontIndex endIndex currentSize maxSize array 5 0 -1 0 null null null null null -
Integer number = new Integer(10); queue.offer(number); queue 0 1 2
3 4 number Offer (Add) 10 • Offer integer 10 to front of queue frontIndex endIndex currentSize maxSize array 5 0 0 1 null null null null 10 -
• Offer 20, so add 1 to endIndex Offer (Add)
20 frontIndex endIndex currentSize maxSize array 5 0 1 2 null null null 10 20 number = new Integer(20); queue.offer(number); queue 0 1 2 3 4 number -
number = new Integer(30); queue.offer(number); queue 0 1 2 3
4 number Offer (Add) 30 • Add 1 to endIndex and currentSize frontIndex endIndex currentSize maxSize array 5 0 2 3 null null 10 20 30 -
number = new Integer(40); queue.offer(number); queue 0 1 2 3
4 number Offer (Add) 40 • frontIndex=0 and endIndex=3 frontIndex endIndex currentSize maxSize array 5 0 3 4 null 10 20 30 40 -
Poll (Remove) Algorithm 1. Initialize variable element to null 2.
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 -
Poll (Remove) Big-O • Since all the steps of the
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) -
number = queue.poll(); number queue 0 1 2 3 4
Poll (Remove) in Memory • Poll from front of queue, number=10 frontIndex endIndex currentSize maxSize array 5 1 3 3 null 10 20 30 40 -
frontIndex endIndex currentSize maxSize array number = queue.poll(); number queue
0 1 2 3 4 Poll (Remove) in Memory • number=20, add 1 to frontIndex 5 2 3 2 null 10 20 30 40 -
number = queue.poll(); //number=30 number queue 0 1 2 3
4 frontIndex endIndex currentSize maxSize array Poll (Remove) in Memory • frontIndex=3 and endIndex=3 5 3 3 1 null 10 20 30 40 -
number = new Integer(50); queue.offer(number); queue 0 1 2 3
4 number Offer (Add) 50 • Offer integer 50 to end of queue frontIndex endIndex currentSize maxSize array 5 3 4 2 10 20 30 40 50 -
number = new Integer(60); queue.offer(number); queue 0 1 2 3
4 number Offer (Add) 60 • Offer integer 60 to end of queue frontIndex endIndex currentSize maxSize array 5 3 4 2 10 20 30 40 50 60 -
• Jump to offer() method definition public boolean offer(T element){
element queue 0 1 2 3 4 number Offer (Add) 60 frontIndex endIndex currentSize maxSize array 5 3 4 2 10 20 30 40 50 60 -
• Queue is NOT full if (!this.full()) { element queue
0 1 2 3 4 number Offer (Add) 60 frontIndex endIndex currentSize maxSize array 5 3 4 2 10 20 30 40 50 60 -
endIndex=(endIndex+1)%maxSize; element queue 0 1 2 3 4 number The
Wrap! • Wrap around array: 0=(4+1)%5 frontIndex endIndex currentSize maxSize array 5 3 0 2 10 20 30 40 50 60 -
array[endIndex] = element; element queue 0 1 2 3 4
number Offer (Add) 60 • Assign element to array at endIndex frontIndex endIndex currentSize maxSize array 5 3 0 2 20 30 40 50 60 -
• Add 1 to current size of array currentSize++; element
queue 0 1 2 3 4 number Offer (Add) 60 frontIndex endIndex currentSize maxSize array 5 3 0 3 20 30 40 50 60 -
• Successful offer(),so return true return true; element queue 0
1 2 3 4 number Offer (Add) 60 frontIndex endIndex currentSize maxSize array 5 3 0 3 20 30 40 50 60 -
• Return to line following offer() queue 0 1 2
3 4 number Offer (Add) 60 frontIndex endIndex currentSize maxSize array 5 3 0 3 20 30 40 50 60 number = new Integer(60); queue.offer(number); -
• Offer 70 to the end of queue queue 0
1 2 3 4 number number = new Integer(70); queue.offer(number); Offer (Add) 70 frontIndex endIndex currentSize maxSize array 5 3 1 4 70 30 40 50 60 -
• Queue has 4 elements, from front to end they
are: 40, 50, 60, 70 queue 0 1 2 3 4 number ArrayQueue Object frontIndex endIndex currentSize maxSize array 5 3 1 4 30 40 50 60 70 -
Peek Algorithm 1. Initialize variable element to null 2. If
the queue is not empty, assign the address to the element at the frontIndex to the element variable 3. Return the element variable -
Peek Big-O • Big-O is O(1), because O(1) + 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) -
number = queue.peek(); queue 0 1 2 3 4 number
Peek in Memory • Peek does not change frontIndex, endIndex, or currentSize frontIndex endIndex currentSize maxSize array 5 3 1 4 30 40 50 60 70 -
number = queue.peek(); queue 0 1 2 3 4 number
Peek in Memory frontIndex endIndex currentSize maxSize array 5 3 1 4 30 40 50 60 70 • Peek only returns address (pointer) to element in front of queue -
Palindromes • A palindrome is a string that reads the
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 -
Algorithm • Algorithm to recognize a palindrome 1.Loop for each
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 -
Memory Defragmenter • Implementing a queue with an array •
Private methods • Algorithms for ArrayQueue methods • Big-O for ArrayQueue methods • Memory for an ArrayQueue object • Palindromes -
Task Manager • Before the next class, you need to:
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