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

Data Structures and Algorithms - Stack

The document discusses stacks, which are linear data structures that follow the LIFO (last in, first out) principle. Stacks have characteristics like only allowing insertion and deletion at one end (the top). Practical applications of stacks include recursion call stacks, undo/redo functions, memory management, and browser navigation. The document also covers infix, prefix, and postfix notation for mathematical expressions and using a stack to evaluate expressions in postfix notation through push and pop operations.

Uploaded by

erikalast.acad
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)
29 views

Data Structures and Algorithms - Stack

The document discusses stacks, which are linear data structures that follow the LIFO (last in, first out) principle. Stacks have characteristics like only allowing insertion and deletion at one end (the top). Practical applications of stacks include recursion call stacks, undo/redo functions, memory management, and browser navigation. The document also covers infix, prefix, and postfix notation for mathematical expressions and using a stack to evaluate expressions in postfix notation through push and pop operations.

Uploaded by

erikalast.acad
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/ 26

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

 Insertion and deletion are


allowed at one end only, i.e.,
the ‘Top' of the stack.

 The ‘Base' of the stack points


to the first inserted element in
the stack and the 'top' of the
stack points to the last
inserted element.
PRACTICAL APPLICATION
Significant place in the realm of IT education
1] RECURSION CALL STACK
2] UNDO/REDO FUNCTIONS
3] MEMORY MANAGEMENT
4] BROWSER NAVIGATION
5] CALCULATOR
“The method of simplifying
artihmetic equation inorder
to calculate the value of
expression”

Expression Conversion
NOTATIONS
01 02 03
INFIX PREFIX POSTFIX
<CONVENTIONAL> <POLISH> <REVERSE POLISH>

Operand 2 Operator Operand 1


Operator Operand 1 Operand 2
Operand 2 Operand 2 Operator

2+3 +23 23+


OPERATOR PRECEDENCE

() ^ *, / +, -
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)

You might also like