DS e-Content - Module 6 Stack
DS e-Content - Module 6 Stack
Module-VI: Stack
Dr. A K Yadav
Contents
Stack 3
Introduction to Stack 3
Abstract Data Type 6
Primitive Stack operations 9
Stack Implementation 10
Array Implementation of Stack 11
Linked List Implementation of Stack 16
Application of Stack 29
Postfix Expressions 32
Prefix Expressions 34
Evaluation of postfix expression 36
Introduction to Stack
I A Stack is a linear data structure that follows a particular
order in which the operations are performed.
I The order may be LIFO(Last In First Out) or FILO(First In
Last Out).
I LIFO implies that the element that is inserted last, comes out
first and FILO implies that the element that is inserted first,
comes out last.
I A variable name Top is used to keep the position of the
topmost element in the stack.
I we can access the elements only on the top of the stack
Stack Implementation
I There are two types of Stack based on Size: Fixed Size Stack
and Dynamic Size Stack
I Arrays are used to implement the fixed size Stack.
I Size of the array declared at the time of declaration of the
array in Java is the size of the Stack.
I For example int Stack[20], here maximum stack size is limited
upto 20. and the valid range of the Top is from 0 to 19.
I Top having value -1 is the condition for empty stack and Top
having value 20 is the condition for full stack.
I When stack is empty, nothing can be popped.
I When stack is full, nothing can be pushed.
I Dynamic size stack can be implemented using the link link.
boolean isEmpty()
{
if(top<0) return true; else return false;
//return (top < 0);
}
Stack()
{
top = -1;
}
boolean push(int x)
{
Module-VI: Stack Dr. A K Yadav Data Structures using JAVA 11/27
School of Computer Science and Engineering
if (top >= (MAX - 1)) {
System.out.println("Stack Overflow");
return false;
}
else {
top=top+1;
a[top] = x;
System.out.println(x + " pushed into stack");
return true;
}
}
int pop()
{
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
Module-VI: Stack Dr. A K Yadav Data Structures using JAVA 11/27
School of Computer Science and Engineering
}
else {
int x = a[top];
top=top-1;
return x;
}
}
int peek()
{
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
}
else {
int x = a[top];
return x;
Module-VI: Stack Dr. A K Yadav Data Structures using JAVA 11/27
School of Computer Science and Engineering
}
}
boolean isFull() {
if(top==MAX-1) return true; else false;
}
class StackArray {
public static void main(String args[])
{
Stack s = new Stack();
s.pop();//Stack underflow
s.push(10);
s.push(20);
s.push(30); //Stack overflow
System.out.println(s.pop() + " Popped from stack");
Module-VI: Stack Dr. A K Yadav Data Structures using JAVA 11/27
School of Computer Science and Engineering
System.out.println("Top element is :" + s.peek());
}
}
Figure: push()
I pop(): Return the top element of the Stack i.e simply delete
the first element from the linked list.
Figure: pop()
Figure: peek()
Figure: isFull()
Figure: isEmpty()
class StackL {
Node top;
StackL() { //constructor
top = null;
}
Module-VI: Stack Dr. A K Yadav Data Structures using JAVA 19/27
School of Computer Science and Engineering
boolean isEmpty() {
if(top==null) return true;
else return false;
}
boolean isFull() {
Node new_node = new Node();
if(new_node==null) return true;
else return false;
}
void pop() {
if (top==null) {
System.out.println("\nStack Underflow");
return;
}
else {
Node temp = top;
top = top.next;
System.out.println("Popped element is " + temp.data);
temp = null;
}
}
Module-VI: Stack Dr. A K Yadav Data Structures using JAVA 19/27
School of Computer Science and Engineering
int peek() {
if (top!=null)
return top.data;
else {
System.out.println("\nStack is empty");
return Integer.MIN_VALUE;
}
}
}
Application of Stack
I Expression evaluation and conversion: Stacks can evaluate
expressions with operators and operands, and convert
expressions from one form to another
I Backtracking: Stacks can check for matching parentheses in
an expression
I Function call management: Stacks manage function calls in
real-time applications
I Memory allocation: Stacks are used for systematic memory
management
I Task scheduling: Stacks are used for task scheduling in
real-time applications
I Program flow management: Stacks are crucial for
managing program flow
Module-VI: Stack Dr. A K Yadav Data Structures using JAVA 20/27
School of Computer Science and Engineering
Postfix Expressions
1. Scan the infix expression from left to right.
2. If the scanned character is an operand, put it in the postfix
expression.
3. Otherwise, do the following
- If the precedence of the current scanned operator is higher
than the precedence of the operator on top of the stack, or if
the stack is empty, or if the stack contains a ‘(‘, then push the
current operator onto the stack.
- Else, pop all operators from the stack that have precedence
higher than or equal to that of the current operator. After that
push the current operator onto the stack.
4. If the scanned character is a ‘(‘, push it to the stack.
5. If the scanned character is a ‘)’, pop the stack and output it
until a ‘(‘ is encountered, and discard both the parenthesis.
Module-VI: Stack Dr. A K Yadav Data Structures using JAVA 22/27
School of Computer Science and Engineering
Prefix Expressions
I Step 1: Reverse the infix expression. Note while reversing
each ‘(‘ will become ‘)’ and each ‘)’ becomes ‘(‘.
I Step 2: Convert the reversed infix expression to “nearly”
postfix expression. While converting to postfix expression,
instead of using pop operation to pop operators with greater
than or equal precedence, here we will only pop the operators
from stack that have greater precedence.
I Step 3: Reverse the postfix expression.
Thank you
Please send your feedback or any queries to
[email protected]