You are on page 1of 13

Part - I

Data Structures
Data Structures
A data structure is a particular way of organizing
data in a computer so that it can be used
effectively.
For example, we can store a list of items having the
same data-type using the array data structure.
Data Structures
Topics:
• Array
• Stack
• Queue
• Linked List
• Binary Tree
• Binary Search Tree
Data Structures - Stack
Stack is a linear data structure which follows a particular order in
which the operations are performed. The order may be LIFO(Last In
First Out) or FILO(First In Last Out).
Mainly the following basic operations are performed in the stack:
Push: Adds an item in the stack. If the stack is full, then it is said to
be an Overflow condition.
Pop: Removes an item from the stack. The items are popped in the
reversed order in which they are pushed. If the stack is empty, then
it is said to be an Underflow condition.
Data Structures - Stack

There are many real-life examples of a stack. Consider the


simple example of plates stacked over one another in a
canteen. The plate which is at the top is the first one to be
removed, i.e. the plate which has been placed at the
bottommost position remains in the stack for the longest
period of time. So, it can be simply seen to follow LIFO/FILO
order.
Data Structures - Stack
Applications of stack:
• Balancing of symbols
• Infix to Postfix /Prefix conversion
• Redo-undo features at many places like editors, photoshop.
• Forward and backward feature in web browsers
• Used in many algorithms like Tower of Hanoi, tree traversals.
• Other applications can be Backtracking, Knight tour
problem, rat in a maze, N queen problem and sudoku solver
Data Structures - Stack
Implementation:
There are two ways to implement a stack:
• Using array
• Using linked list
Implementing Stack using Arrays : push() and pop() operations
boolean push(int x)
{
if (top >= (MAX - 1)) {
System.out.println("Stack Overflow");
return false;
}
else {
a[++top] = x;
System.out.println(x + " pushed into stack");
return true;
}
}
Data Structures - Stack
Implementing Stack using Arrays : push() and pop()
operations
int pop()
{
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
}
else {
int x = a[top--];
return x;
}
}
Stack | (Infix to Postfix / Infix to Prefix)

• Infix expression:T he expression of the form a op b.


When an operator is in-between every pair of operands.
Eg. (A + B) operator in between the operands
• Postfix expression: The expression of the form a b op.
When an operator is preceded by every pair of operands.
Eg. (A B +) operator follows the operands
• Prefix expression : The expression of the form op a b .
When an operator is followed by every pair of operands.
Eg. ( + A B) when operator precedes the operands.
Stack | (Infix to Postfix / Infix to Prefix)

• Rules to convert and Infix expression into Prefix or


Postfix forms:
1. Enclose the parts of the expression under the
parenthesis according to the precedence of the
operators.
2. Express the contents of the brackets by using operator
before or after the operands to convert to prefix or
postfix form.
3. As the whole expression gets converted, remove all the
brackets.
Stack | (Infix to Postfix)

• Let us consider an expression : a + (b * c) + d


Solving from left to right according to the precedence of
operator:
Brackets,
power (^),
*, /, % L to R – which ever comes first
+, - L to R – whichever comes first
= a + (b c * ) + d
= abc*++d
= abc*+d+
Stack | (Infix to Prefix)

• Let us consider an expression : a + (b * c) + d


Solving from left to right according to the precedence of
operator:
= a + (* b c ) + d
= +a*bc +d
= ++a*bcd
Stack | Exercise
1. Convert the given infix expressions to prefix form.
a. A + B * C / D
b. (A – B) / C * (D – E) + F
c. ((x * y – a * b) – d / f + g) + f
d. A / (B + C) +D * ( M – N)
e. A + B * C ^ D – (E / F + G)
2. Convert the given infix expressions to postfix form:
a. A + B * C / D * E ^ F
b. A / (B + C) + D * (E – F)
c. A + B * C ^ D – (M / N + P)
d. A + B / C + M – N * P + K
e. (A – B) + (C / D) * E

You might also like