Stack Experiment
Stack Experiment
Objective : Design a data structure for stack. You have to decide on the data members and also the function
members if you use C++, analogous decisions need to be made for implementation in C. The basic member
functions are for i) construct an empty stack, ii) empty() : that returns true when a stack is empty and false
otherwise; iii) full() that returns true when stack is full and false otherwise; iv) push() to insert an element at top
of stack; v) pop() to delete the element from the top of stack and return the deleted element; vi) peek() to return
the element at top of stack without changing the stack contents. Operations, such as traverse() a stack, that lists
the elements from stack top to the bottom element, without changing the contents of the stack, copy() that
makes a copy of a given stack (without changing its contents), etc. are not commonly used but can be supported
if required.
Skills to be learnt : Design a stack data structure with associated functions; first using an array representation
and then using a linked list for organizing its data. Test both the designs using a common main() to ensure that
we may use either design by including the appropriate header file and get identical results.
Applications : After testing both the designs, use them to solve some sample application problems, such as
balanced parentheses, evaluation of postfix expression, conversion of infix to postfix, evaluation of infix
expression, a simple editor with undo and redo operations, etc.
(a) Complete the definition of “stack_array.h”. Compile and execute the program “stackuse.C” with input from
the file, “stringinp”, also given to you. Manually validate the output generated using your knowledge of
functioning of a stack.
(b) If you want to create a stack of integers, what are the changes needed in the files and at what locations in
either of the two files ? Implement the changes suggested by you and test the correctness by giving a list of
integers as input.
• A file, “stack_linked.h” that contains a partial implementation of a stack using a linked list. You have to
complete the definitions of member functions, item_type pop ( ) {}; item_type peek() {}; bool empty ( )
{ } and bool full ( ) { }. Do not change the code given unless it is absolutely necessary.
Make exactly one change in the file, “stackuse.C”, by replacing the earlier included header file to the one
defined by you here. Compile and execute the program “stackuse.C” with the input from the file, “stringinp”,
also given to you. Manually validate the output generated using your knowledge of functioning of a stack and
compare the outputs of Problems 1 and 2.
(b) The output message of part (a) can be misleading, specially in the case of multiple instances of the same
parenthesis in the expression, as the location of the parenthesis is not specified. State all the changes that are
required in the present design to change the output to the form shown below.
Given an expression in postfix form, involving the operators { +, -, *, /, %, } and integer operands, write a
program to find the value of the expression. The algorithm uses a stack to save the operands till it encounters an
operator at which point the sub-expression is evaluated and the result saved on stack. A few sample input and
output are given to you for your reference. Definition of an integer stack and an incomplete C++ program is
given to you. A few simple functions are also given, you may use them if you wish to do so.
Problem 5. Write a program to convert an infix expression in C or C++ to its equivalent postfix form. For this
problem, you may assume that the infix expression is without parentheses. A few sample input and desirable
output are given below.
Input / output Sample 1 Sample 2 Sample 3
Infix expression 2 + 5 * 6 - 12 / 4 41 % 9 / 3 - 6 * 2 + 13 12 + 41 - 9 / 3 % 6 * 2 - 1
Postfix expression Infix expression is : Infix expression is : Infix expression is :
2 + 5 * 6 - 12 / 4 41 % 9 / 3 - 6 * 2 + 13 12 + 41 - 9 / 3 % 6 * 2 - 1
Postfix expression is : Postfix expression is : Postfix expression is :
2 5 6 * + 12 4 / - 41 9 % 3 / 6 2 * - 13 + 12 41 + 9 3 / 6 % 2 * - 1 -
You are provided with an incomplete file, “infix2postfix-incomplete.C”. This program reads a file,
“operator-attrs.txt” which is to be supplied through command line argument (file contains the
associativity and precedence values of a few C++ operators). Program initializes an array of objects
binops[], each element of which is an object of class oprprop. A support function, find() is also defined
to search for an operator in binops[] and return its index, if found.
(a) You are required to complete the body of “infix2postfix-incomplete.C” so that the program
performs its intended task.
(b) Enhance the program of part (a) so that an input infix expression with parentheses {‘(‘, ‘)’}, nested
or otherwise, is processed and equivalent postfix expression without parentheses is generated.
Input Files : “infix_inp1”, “infix_inp2”, “infix_inp3”, “infix_inp4”, “operator-attrs.txt”
Program Files : “stack_array.h”, “infix2postfix-incomplete.C”
6 L binary | bitwise OR
4 L binary || logical OR
3 L ternary ?: arithmetic if