10-Stack - and if it is not what parenthesis is...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Last In – First Out (LIFO) data structure that supports two operations: - push for adding new element at the top of stack - pop for removing new element at the top of stack Stack Stack
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Stack: Push Stack: Push Max Lex Nika Vlad Push Joe top Max Lex Nika Vlad Joe top
Background image of page 2
Stack: Pop Stack: Pop pop Max Lex Nika Vlad Joe Max Lex Nika Vlad top Joe
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Double-linked list #include <stack> stack<string> myStack; size() returns list size; top() returns top stack element; push () adds new element to (the top of) stack; pop() removes element from (the top of) stack; Generic stack Class (STL) Generic stack Class (STL)
Background image of page 4
There is no way to traverse a stack without removing elements from it! No Stack Traversal No Stack Traversal
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Using string and stack - Read from cin into a string a numeric expression, e.g. (a*(b+c)-[a/b]+{c*(a+b)}) – OK (a*(b+c)-[a/b]+{c*(a+b)) – missing } and make sure that parenthesis are balanced - Report if the expression is balanced
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: and if it is not what parenthesis is missing.-HOW? Use stack! Task: Balanced Parenthesis Task: Balanced Parenthesis 1) Read expression into string 2) Look at each character-If the character is an opening parenthesis type push it onto stack-If a character is a closing parenthesis type compare it to the parenthesis at the top of stack-If there is a match pop a parenthesis from stack, continue the process-If there is no match report error 3) If after going through these steps the stack is not empty the expression is still unbalanced! Balanced Parenthesis Solution Balanced Parenthesis Solution Read chapter 5, prepare for quiz next class. I will randomly question 10 students. Correct answer earns 1%, incorrect earns -1%. From now on successfully complete lab work counts for attendance. Assignment Assignment...
View Full Document

Page1 / 8

10-Stack - and if it is not what parenthesis is...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online