Friday, 25 November 2016

Program to implement STACK and its operations

Stack is a linear data structure in which all insertions or deletions are takes place at only one end rather than in the middle. This end is called as top of stack. For example – a stack of coins, a stack of plates. As the elements are inserted (added) or deleted (removed) from one end only it is called as FILO (First In Last Out) or LIFO (Last In First Out) lists.

If stack is full (no space for new element) and we are inserting new element then such condition is called as Stack Overflow. If stack is empty (no element in stack) and we are deleting an element at top of stack then such condition is called as Stack Underflow. This program demonstrate the operations on STACK. When the new element is added in Stack then value of "top" is incremented by 1 and if we delete an element from Stack then the value of "top" is decremented by 1.


PROGRAM
import java.util.Scanner;

class MyStack {
 
 private final int MAX_SIZE = 10;
 private int top = -1;
 private int stack[ ] = new int[MAX_SIZE];
 
 public void push(int x) {
  if(top == MAX_SIZE -1) {
   System.out.println("Stack Overflow..");
  } 
  else {
   top = top + 1;
   stack[top] = x;
  }
 }
 
 public int pop() {
  
  if(top == -1) {
   System.out.println("Stack Underflow..");
   return -1;
  }
  else {
   int x = stack[top];
   top = top - 1;
   return x;
  }
 }
 
 public void display() {
  
  if(top == -1) {
   System.out.println("Stack Empty..");
  }
  else {
   System.out.println("Current STACK contains ...");
   for(int i = top; i >= 0; i--) {
    System.out.println(stack[i]);
   }
  }
 }
}

class StackDemo {

 public static void main(String[] args) {

  MyStack m = new MyStack();
  
  Scanner s = new Scanner(System.in);
  int ch;
  int item;
  
  while(true) {
   
   System.out.println("\n**** MENU ****");
   System.out.println("1 : Push\n2 : Pop\n3 : Display\n4 : Exit");
   System.out.print("Enter ur choice :: ");
   ch  = s.nextInt();
   switch(ch) {
   
    case 1: // PUSH operation on stack
     System.out.print("Enter the element to push : ");
     item = s.nextInt();
     m.push(item);
     break;
    case 2 :
     item = m.pop();
     if(item != -1) 
      System.out.println("Popped element is " + item);
     break;
    case 3 :
     m.display();
     break;
    case 4 : System.exit(0);
   }
  }
 }
}
OUTPUT
C:\>javac StackDemo.java
C:\>java StackDemo.java
**** MENU ****
1 : Push
2 : Pop
3 : Display
4 : Exit
Enter ur choice :: 1
Enter the element to push : 10

**** MENU ****
1 : Push
2 : Pop
3 : Display
4 : Exit
Enter ur choice :: 1
Enter the element to push : 20

**** MENU ****
1 : Push
2 : Pop
3 : Display
4 : Exit
Enter ur choice :: 1
Enter the element to push : 56

**** MENU ****
1 : Push
2 : Pop
3 : Display
4 : Exit
Enter ur choice :: 3
Current STACK contains ...
56
20
10

**** MENU ****
1 : Push
2 : Pop
3 : Display
4 : Exit
Enter ur choice :: 2
Popped element is 56

**** MENU ****
1 : Push
2 : Pop
3 : Display
4 : Exit
Enter ur choice :: 3
Current STACK contains ...
20
10

**** MENU ****
1 : Push
2 : Pop
3 : Display
4 : Exit
Enter ur choice :: 4

No comments:

Post a Comment