stack using arrays in java
import java.util.*; class stack_in_arrays { public static void main(String[] args) { stack_in_arrays object = new stack_in_arrays(); Scanner vinnu = new Scanner(System.in); while(true) { System.out.println("Please enter the optin \n 1.push \n 2.pop \n 3.peek \n 4.isempty \n 5.isfull \n 6.Show Stack \n 7.exit"); System.out.println("--------------------------------------------------"); int option=vinnu.nextInt(); switch(option) { case 1 : System.out.print("enter the elememt to push = "); int value=vinnu.nextInt(); push(value); break; case 2 : pop(); break; case 3 : peek(); break; case 4 : isempty(); break; case 5 : isfull(); break; case 6 : fullstack(); break; case 7 : System.exit(1); } System.out.println("--------------------------------------------------"); } } private static final int capacity=3; static int stackarray[]=new int[capacity]; static int top=-1; public static void push(int value) { if(top<capacity-1) { top++; stackarray[top]=value; System.out.println(value+" is pushed into the stack ."); } else { System.out.println("Stack is overflowed ."); } } public static void pop() { if(top>=0) { System.out.println(stackarray[top]+" is popped out from the stack ."); top--; } else { System.out.println("Stack underflow ."); } } static int peek() { if(top>=0) { return stackarray[top]; } else return 0; } public static void isempty() { if(top==-1) { System.out.println("Stack is empty ."); } else { System.out.println("Stack is not empty ."); } } public static void isfull() { if(top==capacity-1) { System.out.println("Stack is full ."); } else { System.out.println("Stack is not full ."); } } public static void fullstack() { if(top>=0) { System.out.println("Elements in the Stack "); for(int i=0;i<capacity;i++) { System.out.println(" --->>>"+stackarray[i]); } } else { System.out.println(" Stack is empty "); } } }
stack using arrays in java
Reviewed by Unknown
on
11:18
Rating:
No comments: