Data Structures and Algorithms - Stack
Data Structures and Algorithms - Stack
DATA STRUCTURE
WHAT?
Linear Data Structure
that implements
Abstract Data Type
(ADT), by following the
LIFO (Last In, First Out)
principle
CHARACTERISTICS OF STACK
Expression Conversion
NOTATIONS
01 02 03
INFIX PREFIX POSTFIX
<CONVENTIONAL> <POLISH> <REVERSE POLISH>
() ^ *, / +, -
Use to break the Exponent Multiplication Addition
Precedence Division Subtraction
[1] PREFIX NOTATION
3+5*2
3+*52
+3*52
[2] PREFIX NOTATION
7+2-5/5
7+2-/55
+72-/55
-+72/55
[3] PREFIX NOTATION
100 / 5 ^ 2 + 8 * 3 - 1
100 / ^ 5 2 + 8 * 3 - 1
/ 100 ^ 5 2 + 8 * 3 - 1
/ 100 ^ 5 2 + * 8 3 - 1
+ / 100 ^ 5 2 * 8 3 - 1
- + / 100 ^ 5 2 * 8 3 1
[4] PREFIX NOTATION
(9 - 3 * 2) + 8 * (2 - 1 ^ 5) ^ 4
(9 - * 3 2) + 8 * (2 - 1 ^ 5) ^ 4
- 9 * 3 2 + 8 * (2 - 1 ^ 5) ^ 4
- 9 * 3 2 + 8 * (2 - ^ 1 5) ^ 4
-9*32+8*-2^15^4
-9*32+8*^-2^154
-9*32+*8^-2^154
+-9*32*8^-2^154
[1] POSTFIX NOTATION
3+5*2
3+52*
352*+
[2] POSTFIX NOTATION
7+2-5/5
7+2-55/
72+-55/
72+55/-
[3] POSTFIX NOTATION
100 / 5 ^ 2 + 8 * 3 - 1
100 / 5 2 ^ + 8 * 3 - 1
100 5 2 ^ / + 8 * 3 - 1
100 5 2 ^ / + 8 3 * - 1
100 5 2 ^ / 8 3 * + - 1
100 5 2 ^ / 8 3 * + 1 -
[4] POSTFIX NOTATION
(9 - 3 * 2) + 8 * (2 - 1 ^ 5) ^ 4
(9 - 3 2 *) + 8 * (2 - 1 ^ 5) ^ 4
9 3 2 * - + 8 * (2 - 1 ^ 5) ^ 4
9 3 2 * - + 8 * (2 - 1 5 ^) ^ 4
932*-+8*215^-^4
932*-+8*215^-4^
932*-+8215^-4^*
932*-8215^-4^*+
REVERSE
POLISH
NOTATION
STACK :: The Calculation Simulation
STACK OPERATIONS
Push: Add an element to the top of a stack
Pop: Remove an element from the top of a stack
IsEmpty: Check if the stack is empty
IsFull: Check if the stack is full
Peek: Get the value of the top element without removing it
Count: Returns the total number of elements in a stack
Change: Changes the element at the given position
Display: It prints all the elements available in the stack
WORKING OF STACK DATA STRUCTURE
[1] POSTFIX NOTATION
3 + 5 * 2 = 13
pop(s)
352*+
pop(s)
create(s) push(10,s)
push(3,s) pop(s)
push(5,s) pop(s)
push(2,s) push(13,s)
[2] POSTFIX NOTATION
7+2-5/5=8
pop(s) push(1,s)
72+55/-
push(9,s) pop(s)
create(s) push(5,s) pop(s)
push(7,s) push(5,s) push(8,s)
push(2,s) pop(s)
pop(s) pop(s)
[3] POSTFIX NOTATION pop(s)
pop(s)
100 / 5 ^ 2 + 8 * 3 - 1 = 27
push(24,s)
100 5 2 ^ / 8 3 * + 1 -
pop(s)
create(s) push(25,s) pop(s)
push(100,s) pop(s) push(28,s)
push(5,s) pop(s) push(1,s)
push(2,s) push(4,s) pop(s)
pop(s) push(8,s) pop(s)
pop(s) push(3,s) push(27,s)