210 likes | 311 Views
Comp 245 Data Structures. Stacks. What is a Stack?. A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO random access to other elements within the stack. 2. 2. 8. 8. 5. top. 2. top. 8. 8. top. 8. top. 5. 5. 5. 5. top. 5.
E N D
Comp 245Data Structures Stacks
What is a Stack? • A LIFO (last in, first out) structure • Access (storage or retrieval) may only take place at the TOP • NO random access to other elements within the stack
2 2 8 8 5 top 2 top 8 8 top 8 top 5 5 5 5 top 5 PUSH 5 PUSH 8 PUSH 2 POP POP An Abstract view of a Stack
PUSH and POP • Push Method which will store data sent from the application onto the stack. Where this data is placed now becomes the top of the stack. • Pop Method which will remove the data found at the top of the stack and will send it to the application. The top of the stack will be changed to the next element in the stack.
A RRRRobust Stack • Full Before push can function, you must assure there is room for the data to be pushed!! This can be implemented as a separate method or incorporated into the push function. • Empty Before pop can function, you must assure there is data to be popped!! This can be implemented as a separate method or incorporated into the pop function.
The Palindrome Problem A palindrome is defined as text which if written backwards would be the exact same text. Examples: • 1234321 • BOB • ABLE WAS I ERE I SAW ELBA Note – In this definition, spaces matter. Some definitions will strip spaces and just take the raw text. Problem – Write a function which will take a string on input and will output if this string IS or IS NOT a palindrome. The solution must utilize a stack ADT when executing the solution.
The Palindrome ProblemUtilizing Stacks in the Solution Step 1 – Instantiate Two Stacks, S1 and S2 Step 2 – Push string onto S1 Step 3 – Pop half of S1 onto S2 Step 4 – If length of string is odd, an extra character will be in S1, pop this and trash Step 5 – Pop S1 and S2 and compare popped values Step 6 – If values are equal go back to Step 5 assuming S1 and S2 are not empty (if they are empty go to step 7); however, if values are unequal, string is not a palindrome, go to step 7 Step 7 – Output if the string IS or IS NOT a palindrome
A Stack object will contain: top should always indicate the first available slot where data may be placed top will also indicate whether or not the stack is empty or full Data Top Stack ImplementationArray Based An array of StackTypes: Data An integer control field: Top
Stack ImplementationLinked List Based • Only data needed is a pointer to the top of the linked list. • Very efficient, you are always pushing and popping from the top. There is no list traversal!! • An empty condition is when top = NULL. • A full condition is when you cannot obtain dynamic memory.
Create node for data to be stored in top p Push data (inside of node P) on top of stack top p Stack ImplementationPUSHing a Linked List
top data Return to app x top Move top to next node and delete Stack ImplementationPOPping a Linked List
Defining Stack OperationsFunctionality //Constructor Stack(); //Destructor ~Stack(); //Push Data – return if successful or not – full functionality bool Push (StackType); //Pop Data – return if successful or not – empty functionality bool Pop (StackType&);
Array Based An array of some set size – this is where the stack data is kept. An integer field to be used to mark the top. Linked List Based A pointer which contains the address for the top node in the stack. Defining Stack Data
Stack ApplicationReverse Polish Notation • Arithmetic expressions are normally written in infixnotation. It is called infix because the arithmetic operator (i.e. +, -, *, /) is in-between the operands. • Example: 5 + 3 * 2 – 6 • The answer above is 5. It is difficult to develop an algorithm to evaluate infix expressions due to precedence problems. You cannot simply evaluate an expression straight left to right! • Part of the task of a compiler is to generate machine language instructions to carry out the evaluation of an arithmetic expression. Ex. Z = a + b * c – d; • If we could evaluate an arithmetic expression by simply going straight left to right, the task of the compiler would be much easier.
Stack ApplicationReverse Polish Notation • A polish logician developed a way in which an arithmetic expression could be written that would allow it to be evaluated straight left to right. He called it reverse polish notation. Most people prefer to call this notation postfix. • This notation will… • Eliminate precedence (thus allowing a left to right evaluation) • Eliminate parenthesis
Infix 3 + 2 3 + 2 * 4 3 + 2 * 4 – (5 + 2) Postfix 3 2 + 3 2 4 * + 3 2 4 * + 5 2 + - Stack ApplicationReverse Polish NotationExamples
Stack ApplicationReverse Polish NotationEvaluation • Requires the usage of an operand stack. • A postfix expression consists of two types of tokens; operators and operands. • Steps: • Scan expression left to right • If token is an operand then push • If token is an operator then pop two, evaluate and push result • If the postfix expression was correctly formed then when all tokens have been processed there should be one element remaining on the stack; this should be the answer.
Stack ApplicationReverse Polish NotationEvaluation Practice • 3 4 5 + - Answer: -6 2) 6 1 - 3 * 5 7 8 / + + Answer: 18 3) 2 6 + 5 * - Answer: invalid expression
Stack ApplicationReverse Polish NotationInfix to Postfix Conversion • Requires usage of an operator stack and postfix string. • Steps: • If token is an operand, push onto postfix string. • If token is an operator, compare this with the top of the operator stack • If token is lesser (precedence), pop the operator stack and push onto postfix string then revaluate • If token is greater(precedence), push onto the operator stack • If token is equal (precedence) use the lesser rule • Comparing against an empty operator stackwill always result in a push onto the operator stack
Stack ApplicationReverse Polish NotationInfix to Postfix Practice • 2 + 3 * 4 Answer: 2 3 4 * + • A + (B – D) * E – F Answer: A B D – E * + F -
C++ STL Stack Container Class • The C++ STL stack container is a LIFO structure where elements are inserted and removed only from the end (top) of the container. http://www.cplusplus.com/reference/stl/stack/ • Here is an example of the palindrome problem using the STL stack. PaliSTL.cpp