Sunday, 14 February 2016

Stack Program Using Array

import java.util.Scanner;

class StackArray {

    private static int maxSize;
    private static int[] stackArray;
    private int top;

    public StackArray(int s) {
          maxSize = s;
          stackArray = new int[maxSize];
          top = -1;
    } // End of constructor
   
    public boolean isEmpty() {
        return top == -1;
    }
   
    public boolean isFull() {
        return top == maxSize-1;
    }
   
    public int push(int item) {       
          return stackArray[++top] = item;
    } // End of push()

    public int pop() {
          return stackArray[top--];
    } // End of pop()

    public int peek() {
          return stackArray[top];
    } // End of peek()

    public void display() {
        System.out.println(" inside disp " + stackArray[top]);      
          for(int i=0; i<=top; i++)
              System.out.printf(" %5d", stackArray[i]);
          System.out.println();
    } // End of display()
} // End of class StackArray

public class Stack {

    public static void main(String[] ar) {

          Scanner input = new Scanner(System.in);
          System.out.print("\n Enter the size of Stack : ");
          StackArray stackArray = new StackArray(input.nextInt());
          int ch;

          while(true) {
              System.out.printf("\n 1. Push \n 2. Pop \n 3. Peek \n 4. Display \n Press 0 for exit. \n");
              System.out.print("\n Enter your choice : ");
              ch = input.nextInt();
              switch(ch) {
                   case 0:
                       return;
                   
                   case 1:
                    boolean full = stackArray.isFull();
                    if(full) {
                        System.out.println(" Stack is Full. ");
                        break;
                    }           
                    System.out.print("\n Enter the element to push : ");                   
                    int push = stackArray.push(input.nextInt());                               
                    System.out.println(" Pushed Item : " + push);
                    break;
               
                   case 2:
                    boolean empty = stackArray.isEmpty();
                    if(empty) {
                        System.out.println(" Stack is Empty. ");
                        break;
                    }
                    System.out.println(" Poped Element : " + stackArray.pop());
                    break;
                   
                   case 3:
                    boolean emp = stackArray.isEmpty();
                    if(emp) {
                        System.out.println(" Stack is Empty. ");
                        break;
                    }
                    System.out.println(" Top Element : " + stackArray.peek());
                       break;
                   
                   case 4:
                    boolean e = stackArray.isEmpty();
                    if(e) {
                        System.out.println(" Stack is Empty. ");
                        break;
                    }                   
                       stackArray.display();
                       break;
                    
                   default :
                       System.out.println(" \n Wrong choice entered...! \n");
              } // End of switch
          }  // End of do while loop
    } // End of main()

} // End of class Stack

No comments:

Post a Comment