0% found this document useful (0 votes)
13 views13 pages

Manual - For - Exp - No - 03 - Implementation of Stack

The document outlines an experiment for BTech - CSBS students focusing on the Stack data structure and its application in recursion. It includes the aim, prerequisites, expected outcomes, theoretical concepts, algorithms for stack operations, and tasks related to implementing undo features and checking balanced brackets. The conclusion emphasizes the efficiency of the Stack data structure in these applications.

Uploaded by

hatalad181
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views13 pages

Manual - For - Exp - No - 03 - Implementation of Stack

The document outlines an experiment for BTech - CSBS students focusing on the Stack data structure and its application in recursion. It includes the aim, prerequisites, expected outcomes, theoretical concepts, algorithms for stack operations, and tasks related to implementing undo features and checking balanced brackets. The conclusion emphasizes the efficiency of the Stack data structure in these applications.

Uploaded by

hatalad181
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

SVKM’s NMIMS

Mukesh Patel School of Technology Management & Engineering


Computer Engineering Department
Program: BTech - CSBS, Semester II

Course: Data Structure and Algorithms


PART A
(PART A : TO BE REFFERED BY STUDENTS)

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

2. Fundamental concepts of C\C++.

A.3 Outcome:
After successful completion of this experiment students will be able to

1. Identify the need of appropriate selection of data structure


2. Identify the steps of stack data structure selection.
3. Implement stack data structure to solve the given problem
4. Enlist the applications of stack data structure.

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).

The functions performed on stack are:

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

Functions which call themselves are said to be recursive functions.


Therefore, recursion is a problem solving technique which allows a function to call itself
and this technique can be applied when the solution to a problem can be stated in terms of
itself. In this lab we will examine some problems which can be stated in terms of
themselves and show how these functions can be written recursively.

Example: Computing n! (n factorial)

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.

// Recursive factorial function. Assume n >= 0.

int fact(int n)
{
if (n == 0)
return (1);
else
return (n * fact(n - 1));
}

The value of fact(4) is calculated as follows:

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:

Since fact(0) = 1, then fact(1) = 1 * 1 = 1


Since fact(1) = 1, then fact(2) = 2 * 1 = 2
Since fact(2) = 2, then fact(3) = 3 * 2 = 6
Since fact(3) = 6, then fact(4) = 4 * 6 = 24

A.5 Procedure/Algorithm:
A.5 Task

Algorithm for push


begin
if stack is full
return
endif
else
increment top
stack[top] assign value
end else
end procedure
Algorithm for pop
begin
if stack is empty
return
endif
else
store value of stack[top]
decrement top
return value
end else
end procedure
Algorithm for Top:
begin
return stack[top]
end procedure

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.

Write a complete program to implement and demonstrate the same.

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.

Here is an example of expression with balanced brackets:


a = b + (c – (d + e) * (m – n) + t)
Here is an example of expression with imbalanced brackets:
a = b + (c – (d + e) * (m – n) + t
Identify suitable data structure to implement the given scenario with a proper program. Input a similar
mathematical statement as string and check if it has balanced or imbalanced brackets. Display the
message accordingly.

The idea is:

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)

Roll No.E043 Name:Parag Shirke

Class :Beech CSBS Batch :E2

Date of Experiment: Date of Submission

Grade : Time of Submission:

Date of Grading:

B.1 Software Code written by student:

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.

B.5 Question of Curiosity


Checking for palindrome string

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.
************************

You might also like