Download presentation
Presentation is loading. Please wait.
1
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann
2
Michael Eckmann - Skidmore College - CS 206 - Spring 2009 Today’s Topics Questions? Comments? Queue Stack
3
Queues and Stacks Could we implement a Queue with – a linked list – an array Could we implement a Stack with – a linked list – an array
4
Queues and Stacks Let's implement a stack with an array –what will be our instance variables? Let's implement a queue with an array –what will be our instance variables?
5
Queues and Stacks Let's implement a queue with an array –if we want to keep a fixed sized array and we don't want to keep shifting the values around in the array we can use a scheme that stores an index to the front, an index to the rear –if we want the front to be the index that we would dequeue from and rear to be the index after the last element (so that we would enqueue to rear) then we should do the following: –front being -1 and rear being 0 means the queue is empty. –front and rear being the same value means we have a full queue (we won't allow enqueuing to a full queue) –when enqueue first one, front becomes 0 and rear becomes 1 –when queue becomes empty (how can we tell?), set front to - 1 and rear to 0 –also, when rear goes off the end of the array we can wrap it around to 0, only if front is not still 0. Make sense?
6
Queues and Stacks Let's use that Queue class in an interesting sorting method called Radix Sort.
7
Queues and Stacks Radix Sort. –consider the job of sorting (base 10) integers let's limit them to 0-99 for now (all >=0 and <= 100) –we handled this in several ways already –a totally different way is the following way start with an unsorted list separate the numbers to be sorted into 10 different bins based on their 1's digit then, get the numbers out of the bins and make a new list (take numbers from bin 0, then add on to the end of the list the numbers in bin 1,... and so on, finally adding to the end of the list the numbers from bin 9) then separate this list into 10 bins based on the 10's digit. get the numbers out of the bins like before to get a sorted list.
8
Queues and Stacks Radix Sort. –example on board with the following: { 12, 15, 88, 76, 63, 21, 22, 1, 52, 2, 23, 5 }
9
Queues and Stacks Radix Sort. –example on board with the following: { 12, 15, 88, 76, 63, 21, 22, 1, 52, 2, 23, 5 } –each of the 10 bins were queues –also if you want to sort numbers that have a maximum of n digits in them, then you need n passes through the sort and each pass is a higher place in the number –we could use an array of 10 queues each index to the array corresponds to the digit in whatever place we're working on
10
Queues and Stacks Applications of stacks –balancing symbols. e.g. curly braces (left { ) and (right }) compilers need to figure out if there are the same number of lefts as there are rights. a stack is handy for this how?
11
Queues and Stacks Applications of stacks –balancing symbols. e.g. curly braces (left { ) and (right }) compilers need to figure out if there are the same number of lefts as there are rights. a stack is handy for this –when see a left curly, push it on the stack –when see a right curly try to pop the stack --- there should be a left curly to pop, otherwise it's an error –when get to the end of the source code, if the stack is empty (and we haven't run into the error above) then the curly braces are balanced.
12
Queues and Stacks Applications of stacks –postfix expressions (reverse Polish notation) infix (what we're used to) is like: 1.07*3.85 + 14.05 + 1.07*8.75 postfix is like: 1.07, 8.75, *, 14.05, +, 1.07, 3.85, *, + infix result requires us to do the multiplies first and then store the intermediate values and then to the 2 adds. postfix operates in the following way: –when see a number, push it –when see an operator, perform it to the two top numbers on the stack and push the result final result in our example is: 27.532
13
Queues and Stacks Applications of stacks –converting from infix (with only +, *, (, ) ) to postfix expressions infix example (assume normal precedence): a + b*c + ( d*e + f ) * g postfix for our example could be: a b c * + d e * f + g * + –let's look at the algorithm to do this using a stack
14
Queues and Stacks converting infix to postfix –when an operand (a, b, c, etc.) is read write it to the output –if we see a ) then we pop the stack and write to output until a left paren is on top of the stack --- at which point we pop the left paren but do not output it. –when a + or * is read, examine the top of the stack – if top is of lower precedence than the one just read, then push the operator –when a + or * is read – if top is of the stack does not have lower precedence than the one just read, then pop the operator and write to output –when a ( is read push it –Let's try this algorithm with our example: infix: a + b*c + ( d*e + f ) * g postfix (expected output): a b c * + d e * f + g * +
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.