24AIT151: Data Structure and Algorithms
LAB 4 : Stack
Instructions:
a) Save your lab.doc as LAB_no_Qno_RollNo.doc. At the end of semester you need
to submit your all programs along with the output .
b) Use/paste the snapshot of the steps followed along with result/s.
c) Mention your observation/comment after results in the doc.
_++++++++++++++++++++++++++++++++++++++++++++++++++++++_
Objectives: The student will write a program that uses the Stack data structure.
1. You are to write a program that simply reverses and prints each line of textual input using
a stack data structure, excluding any leading or trailing spaces on the line (that is, you should
remove such space). You must use the stack class that you define and its methods. Each line
can have any length and any content except for a single period, which signals the end of data.
Input:
A series of lines, each of which can be of any length, followed by a terminating line
that contains a single period
Output:
A series of lines, which correspond to each input line (excluding the terminating line)
that are printed backwards and without any leading or trailing space. The program
should print appropriate messages for STACK overflow, Under flow and empty, use
separate functions to detect these cases.
Sample Input
"Hello there, everybody."
To be continued...
Expected Output
".ydobyreve ,ereht olleH"
...deunitnoc eb oT
X
X
22.3
3.22
Observation /Comments:
IBM
MBI
2.. Infix to Postfix (reverse -polish notation )
Write a C program to convert infix expression to postfix.
hint: (5 + 3) * 6 becomes 5 3 + 6 *
24AIT151: Data Structure and Algorithms
LAB 4 : Stack
Supplementary Question: Balanced Parentheses checker using a stack.
Write a program to check expressions that may include parentheses "( )", square brackets "[ ]",
and curly braces "{ }" in any combination. Your program will need to report on whether a
string is balanced, and the maximal size of the stack needed.
Input:
Each line of input will be an expression, containing parentheses, brackets, and/or braces in any
combination. Count the expressions starting with 1.
Output:
Output each case single spaced and with the case number exactly as shown below. Tell whether
the input expression was balanced or not, and give the maximal size of the stack used in the
evaluation. Spacing, punctuation, and capitalization should be exactly as shown below (for
instance, there is only one space following the colon, first period, before/after the = symbol,
and between words).
Sample Input
Sample
Expected Output
Input Expected Output
Expected Output
a+b
(a + b))
[a
{[(22 + 33)*5]+(1-3)}/{a*b}
{{[[3])}
<blank line ends input>
Comment/Observation:
1: Balanced. Maximum stack size = 0.
2: Not balanced.
3: Not balanced.
4: Balanced. Maximum stack size = 3.
5: Not balanced.