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

CS2x1 DSA Spring 2023 L2

This document provides an overview of stacks as an abstract data structure and their applications. It defines stacks as LIFO data structures and covers their basic operations like push, pop, and peek. Exercises are presented on evaluating arithmetic expressions using stacks. The implementation of stacks using arrays is demonstrated, including functions for push, pop, checking for empty/full stacks, and traversal. Finally, applications of stacks like recursion and arithmetic expression evaluation are discussed.

Uploaded by

220120025
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)
13 views16 pages

CS2x1 DSA Spring 2023 L2

This document provides an overview of stacks as an abstract data structure and their applications. It defines stacks as LIFO data structures and covers their basic operations like push, pop, and peek. Exercises are presented on evaluating arithmetic expressions using stacks. The implementation of stacks using arrays is demonstrated, including functions for push, pop, checking for empty/full stacks, and traversal. Finally, applications of stacks like recursion and arithmetic expression evaluation are discussed.

Uploaded by

220120025
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/ 16

CS2x1:Data Structures and Algorithms

Koteswararao Kondepu
[email protected]
Recap
• Data type
• Data Structures
• Classifications of Data Structures
o Primitive data structures → basic data structures
o Non-Primitive data structures → complicated data structures
▪ Linear data structures
▪ Non-Linear data structures
• Abstract Data Structures
o Stack (LIFO)
▪ push/add/insert
▪ pop/remove/delete ▪ overflow
▪ top/peek ▪ underflow
Outline

• Exercises on Stack
• Implementation of Stack
• Applications of Stack
Exercise#1: Data Structure
Select the following correct options which define “Data Structure”!

a. Data structure is a special format for organizing and storing data


b. Data structure is used to denote a particular way of organizing data for
particular type of operations
c. Data structure is a data organization, management, and storage format
that enables efficient access and modification
d. None of the above
Exercise#2: Stack

• What is the output of the program for the following input?


432*6*+

Rules: (i) Read the element from Left to Right


(ii) If it is an operand push it into stack
(iii) if it is an operator →
• pop top 2 elements
• apply the operators on the popped elements
• push the results on to the stack
(iv) Pop → Results
Exercise#3: Stack

• What is the output of the program for the following input?


432*6*-

Rules: (i) Read the element from Left to Right


(ii) If it is an operand push it into stack
(iii) if it is an operator →
• pop top 2 elements
• apply the operators on the popped elements
• push the results on to the stack
(iv) Pop → Results
Implementation: Push
Stack Data Structures

Push Pop IsStackFull IsStackEmpty Traversal

void Push(){ int IsStackFull(){


int Element; if(Top == StackSize-1){
1 if(!IsStackFull()){ return 1;
}
printf("Enter element\n"); else
2 return 0;
scanf("%d", &Element); }
3
Top++;
4 Stack[Top] = Element;
}
else
5
printf("Element cannot be pushed as stack is already
full \n");}
Implementation: Pop
Stack Data Structures

Push Pop IsStackFull IsStackEmpty Traversal

void Pop(){ int IsStackEmpty


if(!IsStackEmpty()){ (){
printf("Popped out element is: %d \n", if(Top == -1){
Stack[Top]); return 1;
//Stack[Top] = -1; ****** }
Top--; else
} return 0;}
else
printf("Stack is already empty, we cannot pop the element
from it\n");
}
Implementation: Traversal
Stack Data Structures

Push Pop IsStackFull IsStackEmpty Traversal

void PrintStack(){ //Top ********


int i=0;

for(i=0; i<StackSize; i++){


printf("Stack[%d] = %d \n", i, Stack[i]);
}
}
Stack Application: Recursion

• Recursion: (i) Any function which calls itself is called recursive. • Base case
(ii) Recursion terminates → we need to make sure • Sub task
• Recursive case
(iii) The small-small recursive functions should be convergence
(iv) The code is shorter

//Calculate the factorial of a positive integer


int Fact(int n){
if (n == 1) // base case: fact of 0 or 1
return 1; 3 1*Fact(1-1)
else if (n == 0) 2 2*Fact(2-1)
return 1; 1 3*Fact(3-1)
else //recursive case: multiply n by (n- 0 4*Fact(4-1)
1) factorial
return n*Fact(n-1);
}
Stack Application: Recursion (1)

void do (int n) { do (3)


if (n > 0){
do (n-1);
print (n);
do (n-1); do (2)
} 3
do (2)
}
2

do (1) 2 do (1) do (1) do (1)

do (0) do (0) do (0) 1 do (0) do (0) 1 do (0) do (0) 1 do (0)


1
Stack Applications: Arithmetic expression evaluation
• Arithmetic expression → Consists of operands and operators.
• Arithmetic notation → Arrangement of operators and operands to write the
arithmetic expression
• Prefix expression (or Polish expression) → The operators in the expression are
placed before the operands on which the operator works. e.g: a+b*c → +a*bc
• Infix expression → The operators in the expression are placed in between the
operands on which the operator works. e.g: a+b*c
• Postfix expression → The operators in the expression are placed after the
operands on which the operator works. e.g: a+b*c → abc*+
Stack Applications: Arithmetic expression evaluation
Precedence Operators Associativity
1 () [] -> . ++ -- Left to Right
2 + – ! ~ ++ — (type)* & sizeof() Right to Left
3 */% Left to Right
4 +– Left to Right
5 <<, >> Left to Right
6 < <= > >= Left to Right
7 == != Left to Right
8 & Left to Right
9 ^ Left to Right
10 | Left to Right
11 && Left to Right
12 || Left to Right
13 ?: Right to Left
14 = += -+ *= /= %= >>= <<= &= ^= |= Right to Left
Stack Applications: Arithmetic expression evaluation (1)
• Convert the following infix expression into the postfix expression
Infix expression: a*(b+c+d)
Infix Stack Postfix
a*(b+c+d)
a*(b+c+d)
a*(b+c+d)
a*(b+c+d)
a*(b+c+d)
a*(b+c+d)
a*(b+c+d)
a*(b+c+d)
a*(b+c+d) a*(b+c+d) \0
Stack Applications: Arithmetic expression evaluation (2)
• Convert the following infix expression into the postfix expression
Infix expression: x+y-z+(s^t)*u/v
Infix Stack Postfix

x+y-z+(s^t)*u/v
thank you!
email:
[email protected]

NEXT Class: 24/04/2023

You might also like