Manual - For - Exp - No - 03 - Implementation of Stack
Manual - For - Exp - No - 03 - Implementation of Stack
Experiment No.03
A.1 Aim:
To study and implement concept of Stack data structure and use in recursion
A.2 Prerequisite:
1. Knowledge of different operations performed on Stack data structure
A.3 Outcome:
After successful completion of this experiment students will be able to
A.4 Theory:
A.4.1. Introduction of Stack
Stack is a linear data structure which follows a particular order in which the operations are
performed. The order may be LIFO (Last in first out) or FILO (First in last out).
1. Push: Adds an item in the stack. If the stack is full, then it is said to be in an overflow
condition.
2. Pop: Removes an item from the stack. The items are popped in the reversed order in
which they are pushed. If the stack is empty, then it is said to be an underflow condition.
3. Peek: Returns the top most element from the stack.
Applications of Stack:
1. Balancing of symbols
2. Infix to postfix\prefix conversion
3. Redo-undo features at many places like editors, Photoshop
4. Forward and backward feature in web browsers
Introduction to Recursion
The classic example of a problem where the solution to the problem can be stated in
terms of itself is the calculation of n factorial for n >= 1. By definition:
n! = 1 * 2 * 3 * .. * n
We easily see that n! = n * (n - 1)!
Notice that to calculate n factorial recursively, we calculate n - 1 factorial and multiply by
n. To calculate n-1 factorial we calculate n – 2 factorial and multiply by n - 1, etc. For
example,
4! = 4 * 3!
= 4 * 3 * 2!
= 4 * 3 * 2 * 1!
=4*3*2*1
To calculate factorial we need a definition. The following is a recursive definition of
factorial:
n! = 1, if n = 0
n! = n * (n - 1)!, if n > 0
A recursive definition consists of a base case step that defines the beginning elements in
the set and a recursive step that expresses the relationship between elements in the set.
Here is a recursive C++ function to calculate n! Notice how closely it follows the
recursive definitions of factorial as shown above.
int fact(int n)
{
if (n == 0)
return (1);
else
return (n * fact(n - 1));
}
fact(4) = 4 * fact(3)
fact(3) = 3 * fact(2)
fact(2) = 2 * fact(1)
fact(1) = 1 * fact(0)
fact(0) is 1 (base case)
When the computer reaches the base case fact(0), there are three suspended
computations. After calculating the value returned for the stopping case, it resumes the
most recently suspended computations to determine the value of fact(1). After that, the
computer completes each of the suspended computations, using each value computed as a
value to plug into another suspended computation, until it reaches and completes the
computation for the original call fact(4). The details of the final computation are
illustrated below:
A.5 Procedure/Algorithm:
A.5 Task
TASK 1:
Scenario: Assume you are making a word document using MS word and you have typed some
statements. Later on you have erased some words from the sentence in particular order by using
backspace. Then you noticed that you have wrongly erased a few more words, now you would like to
regain those deleted words. MS words provide a feature to regain all the deleted words using Ctrl+z in the
opposite order of their deletion.
Consider the above mentioned scenario and identify suitable data structure to implement / simulate the
feature provided by MS words to perform delete and undo operation.
TASK 2:
Scenario: If mathematical expressions are used in the programs, the compiler checks whether the written
expression is syntactically correct or not by checking balanced brackets in the expression.
You keep a Stack of brackets, and every time you encounter an opening bracket, you push it into your
stack. Every time you encounter a closing bracket, you perform a pop operation on your stack. At the end,
you check your stack for being empty. If so, indeed your input expression contained balanced brackets.
Otherwise, it didn't.
PART B
(PART B : TO BE COMPLETED BY STUDENTS)
(Students must submit the soft copy as per following segments within two hours of the
practical. The soft copy must be uploaded on the Blackboard or emailed to the concerned lab
in charge faculties at the end of the practical in case the there is no Black board access
available)
Date of Grading:
Task1:
Task 2:
B.2 Input and Output:
Task1:
Task2:
B.4 Conclusion:
Task 1:
In conclusion, the Stack data structure is an efficient way to implement the undo feature in a
word processing software like MS Word as it can keep track of all the deleted elements in the
order they were deleted and allows for easy undo operations.
Task 2:
This program demonstrates the implementation of a stack data structure to check the balance of brackets
in a mathematical expression. The program takes an expression as input, and using the stack, it checks if
the brackets are balanced or not. The output message displays the status of the balance of brackets in the
expression. This program can be used to check the validity of mathematical expressions before they are
evaluated or used in any other program.
Suppose characters are arriving on a stream reader. Suggest an algorithm to see if the string forms a
palindrome. Capitalization, spacing, and punctuation are ignored.
************************