Data Structures: Radhamadhab Dalai Asst. Professor CSE BIT Mesra, Ranchi
Data Structures: Radhamadhab Dalai Asst. Professor CSE BIT Mesra, Ranchi
Radhamadhab Dalai
Asst. Professor
CSE
BIT Mesra, Ranchi
29-09-2020 1
Stack
29-09-2020 2
Expressions
• Infix: A+B-C
• Prefix : -+ABC
• Postfix: AB+C-
• Example : A+B+C and A+B*C
• AB+C+ and ABC*+
29-09-2020 3
Postfix to Prefix
• Scan the postfix input string from left to right.
• If ‘ch’ is an operand push it to stack
• 1. If ‘ch’ is an operator pop two
elements(operand1 ,operand2).
2. Make a string like str = operator + operand2 +
operand1.
3.Push whole string (str) back to stack
• Repeat untill end of string
• Top of stack will contain whole string as output,
pop.
29-09-2020 4
Postfix to Prefix
• Post fix : ABC*+
Character Stack operation Stack
A PUSH A
B PUSH AB
C PUSH ABC
* POP 2, APPEND, A*BC
PUSH
+ POP 2, APPEND, +A*BC
PUSH
29-09-2020 5
Questions
• What are the applications of circular queue ?
• Revisit queue and singly linked lists and
doubly linked lists.
29-09-2020 6
Polynomial addition
• Addition of two polynomial expressions using
singly linked lists.
• anxn + an-1xn-1 + …….+ a2x2 + a1x +a0
• bnxn + bn-1xn-1 + ……. + b2x2 + b1x +b0
• Cn = an + bn, Cn-1= an-1+bn-1, c0 = a0 + b0
29-09-2020 7
Polynomial addition
• Polynomial representation in linked lists
head1
head2
29-09-2020 8
Polynomial addition
• Scenarios:
• Degree of first polynomial (m) and degree of
second polynomial (n) are not same. m != n
head3
29-09-2020 9
Polynomial addition
• Implementation
• Approach ? A good input set and an output.
• while( ?)
• Time complexity.
29-09-2020 10
Questions
• Questions ?
29-09-2020 11