Abstract Data Types:
It is a set of classes or types for objects such that their behavior is defined by a set of operations and values. The definition depicts that only operations can be performed but mention neither how these operations work at the ground level nor how the storage space is consumed.
The stack is an abstract data type with a predefined capacity. It is an ordered list of similar data types that allows the addition and removal of elements in an orderly fashion. Stack follows the principle “LIFO (Last in First Out)” or “FILO (First In Last Out)” which is similar to the pile of plates.
Operations on the stack:
push(): Used to push the elements to the stack
pop(): Used to remove the element from the stack. To be specific, the element that is last pushed will be removed.
peek(): Prints the top element on the stack
isFull(): To check if the stack is full (Push cannot be performed in this case)
isEmpty(): To check if the stack is empty. (Pop cannot be performed in this case)
Algorithm
Push:
- Check if the stack is full or not.
- If the stack is full, exit the program.
- If the stack is not full, increment the top and add the element to the stack.
Pop:
- Check if the stack is empty or not.
- If the stack is empty, exit the program.
- If the stack is not empty, print the element at the top and decrement the top.
Pseudocode
START PROCEDURE
push(int x)
if isFull() then
print Stack is Full
return
end if
END PROCEDURE
START PROCEDURE
pop()
if isEmpty() then
print Stack is Empty
return 0
end if
return arr[top--]
END PROCEDURE
START PROCEDURE
isEmpty()
return top==-1
END PROCEDURE
START PROCEDURE
isFull()
return top==capacity-1
END PROCEDURE
Implementation in Java
//File Name: Stack.java
class Stack {
private int arr[];
private int top;
private int capacity;
Stack(int size) {
arr = new int[size];
capacity = size;
top = -1;
}
public void push(int x) {
if (isFull()) {
System.out.println("Stack is full! \nProgram Terminated\n");
return;
}
System.out.println("Inserting the element: " + x);
arr[++top] = x;
}
public int pop() {
if (isEmpty()) {
System.out.println("Stack Empty!");
return 0;
}
return arr[top--];
}
public int size() {
return top++;
}
public Boolean isEmpty() {
return top == -1;
}
public Boolean isFull() {
return top == capacity - 1;
}
public void printStack() {
for (int i = 0; i <= top; i++) {
System.out.println(arr[i]);
}
}
public void peek(){
System.out.println("The top element of the stack is: " + arr[top]);
}
public static void main(String[] args) {
Stack stack = new Stack(5);
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.peek();
stack.pop();
System.out.println("\nAfter popping out");
stack.printStack();
}
}
Notations:
Stack() → Function to initialize the stack
push() → Function to add the elments to the stack
pop() → Function to remove elements from stack
size() → Function to determine the size of stack
isEmpty() → Function to return boolean value if
the stack is empty
isFull() → Function to return boolean value if the
stack is full
printStack() → To print the elements in stack
peek() → Function to print the top element of stack
arr → array to demonstrate stack
top, capacity → Variables
Detailed Description of the code is given in the downloadable document.
Some of the Applications:
- Expression Parsing
- Expression Conversion and Evaluation (Infix, postfix and prefix)
One thought on “Stack in Data Structures”