



































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A “stack” is another example of an abstract data type. A stack has the following behaviors / functions: push(value) (or add(value)) - place an ...
Typology: Study notes
1 / 43
This page cannot be seen from the preview
Don't miss anything!
CS 106B Lecture 5: Stacks and Queues
Vector ("dynamic array") Review
Vector: Does the user care how it is implemented? int main() { Vector
Vector: Implemented with Moon Monkeys® We could imagine implementing the Stanford Library Vector using Moon Monkeys®, who keep all of our data on the Moon, and simply pass back the results of our functions as we need them. Vector
Stacks The push, pop, and top operations are the only operations allowed by the stack ADT, and as such, only the top element is accessible. Therefore, a stack is a “Last-In-First-Out” (LIFO) structure: the last item in is the first one out of a stack. 4 2 5 7 3 PUSH POP TOP
Stacks Despite the stack’s limitations (and indeed, because of them), the stack is a very frequently used ADT. In fact, most computer architectures implement a stack at the very core of their instruction sets — both push and pop are assembly code instructions. Stack operations are so useful that there is a stack built in to every program running on your PC — the stack is a memory block that gets used to store the state of memory when a function is called, and to restore it when a function returns. Why are stacks used to when functions are called? Let’s say we had a program like this: What happens to the state of the system as this program runs? main() { function1(); return; } function1() { function2(); return; } function2() { function3(); return; }
Stacks What are some downsides to using a stack?
Reversing the words in a sentence Let's build a program from scratch that reverses the words in a sentence. Goal: reverse the words in a sentence that has no punctuation other than letters and spaces. How might we do this?
More Advanced Stack Example When you were first learning algebraic expressions, your teacher probably gave you a problem like this, and said, "What is the result?" 5 * 4 - 8 / 2 + 9 The class got all sorts of different answers, because no one knew the order of operations yet (the correct answer is 25, by the way). Parenthesis become necessary as well (e.g., 10 / (8-3)). As it turns out, there is a "better" way! We can use a system of arithmetic called "postfix" notation — the expression above would become the following: This is a somewhat annoying problem — it would be nice if there were a better way to do arithmetic so we didn't have to worry about order of operations and parenthesis. 5 4 * 8 2 / - 9 + (^) Wat?
Postfix Example Postfix notation* works like this: Operands (the numbers) come first, followed by an operator (+, -, *, /, etc.). When an operator is read in, it uses the previous operands to perform the calculation, depending on how many are needed (most of the time it is two). So, to multiply 5 and 4 in postfix, the postfix is 5 4 * To divide 8 by 2, it is 8 2 / There is a simple and clever method using a stack to perform arithmetic on a postfix expression: (talk to your neighbor about how you might do this!) 5 4 * 8 2 / - 9 + *Postfix notation is also called "Reverse Polish Notation" (RPN) because in the 1920s a Polish logician named Jan Łukasiewicz invented "prefix" notation, and postfix is the opposite of postfix, and therefore so-called "Reverse Polish Notation" Read the input and push numbers onto a stack until you reach an operator. When you see an operator, apply the operator to the two numbers that are popped from the stack. Push the resulting value back onto the stack. When the input is complete, the value left on the stack is the result.
Postfix Example Code // Postfix arithmetic, implementing +, -, , / #include
Postfix Example Code main():