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

Data - Structure - and - Algorithms - Lecture - 5 Linked List, Stacks

Uploaded by

Hasnain Nisar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Data - Structure - and - Algorithms - Lecture - 5 Linked List, Stacks

Uploaded by

Hasnain Nisar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Data Structure Algorithms

and Applications
CT-157
Course Instructor
Engr. Adiba Jafar
Data Structure and
Algorithms
Lecture 5
CHAPTER #5
Linked List
Header Linked List
Traversing a header linked list
Two way Linked List
Representation in memory
Two way header Linked List
CHAPTER #6

STACKS,QUEUES AND
RECURSION
STACKS
EXAMPLE
Array representation of stacks
Push algorithm
Simulation of push
A
POP algorithm
Simulation of POP
Example
Linked representation of stack
PUSH and POP functions
Example of stack
Consider the following stack of characters ,here STACK is
allocated N=8 memory cells
Describe the stack as the following operations takes place
Arithmetic Expression (Polish
Notation)
Let Q be an arithmetic expression involving constant and
operations
LEVEL OF PRECEDENCE
Infix Notation
Infix notation is the notation commonly used in
arithmetical and logical formulae and statements. It is
characterized by the placement of operators between
operands—"infixed operators"—such as the plus sign
in 2 + 2.

EXAMPLES A +B
C–D
F /E
E* F and so on
Polish or Prefix Notation
Polish notation (PN), also known as normal Polish
notation (NPN), Polish prefix notation or
simply prefix notation, is a mathematical notation in
which operators precede their operands, in contrast to
the more common infix notation, in which operators
are placed between operands.
EXAMPLES +AB
-CD
/EF
*QR and so on
Example of infix to prefix
Let
1. E=(A+B)*(C+D) (+AB)*(+CD)
*(+AB)(+CD)
RESULT *+AB+CD

2. E=(A-B)*(C/D) (-AB)*(/CD)
*(-AB)(/CD)
RESULT *-AB/CD
More Examples
3. E=((A+B^D)/(E+F)) +G
((A+{^BD})/(+EF)) +G
{(+A^BD)/(+EF)}+G
{(/+A^BD)(+EF)}+G
{/+A^BD+EF}+G
RESULT +/+A^BD+EFG
Check this video
https://youtu.be/7cnRfPvvy9k
Postfix or reverse polish
notation
Postfix notation is a notation for writing
arithmetic expressions in which the operands appear
before their operators.
EXAMPLES
AB+
BD-
XY/
ST*
Example of infix to postfix
Let
1. E=(A+B)*(C+D) (AB+)*(CD+)
(AB+)(CD+)*
RESULT AB+CD+*

2. E=(A-B)*(C/D) (AB-)*(CD/)
(AB-)(CD/)*
RESULT AB-CD/*
More Examples
3. E=((A+B^D)/(E+F)) +G
((A+{BD^})/(EF+)) +G
{(ABD^+)/(EF+)}+G
{(ABD^+)(EF+)/}+G
{ABD^+EF+/}+G
RESULT ABD^+EF+/G+
Evaluation of postfix expression
Example
Suppose P =5,6,2,+,*,12,4,/,- Apply Algorithm
Step 1 :add ) no P=5,6,2,+,*,12,4,/,-,)
Symbol Scanned Stack Remarks
5 5
6 5,6
2 5,6,2 6+2
+ 5,8
* 40 5*8
12 40,12
4 40,12,4
/ 40,3 12 / 4
- 37 40-3
Transforming infix Expression
into postfix expression
Home Tasks

1.Simulate Procedure 6.3 , 6.4


2.Practice example 6.4,6.5
3.Solved Problems 6.1 6.4,6.7 ,6.8 ,6.9 ,6.10

You might also like