The program shows stack implementation. In this article, we’ll be writing a custom stack implementation with the following operations pop, peek, and push. Apart from that, we’ll be using predefined class to achieve the stack functionality.
A stack is a data structure that works in LIFO ( Last in First out) manner. Data can be inserted with push operation and deleted using pop operation, only from one end called Top.
Program:
import java.util.Scanner; class StackExample { int number[]; int top; int limit; // constructor public StackExample(int size){ top =-1; limit =size; number=new int[size]; } // insert an element void push(int num) { if(isFull()) System.out.println("Stack is full"); else { top=top+1; number[top]=num; } } // popping out the element that is at the top of stack void pop(){ if(isEmpty()) System.out.println("Stack is Empty"); else{ top=top-1; System.out.println("Element popped out"); } } void peek() { System.out.println("Top most element in Stack ="+number[top]); } // check stack, is it full or not? boolean isFull(){ return top >=limit-1; } boolean isEmpty(){ return top==-1; } void print(){ System.out.println("Elements in Stack:-"); for(int i=top;i>=0;i--) System.out.println(number[i]); } public static void main(String[] args) { Scanner input =new Scanner(System.in) ; int size,option,element; char chr; System.out.print("Enter the maximum size that a stack can have = "); size=input.nextInt(); StackExample obj= new StackExample(size); do{ System.out.println("Please press any number from the following operations:-" + "\n 1. Insert an element " + "\n 2. Pop an element" + "\n 3. Peek of the stack" + "\n 4. Display the elements of the stack"); option=input.nextInt(); switch(option){ case 1: System.out.print("Please enter the element to insert = "); element=input.nextInt(); obj.push(element); break; case 2: obj.pop(); break; case 3: obj.peek(); break; case 4: obj.print(); break; default: System.out.println("Choose wrong option "); } System.out.println("Want to continue? y or n "); chr=input.next().charAt(0); }while(chr=='y'||chr=='Y'); } }
Output:
Java supports a predefined class named Stack. It offers methods like push(), pop() and peek() that helps in achieving stack implementation
Program:
import java.util.Stack; class StackExample { public static void main(String[] args) { Stack<Integer> integerStack = new Stack<>(); // Pushing 5 element into the stack integerStack.push(34); integerStack.push(2); integerStack.push(5); integerStack.push(100); integerStack.push(2); // printing the stack's elements System.out.println("Elements in the Stack :: " + integerStack); // Pop operation on the stack, 2 would be removed from the top integerStack.pop(); // printing the stack's elements System.out.println("Elements in the Stack :: " + integerStack); // Peek operation on the stack,100 would be printed System.out.println("Top of the stack :: " + integerStack.peek()); } }
Output:
Elements in the Stack :: [34, 2, 5, 100, 2] Elements in the Stack :: [34, 2, 5, 100] Top of the stack :: 100
This post was last modified on August 16, 2022