Stack Implementation
Stack Implementation
Lecture #
By: Abdul Aleem
1 / 20
Contents
Introduction
2 Stack Implementation
2 / 20
Introduction
Stack:
3 / 20
Stack Implementation
4 / 20
Array Implementation of Stack
class Stack
{
int top;
int MAXSIZE = 5;
int[] arr = new int[MAXSIZE];
}
5 / 20
isFull( )
6 / 20
PUSH Operations on Stack
7 / 20
Push operations on stack
8 / 20
isEmpty( )
9 / 20
POP Operations on Stack
10 / 20
Pop operations on stack
11 / 20
Display the stack
12 / 20
Display operation on stack (Example)
13 / 20
Linked List implementation of stack
14 / 20
PUSH Operation
If the list is empty then the item is to be pushed as the start node of the list.
This includes assigning value to the data part of the node and assign null to the
address part of the node.
If there are some nodes in the list already, then we have to add the new
element in the beginning of the list (to not violate the property of the stack). For
this purpose, assign the address of the starting element to the address field of the
new node and make the new node, the starting node of the list.
15 / 20
PUSH Operation
16 / 20
POP Operation
Deleting a node from the top of stack is referred to as pop operation. In order to pop
an element from the stack, we need to follow the following steps :
Check for the underflow condition: The underflow condition occurs when
we try to pop from an already empty stack. The stack will be empty if the head
pointer of the list points to null.
Adjust the head pointer accordingly: In stack, the elements are popped only
from one end, therefore, the value stored in the head pointer must be deleted and
the node must be free. The next node of the head node now becomes the head
node.
17 / 20
POP Operation
18 / 20
Display the nodes
[1] Goodrich and Tamassia ‘Data Structures and Algorithms in java ’, Wiley, India.
20 / 20