Stack Data Structure
Stack Data Structure
[Link]
Stack
“Push” is the term used to insert an element into a stack. “Pop” is the
term used to delete an element from the stack.
All insertions and deletions take place at the same end, so the last element
added to the stack will be the first element removed from the stack. When
a stack is created, the stack base remains fixed while the stack top
changes as elements are added and removed. The most accessible
element is the top and the least accessible element is the bottom of the
stack.
➔Representation of Stack:
Let us consider a stack with 6 elements capacity. This is called as the size
of the stack. The number of elements to be added should not exceed the
maximum size of the stack. If we attempt to add new element beyond the
maximum size, we will encounter a stack overflow condition. Similarly,
you cannot remove elements beyond the base of the stack. If such is the
case, we will reach a stack underflow condition.
©Topperworld
Data Structure and Algorithms
4 4 4 4
3 3 3 3
TOP
2 2 2 33 2
TOP
22 22
1 TOP 1 1 1
11 11 11
TOP 0 0 0 0
Empty Insert Insert Insert
Stack 11 22 33
When an element is taken off from the stack, the operation is performed
by pop(). Figure 2 shows a stack initially with three elements and shows
the deletion of elements using pop().
4 4 4 4
TOP 3 3 3 3
33 2 2 2 2
TOP
22 22
1 1 TOP 1 1
11 11 11 TOP
0 0 0 0
Initial POP POP POP
Stack
Empty
Stack
Figure 2. Pop operations on stack
class Stack {
private int maxSize;
private int[] stackArray;
private int top;
©Topperworld
Data Structure and Algorithms
©Topperworld
Data Structure and Algorithms
[Link](1);
[Link](2);
[Link](3);
[Link]();
[Link](4);
[Link](5);
[Link]();
OUTPUT:-
Stack: 1 2 3
Popped: 3
Stack: 1 2 4 5
Peeked: 5
©Topperworld
Data Structure and Algorithms
top
400
data next
40 X
400
30 400
300
20 300
200
start
100 10 200
100
representation
class Node {
int data;
Node next;
class Stack {
private Node top;
©Topperworld
Data Structure and Algorithms
©Topperworld
Data Structure and Algorithms
[Link](1);
[Link](2);
[Link](3);
[Link]();
[Link](4);
[Link](5);
[Link]();
OUTPUT:-
Stack: 3 2 1
Popped: 3
Stack: 5 4 2 1
Peeked: 5
➔Algebraic Expressions:
©Topperworld
Data Structure and Algorithms
Example: (A + B) * (C - D)
Example: * + A B – C D
Example: A B + C D - *
Exponentiation ($ Highest 3
or ↑ or ^)
*, / Next highest 2
+, - Lowest 1
©Topperworld
Data Structure and Algorithms
Let us convert the expressions from one type to another. These can be
done as follows:
1. Infix to postfix
2. Infix to prefix
3. Postfix to infix
4. Postfix to prefix
5. Prefix to infix
6. Prefix to postfix
©Topperworld
Data Structure and Algorithms
Example 1:
Example 2:
©Topperworld
Data Structure and Algorithms
+ abc*+ +
( abc*+ +(
d abc*+d +(
* abc*+d +(*
e abc*+de +(*
+ abc*+de* +(+
f abc*+de*f +(+
) abc*+de*f+ +
* abc*+de*f+ +*
g abc*+de*f+ +*
g
End of a b c * + d e * f + The input is now empty. Pop the output
string g*+ symbols from the stack until it is empty.
Example 3:
©Topperworld
Data Structure and Algorithms
Example 4:
©Topperworld
Data Structure and Algorithms
import [Link];
while (![Link]()) {
if ([Link]() == '(') {
return "Invalid Expression"; // Unmatched
parenthesis
}
[Link]([Link]());
}
©Topperworld
Data Structure and Algorithms
return [Link]();
}
return [Link]();
}
©Topperworld
Data Structure and Algorithms
case '*':
case '/':
return 2;
}
return -1;
}
OUTPUT:-
The precedence rules for converting an expression from infix to prefix are
identical. The only change from postfix conversion is that traverse the
expression from right to left and the operator is placed before the
operands rather than after them. The prefix form of a complex expression
is not the mirror image of the postfix form.
Example 1:
PREFIX
SYMBOL STACK REMARKS
STRING
©Topperworld
Data Structure and Algorithms
C C
- C -
B BC -
+ BC -+
A ABC -+
End of - + A B C The input is now empty. Pop the output symbols
string from the stack until it is empty.
Example 2:
PREFIX
SYMBOL STACK REMARKS
STRING
) )
D D )
- D )-
C CD )-
( -CD
* -CD *
) -CD *)
B B-CD *)
+ B-CD *)+
A AB-CD *)+
( +AB–C *
D
End of * + A B – The input is now empty. Pop the output symbols
string C D from the stack until it is empty.
Example 3:
©Topperworld
Data Structure and Algorithms
+ H )+
G GH )+
( +GH
/ +GH /
F F+GH /
/ F+GH //
E EF+GH //
+ //EF+GH +
D D//EF+GH +
- D//EF+GH +-
C CD//EF+GH +-
* CD//EF+GH +-*
B BCD//EF+GH +-*
↑ BCD//EF+GH +-*↑
A ABCD//EF+GH +-*↑
End of The input is now empty. Pop the
+-* ↑ ABCD//EF+
string output symbols from the stack
GH
until it is empty.
import [Link];
©Topperworld
Data Structure and Algorithms
[Link]([Link]());
}
if (![Link]() && [Link]() != ')') {
return "Invalid Expression"; // Unmatched
parenthesis
} else {
[Link](); // Pop ')'
}
} else {
while (![Link]() && precedence(c) <
precedence([Link]())) {
[Link]([Link]());
}
[Link](c);
}
}
while (![Link]()) {
if ([Link]() == ')') {
return "Invalid Expression"; // Unmatched
parenthesis
}
[Link]([Link]());
}
return [Link]().toString();
}
©Topperworld
Data Structure and Algorithms
return [Link]();
}
©Topperworld
Data Structure and Algorithms
OUTPUT:-
Example:
©Topperworld
Data Structure and Algorithms
import [Link];
©Topperworld
Data Structure and Algorithms
if ([Link]() != 1) {
return "Invalid Postfix Expression";
}
return [Link]();
}
OUTPUT:-
©Topperworld
Data Structure and Algorithms
Example:
©Topperworld
Data Structure and Algorithms
import [Link];
if ([Link]() != 1) {
return "Invalid Postfix Expression";
}
return [Link]();
}
©Topperworld
Data Structure and Algorithms
OUTPUT:-
Example:
©Topperworld
Data Structure and Algorithms
import [Link];
if ([Link]() != 1) {
return "Invalid Prefix Expression";
}
return [Link]();
}
©Topperworld
Data Structure and Algorithms
String infixExpression =
prefixToInfix(prefixExpression);
OUTPUT:-
©Topperworld
Data Structure and Algorithms
Example:
©Topperworld
Data Structure and Algorithms
import [Link];
if ([Link]() != 1) {
return "Invalid Prefix Expression";
}
return [Link]();
}
©Topperworld
Data Structure and Algorithms
OUTPUT:-
Example 1:
OPERAND OPERAND
SYMBOL VALUE STACK REMARKS
1 2
6 6
5 6, 5
2 6, 5, 2
The first four symbols
3 6, 5, 2, 3
are placed on the stack.
Next a ‘+’ is read, so 3
and 2 are popped from
+ 2 3 5 6, 5, 5
the stack and their sum
5, is pushed
8 2 3 5 6, 5, 5, 8 Next 8 is pushed
Now a ‘*’ is seen, so 8 and
* 5 8 40 6, 5, 40 5 are popped as 8 * 5 =
40 is pushed
Next, a ‘+’ is seen, so 40
+ 5 40 45 6, 45 and 5 are popped and 40
+ 5 = 45 is pushed
3 5 40 45 6, 45, 3 Now, 3 is pushed
Next, ‘+’ pops 3 and 45
+ 45 3 48 6, 48 and pushes 45 + 3 = 48
is pushed
©Topperworld
Data Structure and Algorithms
Example 2:
©Topperworld
Data Structure and Algorithms
import [Link];
if ([Link]() != 1) {
throw new IllegalArgumentException("Invalid Postfix
Expression");
}
return [Link]();
}
©Topperworld
Data Structure and Algorithms
OUTPUT:-
➔Applications of stacks:
©Topperworld