SlideShare a Scribd company logo
1 of 38
Data Structure & Algorithm
CS-102
Ashok K Turuk
• There are certain frequent situations in
computer science when one wants to
restrict insertion and deletions so that
they can take place only at the
beginning or at the end not in the
middle.
– Stack
– Queue
2
Stack

3
Stack
• A Stack is a list of elements in which an
element may be inserted or deleted only
at one end, call top of the Stack
• Two basic operations are associated
with Stack
– Push : Insert an element into a stack
– Pop : Delete an element from a stack

4
Stack
• Stores a set of elements in a
particular order
• Stack principle: LAST IN FIRST
OUT= LIFO

• It means: the last element inserted is
the first one to be removed
• Which is the first element to pick up?

5
Last In First Out

D top
top
top C
C
B
B
B
top A
A
A
A
Initial 1
2
3
4

C
B
A
5
6
Last In First Out

8

top C
B
B
top A
A
A
7
6
5

top

7
Representation of Stack
Stack can be represented in two
different ways :
[1] Linear ARRAY
[2] One-way Linked list

8
Array Representation of Stack
STACK
AA
1

BB
2

TOP 3

CC
3

4

5

7

7

8

MAXSTK 8

TOP = 0 or TOP = NULL will indicates
that the stack is empty

9
PUSH Operation
Perform the following steps to PUSH an
ITEM onto a Stack
[1] If TOP = MAXSTK, Then print:
Overflow, Exit [ Stack already filled]
[2] Set TOP = TOP + 1
[3] Set STACK[TOP] = ITEM [Insert
Item into new TOP Position]
[4] Exit
10
POP Operation
Delete top element of STACK and assign
it to the variable ITEM
[1] If TOP = 0, Then print Underflow and
Exit
[2] Set ITEM = STACK[TOP]
[3] Set TOP = TOP -1
[4] Exit

11
Linked List Representation of
Stack
TOP

Head
CC

Top Of Stack

BB

AA X

Bottom Of Stac

12
PUSH Operation
• Push operation into the stack is
accomplished by inserting a node into
the front of the list [Insert it as the
first node in the list]
TOP
CC

BB

PUSH DD into STACK

AA X
DD
13
PUSH Operation
STACK before PUSH Operation
TOP
CC
TOP

BB

AA X

STACK After PUSH Operation

DD

CC

BB

AA X
14
PUSH Operation
[1] NEW->INFO = ITEM
[2] NEW->LINK = TOP
[3] TOP = NEW
[4] Exit

15
POP Operation
• POP operation is accomplished by
deleting the node pointed to by the
TOP pointer [Delete the first node in
the list]

16
POP Operation
STACK before POP Operation

TOP

BB

CC

DD

AA X

STACK After POP Operation
TOP

CC

BB

AA X
17
POP Operation
[1] IF TOP == NULL Then Write Overflow
and Exit
[2] Set ITEM = TOP->INFO
[3] Set TOP = TOP->LINK
[4] Exit

18
Arithmetic Expression; Polish
Notation
• Let Q be an arithmetic expression
involving constant and operations
• Find the value of Q using reverse Polish
(Postfix) Notation

19
Polish Notation
• Evaluate the following parenthesis-free
arithmetic expression
2 î 3 + 5 * 2 î 2 – 12 / 6
Evaluate the exponentiation to obtain
8 + 5 * 4 – 12 /6
Evaluate Multiplication and Division
8 + 20 – 2
Evaluate Addition and Subtraction
20
20
Polish Notation

• Infix notation [Operator symbol is
placed between two Operand]
A + B , C – D , E * F , G /H
(A + B) * C and A + (B*C)
• Polish Notation [Operator symbol is
placed before its operand]
+AB, -CD, *EF , /GH
Polish Notations are frequently called
Prefix
21
Polish Notation
• Infix expression to Polish Notation
[ ] to indicate a partial translation
(A+B)*C = [+AB]*C = *+ABC
A+(B*C) = A+[*BC] = +A*BC

(A+B)/(C-D) = [+AB]/[-CD] = /+AB-CD
22
Polish Notation
• The fundamental property of Polish
notation is that the order in which the
operations are to be performed is
completely determined by the positions
of the operators and operand in the
expression.
• One never needs parenthesis when
writing expression in Polish notations
23
Reverse Polish Notation
• Operator symbol is placed after its two
operand
AB+, CD-, EF*, GC/
One never needs parenthesis to
determine the order of the operation
in any arithmetic expression written in
reverse Polish notation.
Also known as Postfix notation
24
• Computer usually evaluates an
arithmetic expression written in infix
notation in two steps:
• First Step: Converts the expression to
Postfix notation
• Second Step: Evaluates the Postfix
expression.

25
Evaluation of Postfix Expression
• Algorithm to find the Value of an
arithmetic expression P Written in
Postfix
[1] Add a right parenthesis „)” at the end
of P. [This act as delimiter]
[2] Scan P from left to right and repeat
Steps 3 and 4 for each element of P
until the delimiter “)” is encountered

26
Evaluation of Postfix Expression
[3] If an operand is encountered, put it on
STACK
[4] If an operator is encountered, then
(a) Remove the two top elements of
STACK, where A is the top element and
B is the next-to-top element
(b) Evaluate B A
(c ) Place the result of (b) on STACK
27
Evaluation of Postfix Expression
[5] Set Value equal to the top element of
STACK
[6] Exit

28
Example

• P = 5, 6, 2, + , *, 12, 4, /, - [Postfix]
• Q = 5 * ( 6 + 2) – 12 / 4 [Infix]
• P:
5,
(1)

6, 2, +, *, 12,
(2) (3) (4) (5) (6)

4, /, -, )
(7) (8) (9) (10)

29
5,
(1)

6, 2, +, *, 12,
(2) (3) (4) (5) (6)
Symbol
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)

Scanned
5
6
2
+
*
12
4
/
-

4, /, -, )
(7) (8) (9) (10)

STACK
5
5,
6
5,
6,
5,
8
40
40, 12
40, 12,
40, 3
37

2

4

30
Infix to Postfix
• Q is an arithmetic expression written in
infix notation
• î , * , / , + , • Three level of precedence

31
Infix to Postfix
• Q is an arithmetic expression written in
infix notation. This algorithm finds the
equivalent postfix notation
[1] Push “(“ onto STACK and “)” to the end
of Q
[2] Scan Q from Left to Right and Repeat
Steps 3 to 6 for each element of Q
until the STACK is empty

32
[3] If an operand is encountered, add it
to P
[4] If a left parenthesis is encountered,
push it onto STACK
[5] If an operator is encountered, then:
(a) Repeatedly pop from STACK and
to P each operator (on the top of
STACK) which has same precedence as
or higher precedence than .
33
(b) Add to STACK
[6] If a right parenthesis is encountered,
then
(a) Repeatedly pop from the STACK
and add to P each operator (on top of
STACK) until a left parenthesis is
encountered.
(b) Remove the left parenthesis. [Do
not add it to P]
[7] Exit
34
Example

• Q:A+(B*C–(D/E îF)*G)*H
A + ( B * C - ( D / E î F ) * G ) * H )
1 2 3 4 5 6 7 8 9
20

35
A + ( B * C - ( D / E î F ) * G ) * H )
1 2 3 4 5 6 7 8 9
20
Symbol
Scanned

STACK

Expression P

1
2
3
4
5
6
7
8

(
(+
(+(
(+(
(+(
(+(
(+(
(+(

A
A
A
A
A
A
A
A

A
+
(
B
*
C
(

*
*
-(

B
B
BC
BC*
BC*
36
A + ( B * C - ( D / E î F ) * G ) * H )
1 2 3 4 5 6 7 8 9
20
Symbol
Scanned

STACK

Expression P

8
9
10
11
12
13
14
15

(+(
(+(
(+(
(+(
(+(
(+(
(+(
(+(

A
A
A
A
A
A
A
A

(
D
/
E
î
F
)
*

-(
-(
-(/
-(/
-(/î
-(/î
-*

BC*
BC*D
BC*D
BC*DE
BC*DE
BC*DEF
BC*DEFî/
BC*DEFî/
37
A + ( B * C - ( D / E î F ) * G ) * H )
1 2 3 4 5 6 7 8 9
20
Symbol
Scanned

STACK

Expression P

15 *
16 G
17 )
18 *
19 H
20 )

(+(-*
(+(-*
(+
(+*
(+*
A B C

ABC*DEFî/
A B C * D E F î /G
ABC*DEFî/G*ABC*DEFî/G*ABC*DEFî/G*-H
* D E F î / G * - H * +

38

More Related Content

What's hot

Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructurerajshreemuthiah
 
Sorting Techniques
Sorting TechniquesSorting Techniques
Sorting TechniquesRafay Farooq
 
STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureYaksh Jethva
 
Queue data structure
Queue data structureQueue data structure
Queue data structureanooppjoseph
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure Janki Shah
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...Umesh Kumar
 
Data structures & algorithms lecture 3
Data structures & algorithms lecture 3Data structures & algorithms lecture 3
Data structures & algorithms lecture 3Poojith Chowdhary
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structureadeel hamid
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptxParagAhir1
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary treeKrish_ver2
 

What's hot (20)

Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
 
Data structure and its types.
Data structure and its types.Data structure and its types.
Data structure and its types.
 
Sorting Techniques
Sorting TechniquesSorting Techniques
Sorting Techniques
 
Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point
 
STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data Structure
 
Stack
StackStack
Stack
 
Linked List
Linked ListLinked List
Linked List
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
 
Data structures & algorithms lecture 3
Data structures & algorithms lecture 3Data structures & algorithms lecture 3
Data structures & algorithms lecture 3
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 
Insertion sort algorithm power point presentation
Insertion  sort algorithm power point presentation Insertion  sort algorithm power point presentation
Insertion sort algorithm power point presentation
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
 
Linked list
Linked listLinked list
Linked list
 
Queues
QueuesQueues
Queues
 
Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
 
Data Structures
Data StructuresData Structures
Data Structures
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 

Similar to Data Structures and Algorithms Explained

Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTUREMandeep Singh
 
Data Structure and Algorithms Stacks
Data Structure and Algorithms StacksData Structure and Algorithms Stacks
Data Structure and Algorithms StacksManishPrajapati78
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7Kumar
 
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxhaaamin01
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxchandankumar364348
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-listpinakspatel
 
13 Stacks and Queues.pptx
13 Stacks and Queues.pptx13 Stacks and Queues.pptx
13 Stacks and Queues.pptxssuserb7c8b8
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationRAtna29
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacksmaamir farooq
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxprakashvs7
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manualnikshaikh786
 

Similar to Data Structures and Algorithms Explained (20)

5.stack
5.stack5.stack
5.stack
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
Unit 3 stack
Unit 3   stackUnit 3   stack
Unit 3 stack
 
Data Structure and Algorithms Stacks
Data Structure and Algorithms StacksData Structure and Algorithms Stacks
Data Structure and Algorithms Stacks
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
 
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptx
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
The Stack And Recursion
The Stack And RecursionThe Stack And Recursion
The Stack And Recursion
 
Stack Operation In Data Structure
Stack Operation In Data Structure Stack Operation In Data Structure
Stack Operation In Data Structure
 
STACK1.pptx
STACK1.pptxSTACK1.pptx
STACK1.pptx
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
13 Stacks and Queues.pptx
13 Stacks and Queues.pptx13 Stacks and Queues.pptx
13 Stacks and Queues.pptx
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparation
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacks
 
Stack
StackStack
Stack
 
LEC3-DS ALGO(updated).pdf
LEC3-DS  ALGO(updated).pdfLEC3-DS  ALGO(updated).pdf
LEC3-DS ALGO(updated).pdf
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
 
Introduction To Stack
Introduction To StackIntroduction To Stack
Introduction To Stack
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 

More from Aakash deep Singhal

Lecture 15 data structures and algorithms
Lecture 15 data structures and algorithmsLecture 15 data structures and algorithms
Lecture 15 data structures and algorithmsAakash deep Singhal
 
Lecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsLecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsAakash deep Singhal
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsAakash deep Singhal
 
Lecture 12 data structures and algorithms
Lecture 12 data structures and algorithmsLecture 12 data structures and algorithms
Lecture 12 data structures and algorithmsAakash deep Singhal
 
Lecture 11 data structures and algorithms
Lecture 11 data structures and algorithmsLecture 11 data structures and algorithms
Lecture 11 data structures and algorithmsAakash deep Singhal
 
Lecture 10 data structures and algorithms
Lecture 10 data structures and algorithmsLecture 10 data structures and algorithms
Lecture 10 data structures and algorithmsAakash deep Singhal
 
Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsLecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsAakash deep Singhal
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsAakash deep Singhal
 
Lecture 5 data structures and algorithms
Lecture 5 data structures and algorithmsLecture 5 data structures and algorithms
Lecture 5 data structures and algorithmsAakash deep Singhal
 
Lecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsLecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsAakash deep Singhal
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsAakash deep Singhal
 
Lecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsLecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsAakash deep Singhal
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsAakash deep Singhal
 
Lecture 16 data structures and algorithms
Lecture 16 data structures and algorithmsLecture 16 data structures and algorithms
Lecture 16 data structures and algorithmsAakash deep Singhal
 

More from Aakash deep Singhal (15)

Subsidence in coal mines
Subsidence in coal minesSubsidence in coal mines
Subsidence in coal mines
 
Lecture 15 data structures and algorithms
Lecture 15 data structures and algorithmsLecture 15 data structures and algorithms
Lecture 15 data structures and algorithms
 
Lecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsLecture 14 data structures and algorithms
Lecture 14 data structures and algorithms
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
 
Lecture 12 data structures and algorithms
Lecture 12 data structures and algorithmsLecture 12 data structures and algorithms
Lecture 12 data structures and algorithms
 
Lecture 11 data structures and algorithms
Lecture 11 data structures and algorithmsLecture 11 data structures and algorithms
Lecture 11 data structures and algorithms
 
Lecture 10 data structures and algorithms
Lecture 10 data structures and algorithmsLecture 10 data structures and algorithms
Lecture 10 data structures and algorithms
 
Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsLecture 9 data structures and algorithms
Lecture 9 data structures and algorithms
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithms
 
Lecture 5 data structures and algorithms
Lecture 5 data structures and algorithmsLecture 5 data structures and algorithms
Lecture 5 data structures and algorithms
 
Lecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsLecture 4 data structures and algorithms
Lecture 4 data structures and algorithms
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
 
Lecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsLecture 2 data structures and algorithms
Lecture 2 data structures and algorithms
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
 
Lecture 16 data structures and algorithms
Lecture 16 data structures and algorithmsLecture 16 data structures and algorithms
Lecture 16 data structures and algorithms
 

Recently uploaded

Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 

Recently uploaded (20)

Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 

Data Structures and Algorithms Explained

  • 1. Data Structure & Algorithm CS-102 Ashok K Turuk
  • 2. • There are certain frequent situations in computer science when one wants to restrict insertion and deletions so that they can take place only at the beginning or at the end not in the middle. – Stack – Queue 2
  • 4. Stack • A Stack is a list of elements in which an element may be inserted or deleted only at one end, call top of the Stack • Two basic operations are associated with Stack – Push : Insert an element into a stack – Pop : Delete an element from a stack 4
  • 5. Stack • Stores a set of elements in a particular order • Stack principle: LAST IN FIRST OUT= LIFO • It means: the last element inserted is the first one to be removed • Which is the first element to pick up? 5
  • 6. Last In First Out D top top top C C B B B top A A A A Initial 1 2 3 4 C B A 5 6
  • 7. Last In First Out 8 top C B B top A A A 7 6 5 top 7
  • 8. Representation of Stack Stack can be represented in two different ways : [1] Linear ARRAY [2] One-way Linked list 8
  • 9. Array Representation of Stack STACK AA 1 BB 2 TOP 3 CC 3 4 5 7 7 8 MAXSTK 8 TOP = 0 or TOP = NULL will indicates that the stack is empty 9
  • 10. PUSH Operation Perform the following steps to PUSH an ITEM onto a Stack [1] If TOP = MAXSTK, Then print: Overflow, Exit [ Stack already filled] [2] Set TOP = TOP + 1 [3] Set STACK[TOP] = ITEM [Insert Item into new TOP Position] [4] Exit 10
  • 11. POP Operation Delete top element of STACK and assign it to the variable ITEM [1] If TOP = 0, Then print Underflow and Exit [2] Set ITEM = STACK[TOP] [3] Set TOP = TOP -1 [4] Exit 11
  • 12. Linked List Representation of Stack TOP Head CC Top Of Stack BB AA X Bottom Of Stac 12
  • 13. PUSH Operation • Push operation into the stack is accomplished by inserting a node into the front of the list [Insert it as the first node in the list] TOP CC BB PUSH DD into STACK AA X DD 13
  • 14. PUSH Operation STACK before PUSH Operation TOP CC TOP BB AA X STACK After PUSH Operation DD CC BB AA X 14
  • 15. PUSH Operation [1] NEW->INFO = ITEM [2] NEW->LINK = TOP [3] TOP = NEW [4] Exit 15
  • 16. POP Operation • POP operation is accomplished by deleting the node pointed to by the TOP pointer [Delete the first node in the list] 16
  • 17. POP Operation STACK before POP Operation TOP BB CC DD AA X STACK After POP Operation TOP CC BB AA X 17
  • 18. POP Operation [1] IF TOP == NULL Then Write Overflow and Exit [2] Set ITEM = TOP->INFO [3] Set TOP = TOP->LINK [4] Exit 18
  • 19. Arithmetic Expression; Polish Notation • Let Q be an arithmetic expression involving constant and operations • Find the value of Q using reverse Polish (Postfix) Notation 19
  • 20. Polish Notation • Evaluate the following parenthesis-free arithmetic expression 2 î 3 + 5 * 2 î 2 – 12 / 6 Evaluate the exponentiation to obtain 8 + 5 * 4 – 12 /6 Evaluate Multiplication and Division 8 + 20 – 2 Evaluate Addition and Subtraction 20 20
  • 21. Polish Notation • Infix notation [Operator symbol is placed between two Operand] A + B , C – D , E * F , G /H (A + B) * C and A + (B*C) • Polish Notation [Operator symbol is placed before its operand] +AB, -CD, *EF , /GH Polish Notations are frequently called Prefix 21
  • 22. Polish Notation • Infix expression to Polish Notation [ ] to indicate a partial translation (A+B)*C = [+AB]*C = *+ABC A+(B*C) = A+[*BC] = +A*BC (A+B)/(C-D) = [+AB]/[-CD] = /+AB-CD 22
  • 23. Polish Notation • The fundamental property of Polish notation is that the order in which the operations are to be performed is completely determined by the positions of the operators and operand in the expression. • One never needs parenthesis when writing expression in Polish notations 23
  • 24. Reverse Polish Notation • Operator symbol is placed after its two operand AB+, CD-, EF*, GC/ One never needs parenthesis to determine the order of the operation in any arithmetic expression written in reverse Polish notation. Also known as Postfix notation 24
  • 25. • Computer usually evaluates an arithmetic expression written in infix notation in two steps: • First Step: Converts the expression to Postfix notation • Second Step: Evaluates the Postfix expression. 25
  • 26. Evaluation of Postfix Expression • Algorithm to find the Value of an arithmetic expression P Written in Postfix [1] Add a right parenthesis „)” at the end of P. [This act as delimiter] [2] Scan P from left to right and repeat Steps 3 and 4 for each element of P until the delimiter “)” is encountered 26
  • 27. Evaluation of Postfix Expression [3] If an operand is encountered, put it on STACK [4] If an operator is encountered, then (a) Remove the two top elements of STACK, where A is the top element and B is the next-to-top element (b) Evaluate B A (c ) Place the result of (b) on STACK 27
  • 28. Evaluation of Postfix Expression [5] Set Value equal to the top element of STACK [6] Exit 28
  • 29. Example • P = 5, 6, 2, + , *, 12, 4, /, - [Postfix] • Q = 5 * ( 6 + 2) – 12 / 4 [Infix] • P: 5, (1) 6, 2, +, *, 12, (2) (3) (4) (5) (6) 4, /, -, ) (7) (8) (9) (10) 29
  • 30. 5, (1) 6, 2, +, *, 12, (2) (3) (4) (5) (6) Symbol (1) (2) (3) (4) (5) (6) (7) (8) (9) Scanned 5 6 2 + * 12 4 / - 4, /, -, ) (7) (8) (9) (10) STACK 5 5, 6 5, 6, 5, 8 40 40, 12 40, 12, 40, 3 37 2 4 30
  • 31. Infix to Postfix • Q is an arithmetic expression written in infix notation • î , * , / , + , • Three level of precedence 31
  • 32. Infix to Postfix • Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix notation [1] Push “(“ onto STACK and “)” to the end of Q [2] Scan Q from Left to Right and Repeat Steps 3 to 6 for each element of Q until the STACK is empty 32
  • 33. [3] If an operand is encountered, add it to P [4] If a left parenthesis is encountered, push it onto STACK [5] If an operator is encountered, then: (a) Repeatedly pop from STACK and to P each operator (on the top of STACK) which has same precedence as or higher precedence than . 33
  • 34. (b) Add to STACK [6] If a right parenthesis is encountered, then (a) Repeatedly pop from the STACK and add to P each operator (on top of STACK) until a left parenthesis is encountered. (b) Remove the left parenthesis. [Do not add it to P] [7] Exit 34
  • 35. Example • Q:A+(B*C–(D/E îF)*G)*H A + ( B * C - ( D / E î F ) * G ) * H ) 1 2 3 4 5 6 7 8 9 20 35
  • 36. A + ( B * C - ( D / E î F ) * G ) * H ) 1 2 3 4 5 6 7 8 9 20 Symbol Scanned STACK Expression P 1 2 3 4 5 6 7 8 ( (+ (+( (+( (+( (+( (+( (+( A A A A A A A A A + ( B * C ( * * -( B B BC BC* BC* 36
  • 37. A + ( B * C - ( D / E î F ) * G ) * H ) 1 2 3 4 5 6 7 8 9 20 Symbol Scanned STACK Expression P 8 9 10 11 12 13 14 15 (+( (+( (+( (+( (+( (+( (+( (+( A A A A A A A A ( D / E î F ) * -( -( -(/ -(/ -(/î -(/î -* BC* BC*D BC*D BC*DE BC*DE BC*DEF BC*DEFî/ BC*DEFî/ 37
  • 38. A + ( B * C - ( D / E î F ) * G ) * H ) 1 2 3 4 5 6 7 8 9 20 Symbol Scanned STACK Expression P 15 * 16 G 17 ) 18 * 19 H 20 ) (+(-* (+(-* (+ (+* (+* A B C ABC*DEFî/ A B C * D E F î /G ABC*DEFî/G*ABC*DEFî/G*ABC*DEFî/G*-H * D E F î / G * - H * + 38