Stack In JAVA

Stack in JAVA

In this tutorial you will learn 1) What is a stack? 2) How is stack used in programming? 3) Implementation of stack in JAVA What is a stack? Stack is one of the most important data structure in programming, it can be imagined as the bottle of balls which is open from one end. So the addition and deletion of balls can be done through one end only. The element which goes last in the stack is first to be extracted from the stack. So the stack works on last in first out principle (LIFO). How is stack used in programming Stack is very useful for programming purposes. The stack can be used to store the last used values of some variables as well as in operating system and micro controller programming the stacks can be used handle interrupts and used to store the program counter values and addresses. Implementation of stack in JAVA The stack in JAVA is implemented using ordinary array. Stack has few functions associated with it. They are: Push: Used to put data into the stack Pop: Used to remove data from the top of the stack Top: The first element of the array is called the top of the stack. In the code below first I have defined the class Stack, this class contains data members, methods and the constructor which initialize the data members. Then in the main of the program I have the object of stack class and performed operations on it using its methods. The detail and significance of each line is provided in the comments with the code below.
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class UsingJAVA {
  5.  public static class Stack { //we are defining the stack class
  6.  private int maximumSize;  //its the size of the array
  7.  private int[] structure; //this is the array in which the contents are stored
  8.  private int top;
  9.  public Stack(int M) //this is the constructor
  10.                            {
  11.  maximumSize = M; //The size of the array is of the user's choice
  12.  structure = new int[maximumSize]; // we are making an array
  13.  top = -1; // top is declared  -1 because we need to access the 0th postion at first insertion
  14.                            }
  15.  public void push(int j) //function to put values into stack.
  16.                            {
  17.  structure[++top] = j; //the value of top pointer is increased and value is inserted
  18.                            }
  19.  public int pop() //to take data out of the stack
  20.                            {
  21.  return structure[top--]; //decrement takes the top to the element below the top element
  22.                            }
  23.                          
  24.  public boolean isEmpty() //standard function to check whether the stack is empty or not
  25.                            {
  26.  return (top == -1);
  27.                            }
  28.  public boolean isFull() //standard function to check if the stack is full?
  29.                            {
  30.  return (top == maximumSize - 1);
  31.                            }
  32.  public static void main(String[] args) {
  33.  int x;
  34.  System.out.println("What should be the size of the stack?");
  35.  Scanner inputScanner= new Scanner(System.in);
  36.  x=inputScanner.nextInt();
  37.  Stack Workstack = new Stack(x); //the object of the stack class is made
  38.  System.out.println("Please enter the values you want to insert into stack");
  39.  for(int j=0; j<x ;j++) //for loop used to insert the number of values into the stack
  40.  { int z;
  41.  z=inputScanner.nextInt();
  42.  Workstack.push(z); //pushing a value into stack
  43.  }
  44.  System.out.println("The contents of the stack are popped out and they are:" );
  45.  while (!Workstack.isEmpty()) {
  46.  int element = Workstack.pop(); //retrieving each element and saving it in variable
  47.  System.out.print(element); //displaying the variable to screen.
  48.  System.out.println();
  49.                                            }
  50.  System.out.println();
  51.                            }
  52.                         }
  53.        
  54. }
Screen Shot Screen Shot

Add new comment