0% found this document useful (0 votes)
21 views

Experiment 3 DS Student

Uploaded by

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

Experiment 3 DS Student

Uploaded by

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

Data Structure (3130702)

Experiment No – 3
AIM: Classification of Data Structure and Implementation of Stack and Application of Stack in
Conversion of Infix Notation to Postfix Notation, and Implementation of Tower of Hanoi.

3.1 Implement a program for stack that performs following operations using array. (a) PUSH (b)
POP (c) PEEP (d) CHANGE (e) DISPLAY
3.2 Implement a program to convert infix notation to postfix notation using stack.
3.3 Write a program to implement Tower of Hanoi problem.
3.4 Identify widely used application which use stack data structure for implementation of its
important feature.

Date:

Competency and Practical Skills: Logic building and programming

Relevant CO: CO1, CO2, CO5

Objectives: (a) To understand the concepts of stack


(b) To implement stack that performs operations using array
(c) To implement various applications of the stack using array

Equipment/Instruments: Computer System with C/C++ compiler

Safety and necessary Precautions:

✓ Operate computer system carefully and responsibly.


✓ Use required lab resources cautiously

Theory:

Stack
A stack is a data structure that follows the last-in first-out (LIFO) principle, meaning that objects
are inserted and removed from the container in a particular order. In pushdown stacks, only two
operations are allowed: pushing an item onto the stack, and popping an item off the top of the stack.
Access to the stack is limited, as elements can only be added and removed from the top. When an
item is pushed onto the stack, it becomes the new top item. Conversely, when an item is popped off
the stack, it is removed from the top.

To illustrate this concept, consider a stack of books. Just as you can only remove the top book, you
can only add a new book to the top of the stack. A stack can also have a limited capacity. If the
stack is already full and there is no space to add a new item, it is said to be in an overflow state. On
the other hand, if the stack is empty and an item is removed, it is in an underflow state, meaning
that no items are present in the stack to be removed.

A stack is an abstract data structure that operates on the LIFO principle, where the last item added

Page No
Data Structure (3130702)
is the first item to be removed. Items can be inserted and deleted at one end called the top, creating
a structure that resembles a closed tube on one side.

✓ The add operation of the stack is called push operation


✓ The delete operation is called as pop operation.
✓ Push operation on a full stack causes stack overflow.
✓ Pop operation on an empty stack causes stack underflow.
✓ SP is a pointer, which is used to access the top element of the stack.
✓ If you push elements that are added at the top of the stack;
✓ In the same way when we pop the elements, the element at the top of the stack is deleted.

There are two operations applied on stack they are

(1) PUSH
(2) POP

In-fix- to Postfix Conversion:

Procedure to convert from infix expression to postfix expression is as follows:

1. Start scanning the infix expression from left to right.


2. If the symbol scanned is a left parenthesis, push it onto the stack.
3. If the scanned symbol is an operand, place it directly into the postfix expression output.
4. If the symbol scanned is a right parenthesis, continue to pop all items from the stack and
place them into the postfix expression output until a matching left parenthesis is found.
5. If the scanned symbol is an operator, remove all operators from the stack and place them in
the postfix expression output if and only if the precedence of the operator on top of the stack
is greater than or equal to the precedence of the scanned operator. Then push the scanned
operator onto the stack; otherwise, push the scanned operator onto the stack.

3.1 Implement a program for stack that performs following operations using array.
(a) PUSH (b) POP (c) PEEP (d) CHANGE (e) DISPLAY

Program:

#include<stdio.h>
#define size 5
struct stack
{
int a[size],top;
int temp[size];
}s;
// PUSH Operation
void push()
{
int value;
printf(" Enter value to be pushed: ");
scanf("%d", &value);
s.top = s.top + 1;

Page No
Data Structure (3130702)
s.a[s.top] = value;
}
// POP Operation
void pop()
{
printf(" Popped element is %d\n", s.a[s.top]);
s.top = s.top - 1;
}
// PEEP Operation
void peep()
{
printf(" The value at top position is : %d\n", s.a[s.top]);
}
// DISPLAY Operation
void display()
{
int i;
printf(" The stack contains: ");
for(i=s.top; i>=0; i--)
{
printf("\t%d", s.a[i]);
}
printf("\n");
}
// CHANGE Operation
void change(int index, int new_element)
{
int i, j=-1;
for(i=s.top; i>index; i--)
{
s.temp[++j] = s.a[s.top--];
}
s.a[s.top] = new_element;
for(i = j; i>-1; i--)
{
s.a[++s.top] = s.temp[j--];
}
}
void main()
{
s.top = -1;
int choice, index, new_element;
do
{
printf("\n STACK IMPLEMENTATION PROGRAM");
printf("\n 1. PUSH 2. POP 3. PEEP 4. CHANGE 5. DISPLAY 0. EXIT\n");
printf("\n Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
if(s.top == size-1)
{
printf("\tSTACK OVERFLOW\n");
}
else
{
push(); Page No
Data Structure (3130702)
}
break;
case 2:
if(s.top == -1)
{
printf("\tSTACK UNDERFLOW\n");
}
else
{
pop();
}
break;
case 3:
if(s.top == -1)
{
printf("\tStack is empty.\n");
}
else
{
peep();
}
break;
case 4:
printf(" Enter index no : ");
scanf("%d",&index);
if(index<0 || index>s.top)
{
printf("\tINVALID INDEX NUMBER\n");
}
else
{
}
break;
case 5:
printf(" Enter new element: ");
scanf("%d", &new_element);
change(index, new_element);
if(s.top == -1)
{
printf("\t Stack is empty.\n");
}
else
{
}
break;
case 0:
display();
printf("\tEND OF PROGRAM");
break;
default :
printf("\tINVALID CHOICE\n");
}
} while(choice != 0);
}

Page No
Data Structure (3130702)
Output:

STACK IMPLEMENTATION PROGRAM


1. PUSH 2. POP 3. PEEP 4. CHANGE 5. DISPLAY 0. EXIT

Enter your choice: 1


Enter value to be pushed: 10

STACK IMPLEMENTATION PROGRAM


1. PUSH 2. POP 3. PEEP 4. CHANGE 5. DISPLAY 0. EXIT

Enter your choice: 1


Enter value to be pushed: 20

STACK IMPLEMENTATION PROGRAM


1. PUSH 2. POP 3. PEEP 4. CHANGE 5. DISPLAY 0. EXIT

Enter your choice: 1


Enter value to be pushed: 30

STACK IMPLEMENTATION PROGRAM


1. PUSH 2. POP 3. PEEP 4. CHANGE 5. DISPLAY 0. EXIT

Enter your choice: 1


Enter value to be pushed: 40

STACK IMPLEMENTATION PROGRAM


1. PUSH 2. POP 3. PEEP 4. CHANGE 5. DISPLAY 0. EXIT

Enter your choice: 2


Popped element is 40

STACK IMPLEMENTATION PROGRAM


1. PUSH 2. POP 3. PEEP 4. CHANGE 5. DISPLAY 0. EXIT

Enter your choice: 3


The value at top position is : 30

STACK IMPLEMENTATION PROGRAM


1. PUSH 2. POP 3. PEEP 4. CHANGE 5. DISPLAY 0. EXIT

Enter your choice: 2


Popped element is 30

STACK IMPLEMENTATION PROGRAM


1. PUSH 2. POP 3. PEEP 4. CHANGE 5. DISPLAY 0. EXIT

Enter your choice: 5


Enter new element: 30

STACK IMPLEMENTATION PROGRAM


1. PUSH 2. POP 3. PEEP 4. CHANGE 5. DISPLAY 0. EXIT

Page No
Data Structure (3130702)

Enter your choice: 0


The stack contains: 30 10
END OF PROGRAM

3.2 Implement a program to convert infix notation to postfix notation using stack.

Program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Function to return precedence of operators


int prec(char c) {
if (c == '^')
return 3;
else if (c == '/' || c == '*')
return 2;
else if (c == '+' || c == '-')
return 1;
else
return -1;
}

// Function to return associativity of operators


char associativity(char c) {
if (c == '^')
return 'R';
return 'L'; // Default to left-associative
}

// The main function to convert infix expression to postfix expression


void infixToPostfix(char s[]) {
char result[1000];
int resultIndex = 0;
int len = strlen(s);
char stack[1000];
int stackIndex = -1;
int i;
for (i = 0; i < len; i++) {
char c = s[i];

// If the scanned character is an operand, add it to the output string.


if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c
<= '9')) {
result[resultIndex++] = c;
}
// If the scanned character is an ‘(‘, push it to the stack.
else if (c == '(') {
stack[++stackIndex] = c;
}
// If the scanned character is an ‘)’, pop and add to the output string
from the stack
// until an ‘(‘ is encountered.
else if (c == ')') {
Page No
Data Structure (3130702)
while (stackIndex >= 0 && stack[stackIndex] != '(') {
result[resultIndex++] = stack[stackIndex--];
}
stackIndex--; // Pop '('
}
// If an operator is scanned
else {
while (stackIndex >= 0 && (prec(s[i]) < prec(stack[stackIndex]) ||
prec(s[i]) == prec(stack[stackIndex]) &&
associativity(s[i]) == 'L')) {
result[resultIndex++] = stack[stackIndex--];
}
stack[++stackIndex] = c;
}
}

// Pop all the remaining elements from the stack


while (stackIndex >= 0) {
result[resultIndex++] = stack[stackIndex--];
}

result[resultIndex] = '\0';
printf("Postfix notation of: %s\n is given by : %s", s, result);
}

// Driver code
int main() {
char exp[] = "a+b*(c^d-e)";

// Function call
infixToPostfix(exp);

return 0;
}

Output:

Postfix notation of: a+b*(c^d-e)


is given by : abcd^e-*+

3.3 Write a program to implement Tower of Hanoi problem.

Program:

#include <stdio.h>

// C recursive function to solve tower of hanoi puzzle


void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod)
{
if (n == 1)
{
printf("\n Move disk 1 from rod %c to rod %c", from_rod, to_rod);
return;
}
towerOfHanoi(n-1, from_rod, aux_rod, to_rod);
printf("\n Move disk %d from rod %c to rod %c", n, from_rod, to_rod);
Page No
Data Structure (3130702)
towerOfHanoi(n-1, aux_rod, to_rod, from_rod);
}

int main()
{
int n = 4; // Number of disks
towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods
return 0;
}

Output:

Move disk 1 from rod A to rod B


Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C
Move disk 3 from rod A to rod B
Move disk 1 from rod C to rod A
Move disk 2 from rod C to rod B
Move disk 1 from rod A to rod B
Move disk 4 from rod A to rod C
Move disk 1 from rod B to rod C
Move disk 2 from rod B to rod A
Move disk 1 from rod C to rod A
Move disk 3 from rod B to rod C
Move disk 1 from rod A to rod B
Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C

3.4 Identify widely used application which use stack data structure for implementation of its
important feature.

Stack Applications:

1. Stack is used by compilers to check for balancing of parentheses, brackets and braces.
2. Stack is used to evaluate a postfix expression.
3. Stack is used to convert an infix expression into postfix/prefix form.
4. In recursion, all intermediate arguments and return values are stored on the processor’s
stack.
5. During a function call the return address and arguments are pushed onto a stack and on
return they are popped off.
6. Depth first search uses a stack data structure to find an element from a graph.

Conclusion:

Page No
Data Structure (3130702)

Quiz:

(1) List various stack operations

(2) Differentiate FIFO and LIFO

Page No
Data Structure (3130702)

(3) Explain infix, prefix and postfix expressions

Suggested Reference:

1. An Introduction to Data Structures with Applications. by Jean-Paul Tremblay & Paul G.


Sorenson Publisher-Tata McGraw Hill.
2. Data Structures using C & C++ -By Ten Baum Publisher – Prenctice-Hall International
3. Fundamentals of Computer Algorithms by Horowitz, Sahni,Galgotia Pub. 2001 ed.
4. http://www.geeksforgeeks.org/data-structures/
5. http://www.coursera.org/specializations/data-structures-algorithms

References used by the students:

Rubric-wise marks obtained:

Problem Coding Completeness


Logic
Understanding Standards and accuracy Q&A
Rubrics Building (2) Total
(2) (2) (2)
Avg. Good Avg. Good Avg. Good Avg. Good Avg. Good
(1) (2) (1) (2) (1) (2) (1) (2) (1) (2)

Marks

Page No

You might also like