Write a program to create a stack

                  Program plan:

·                     The aim of program is to create remove()method in place of display
method.
·                     The programmer have to implement the LinkedStack, StackArray and
            VectorArray as outlined in 6-1, 6-2, and 6-3 respectively in the text book.
·                     Create an interface Interface4Stack, which defines stack operation
            methods.
·                     Create remove() method, that is used to remove all items of stack from top to
            bottom.
·                     If remove() method is used, it will give an exception when stack will not
·                     get any push in the list.
·                     Define interface4stack stack interface, in which stack operations method
            will define. These methods are as follows:  the pop method, push method,
            Peek method and remove method.
·                     Define class linkedStack that implements the stack interface
          interface4stack.
·                     The remove method is used to remove all items from the stack from top to
            bottom. The remove method will operate if the top will be greater than zero.
·                     Define Node class, which is required to add the elements in the list.
·                     Define the DataInputStream method, which is used to enter values by user
keyboard.
·                     The main method of program is defined, from where the execution of program
            begins start. The input/ output exception in main method is also defined.


Program:


// include header
import java.io.DataInputStream;
import java.io.IOException;
import java.util.*;

// define stack interface
interface Interface4Stack
{
     int n = 20;

Define stack operation in the interface, in which the pop method, push method, peek method and remove method.

     public void pop();
     public void push();
     public void peek();
     public void remove(int n);
}

// define class linkedStack that implements the stack
// interface
public  class LinkedStack<T> implements Interface4Stack
{
     // define variables data type
     private Node top_Node;

     // define stack array
     int stack_array[] = new int[n];
     int top = -1;

     // implementing stack operation
     // push operation
     public void push()
     {    // try for exception
          try
          {
              // use this method to read data input from
               // keyboard
              DataInputStream dis = new
                                DataInputStream(System.in);
             
              // display monitor start to enter input
              System.out.println("Enter Element");
             
              // uses readLine() method of dataInputStream
               // to read data from keyboard
              int element =
                         Integer.parseInt(dis.readLine());
              stack_array[++top] = element;
          }
          // exception get catch
          catch (Exception e)
          {
              System.out.println("e");
        }
     }

     // define pop operation of stack
     public void pop()
     {
          int popping = stack_array[top];
          top--;
          System.out.println("popped element " + popping);
    }

     // define peek operation method
     public void peek()
     {
          int popping = stack_array[top];
          System.out.println("popped element " + popping);
    }

The remove method is used to remove all items from the stack from top to bottom. The remove method is operated if the top is greater than zero. It means that one push item is required otherwise an exception in main is displayed on the monitor.

     // define remove method instead of display method
     @Override
     public void remove(int n)
     {
          // if top is greater than 0 then it pop from top
          // to bottom item
          while(n>0)
          {
              pop();
              n--;
          }        
     }   
     // define method isEmpty as boolean
     public boolean isEmpty()
     {
          return top_Node == null;
     } // end isEmpty

The Node class is required to add the elements in the list. The class Node is private.

     // define Node class
     private class Node
     {
          // Entry in stack
          private T    data;
          // Link to next node
          private Node next;
          private Node(T dataPortion)
          {   
              this(dataPortion, null);
     } // end constructor
    
     // create constructor of Node class
     private Node(T dataPortion, Node linkPortion)
     {
          data = dataPortion;
          next = linkPortion;
     } // end constructor
    
     private T getData()
     {
          return data;
     } // end getData
    
     private void setData(T newData)
     {
          data = newData;
     } // end setData

Define private getNextNode, only Node class will change the node in the list.
           
     private Node getNextNode()
     {
          return next; 
     } // end getNextNode
    
     private void setNextNode(Node nextNode)
     {
          next = nextNode;
     } // end setNextNode
    
} // end Node

The main method of program is defined, from where the execution of program begins start. The input and output exception in main method is also defined.

public  void main(String arg[]) throws IOException
{

Define DataInputStream method, which is used to enter values by user keyboard.

   DataInputStream dis = new DataInputStream(System.in);
   LinkedStack LK = new LinkedStack();
   System.out.println("Is stack empty?"+LK.isEmpty());
   int menu = 0;
   do
   {
     System.out.println("1.push \n2.pop \n3.peek \n4.remove
                         \n5.Exit");
      // choose a choice
      System.out.print("Enter your choice: ");
       
      // uses readLine() method of DataInputStream
      // that read the user input from keyboard
      menu = Integer.parseInt(dis.readLine());
      switch (menu)
        {
        case 1:
          LK.push();
          break;
        case 2:
          LK.pop();
          break;
        case 3:
          LK.peek();
          break;
        case 4:
          LK.remove(menu);
            break;
        case 5:
          System.exit(0);
        }
    }
    while (menu <= 5);
            System.out.println();
  }
}


Sample Output:

1. Push
2. Pop
3. Peek
4. Remove
5. Exit
Enter your choice: 1
Enter Element
100
1. Push
2. Pop
3. Peek
4. Remove
5. Exit
Enter your choice: 1
Enter Element
200
1. Push
2. Pop
3. Peek
4. Remove
5. Exit
Enter your choice: 1
Enter Element
300
1. Push
2. Pop
3. Peek
4. Remove
5. Exit
Enter your choice: 1
Enter Element
400
1. Push
2. Pop
3. Peek
4. Remove
5. Exit
Enter your choice: 4
Popped element 400
Popped element 300
Popped element 200
Popped element 100

Post a Comment

Previous Post Next Post