data structure lecture 3
DESCRIPTION
Linear Data Structure: This is about stack.TRANSCRIPT
![Page 1: Data Structure Lecture 3](https://reader033.vdocuments.site/reader033/viewer/2022061206/5482acd15906b5cd158b459c/html5/thumbnails/1.jpg)
Linear Data Structures
Stack
![Page 2: Data Structure Lecture 3](https://reader033.vdocuments.site/reader033/viewer/2022061206/5482acd15906b5cd158b459c/html5/thumbnails/2.jpg)
Topics Covered in This Lecture
• Applications of stack– Reversing the string– Balancing symbols– Postfix expression evaluation– Translating infix expression to postfix expression
![Page 3: Data Structure Lecture 3](https://reader033.vdocuments.site/reader033/viewer/2022061206/5482acd15906b5cd158b459c/html5/thumbnails/3.jpg)
Applications of Stack
• Reversing the string
– push each character on to a stack as it is read.
– When the line is finished, we then pop characters off the stack, and they will come off in the reverse order.
![Page 4: Data Structure Lecture 3](https://reader033.vdocuments.site/reader033/viewer/2022061206/5482acd15906b5cd158b459c/html5/thumbnails/4.jpg)
Applications of Stack
Reversing the stringvoid ReverseRead(void){ //using static Stack class of Lecture#3
Stack<char> stack;//The Stack ‘stack’ is createdchar item;cin>>item;while (!stack.isFull()&&item!='$')//type in $ from keyboard to stop input{ stack.push(item); // push each character onto the stack
cin>>item;}while(!stack.isEmpty() ){ item=stack.pop ();//Pop an element from stack
cout<<item;}
}
![Page 5: Data Structure Lecture 3](https://reader033.vdocuments.site/reader033/viewer/2022061206/5482acd15906b5cd158b459c/html5/thumbnails/5.jpg)
Applications of Stack
• Balancing SymbolsCompilers use a program that checks whether every symbol (like braces, parenthesis, brackets, etc) in a program is balanced.
The simple algorithm for this purpose is:
1. Make an empty stack.
2. Read the characters until end of file.
3. If the character is an open any thing, push it onto the stack.
4. If it is a close any thing, then
if the stack is empty report an error.
Otherwise Pop the Stack.
If the popped symbol is not the corresponding opening symbol, then report an error.
5. At the end of the file, if the stack is not empty report an error.
![Page 6: Data Structure Lecture 3](https://reader033.vdocuments.site/reader033/viewer/2022061206/5482acd15906b5cd158b459c/html5/thumbnails/6.jpg)
Polish Notation
a – b / c + d * e
Precedence?
1. b/c
2. d*e
3. a – a1 /a1 = b/c /
4. t2 + a2 / t2 = a – b/c / /a2 = d*e/
![Page 7: Data Structure Lecture 3](https://reader033.vdocuments.site/reader033/viewer/2022061206/5482acd15906b5cd158b459c/html5/thumbnails/7.jpg)
Infix, Suffix, PrefixInfix = a * b + c((a*b) +c) Priority: 1. a * b2. a1 + c / a1 = a * b / Prefix = * a b , +a1 c+*abcSuffix = ab* , a1c+ab*c+
![Page 8: Data Structure Lecture 3](https://reader033.vdocuments.site/reader033/viewer/2022061206/5482acd15906b5cd158b459c/html5/thumbnails/8.jpg)
Infix, Suffix, Prefixinfix = a- b * c / d + e / f suffix =a – bc* / d + e / f
a – bc*d/ + e / f a – bc*d/ + ef/ abc*d/- + ef/ abc*d/-ef/+
prefix =a - *bc / d + e / f a - /*bcd + e / f a - /*bcd + /ef -a/*bcd + /ef +-a/*bcd/ef
![Page 9: Data Structure Lecture 3](https://reader033.vdocuments.site/reader033/viewer/2022061206/5482acd15906b5cd158b459c/html5/thumbnails/9.jpg)
Infix, Suffix, Prefix
Infix:
a+b*c–d/f+e
Suffix:
abc*+df/-e+
Prefix:
+-+a*bc/dfe
![Page 10: Data Structure Lecture 3](https://reader033.vdocuments.site/reader033/viewer/2022061206/5482acd15906b5cd158b459c/html5/thumbnails/10.jpg)
Applications of Stack
Postfix Expression Evaluation
– When a number is seen, it is pushed onto the stack
– When an operator is seen, then pop two elements from stack and push the result onto the stack.
Now we evaluate the following postfix expression.6 5 2 3 + 8 * + 3 + *
1. The first four are placed on the stack. The resulting stack is
3
2
5
6
stack
![Page 11: Data Structure Lecture 3](https://reader033.vdocuments.site/reader033/viewer/2022061206/5482acd15906b5cd158b459c/html5/thumbnails/11.jpg)
Applications of Stack
• evaluating the following postfix expression.6 5 2 3 + 8 * + 3 + *
3
2
5
6
stack2. Next a + is read, so 3 and 2 are popped from the stack and their sum 5 is
pushed. 5
5
6
stack
![Page 12: Data Structure Lecture 3](https://reader033.vdocuments.site/reader033/viewer/2022061206/5482acd15906b5cd158b459c/html5/thumbnails/12.jpg)
Applications of Stack
• evaluating the following postfix expression.6 5 2 3 + 8 * + 3 + *
5
5
6
stack3. Next 8 is read and pushed. 8
5
5
6
stack
![Page 13: Data Structure Lecture 3](https://reader033.vdocuments.site/reader033/viewer/2022061206/5482acd15906b5cd158b459c/html5/thumbnails/13.jpg)
Applications of Stack
• evaluating the following postfix expression.6 5 2 3 + 8 * + 3 + *
4. Next a * is seen so 8 and 5 are popped as 8 * 5 = 40 is pushed
40
5
6
stack
8
5
5
6
stack
![Page 14: Data Structure Lecture 3](https://reader033.vdocuments.site/reader033/viewer/2022061206/5482acd15906b5cd158b459c/html5/thumbnails/14.jpg)
Applications of Stack
• evaluating the following postfix expression.6 5 2 3 + 8 * + 3 + *
5. Next a + is read so 40 and 5 are popped and 40 + 5 = 45 is pushed.
40
5
6
stack
45
6
stack
![Page 15: Data Structure Lecture 3](https://reader033.vdocuments.site/reader033/viewer/2022061206/5482acd15906b5cd158b459c/html5/thumbnails/15.jpg)
Applications of Stack
• evaluating the following postfix expression.6 5 2 3 + 8 * + 3 + *
6. Now 3 is pushed
45
6
stack
3
45
6
stack
![Page 16: Data Structure Lecture 3](https://reader033.vdocuments.site/reader033/viewer/2022061206/5482acd15906b5cd158b459c/html5/thumbnails/16.jpg)
Applications of Stack
• evaluating the following postfix expression.6 5 2 3 + 8 * + 3 + *
7. Next symbol is + so pops 3 and 45 and pushes 45 + 3 = 48, so push 48 in stack.
3
45
6
stack
48
6
stack
![Page 17: Data Structure Lecture 3](https://reader033.vdocuments.site/reader033/viewer/2022061206/5482acd15906b5cd158b459c/html5/thumbnails/17.jpg)
Applications of Stack
• evaluating the following postfix expression.6 5 2 3 + 8 * + 3 + *
7. Finally a * is seen and 48 and 6 are popped, the result 6 * 48 = 288 is pushed.
288
stack
48
6
stack
8. As there is no input, so pop the stack and we get the result.
![Page 18: Data Structure Lecture 3](https://reader033.vdocuments.site/reader033/viewer/2022061206/5482acd15906b5cd158b459c/html5/thumbnails/18.jpg)
Applications of Stack• Translating infix expressions to postfix expression
– When an operand is read, it is immediately placed onto the output.
– When an operator or left parenthesis comes then save it in the stack initially stack is empty.
– If we see a right parenthesis, then we pop the stack, writing symbols until we encounter a (corresponding) left parenthesis, which is popped but not output.
– If we see any other symbol (‘+’, ‘*’, ‘(‘, etc) then we pop entries form the stack until we find an entry of lower priority. One exception is that we never remove a ‘(‘ from the stack except when processing a ‘)’.
– When the popping is done, we push the operand onto the stack.
– Finally, if we read the end of input, we pop the stack until it is empty, writing symbols onto the output.
![Page 19: Data Structure Lecture 3](https://reader033.vdocuments.site/reader033/viewer/2022061206/5482acd15906b5cd158b459c/html5/thumbnails/19.jpg)
Applications of Stack• Translating infix expressions to postfix expression
Convert the following infix expression to postfix expression.a+b*c+(d*e+f)*g
1. First the symbol a is read, so it is passed through to the output a
output2. Then + is read and pushed onto the stack.
+
stack
4. Next a * is read. The top entry on the operator stack has lower precedence than *, so nothing is output and * is put on the .
3. Next b is read and passed through to the output. ab
output *
+
stack
![Page 20: Data Structure Lecture 3](https://reader033.vdocuments.site/reader033/viewer/2022061206/5482acd15906b5cd158b459c/html5/thumbnails/20.jpg)
Applications of StackConverting the following infix expression to postfix
expression.a+b*c+(d*e+f)*g5. Next, c is read and output.
6. The next symbol is a +. Checking the stack, we find that priority of stack top symbol * is higher than + . So we pop a * and place it on the output, Pop the other +, which is not of lower but equal priority, and then push +.
+
stack
abc*+
output
*
+
stack
abc
output
![Page 21: Data Structure Lecture 3](https://reader033.vdocuments.site/reader033/viewer/2022061206/5482acd15906b5cd158b459c/html5/thumbnails/21.jpg)
Applications of StackConverting the following infix expression to postfix
expression.a+b*c+(d*e+f)*g
7. The next symbol read is an ‘(‘, which, being of highest precedence, is placed on the stack.
(
+
stack8. Then d is read and output. abc*+d
output
![Page 22: Data Structure Lecture 3](https://reader033.vdocuments.site/reader033/viewer/2022061206/5482acd15906b5cd158b459c/html5/thumbnails/22.jpg)
Applications of StackConverting the following infix expression to postfix
expression.a+b*c+(d*e+f)*g9. We continue by reading a *. Since open parenthesis do not get removed except
when a closed parenthesis is being processed, there is no output and we push * in stack
*
(
+
stack10. Next, e is read and output.
abc*+de
output
![Page 23: Data Structure Lecture 3](https://reader033.vdocuments.site/reader033/viewer/2022061206/5482acd15906b5cd158b459c/html5/thumbnails/23.jpg)
Applications of StackConverting the following infix expression to postfix
expression.a+b*c+(d*e+f)*g11. The next symbol read is a +, since priority of stack top value is higher so we pop
* and push +.
abc*+de*
output
+
(
+
stack
12. Now we read f and output f.abc*+de*f
output
![Page 24: Data Structure Lecture 3](https://reader033.vdocuments.site/reader033/viewer/2022061206/5482acd15906b5cd158b459c/html5/thumbnails/24.jpg)
Applications of StackConverting the following infix expression to postfix
expression.a+b*c+(d*e+f)*g
13. Now we read a ‘)’, so the stack is emptied back to the ‘(‘, we output a +.
14. We read a * next; it is pushed onto the stack.
abc*+de*f+
output+
stack
*
+
stack15. Now, g is read and output. abc*+de*f+g
output
![Page 25: Data Structure Lecture 3](https://reader033.vdocuments.site/reader033/viewer/2022061206/5482acd15906b5cd158b459c/html5/thumbnails/25.jpg)
Applications of StackConverting the following infix expression to postfix
expression.a+b*c+(d*e+f)*g
16. The input is now empty, so pop output symbols from the stack until it is empty.
abc*+de*f+g*+
outputstack
*
+
stack
![Page 26: Data Structure Lecture 3](https://reader033.vdocuments.site/reader033/viewer/2022061206/5482acd15906b5cd158b459c/html5/thumbnails/26.jpg)
Assignment 1 Group Size: 2, Due Date : Feb 27, 2012
• Implement Applications of stack1. Balancing of symbols2. Decimal to Binary Conversion3. Infix to Postfix conversion4. Postfix expression evaluation5. Infix to Prefix conversion6. Prefix expression evaluation
1. Each application will be implemented as a separate function. Function will use stack, which is already implemented in a header file and is available for reuse.
2. Difference between CGPAs of both group members should not be more than ONE
![Page 27: Data Structure Lecture 3](https://reader033.vdocuments.site/reader033/viewer/2022061206/5482acd15906b5cd158b459c/html5/thumbnails/27.jpg)