← All resources

Write a program to implement an array based STACK

The following post is made to implement array based stack using java programming language

In computer science, a stack is an abstract data type that serves as a collection of elements, with two principal operations:

push, which adds an element to the stack, and
pop, which removes the most recently added element that was not yet removed.
The order in which elements come off a stack gives rise to its alternative name, LIFO (last in, first out).

The name “stack” for this type of structure comes from the analogy to a set of physical items stacked on top of each other, which makes it easy to take an item off the top of the stack, while getting to an item deeper in the stack may require taking off multiple other items first.

Considered as a linear data structure, or more abstractly a sequential collection, the push and pop operations occur only at one end of the structure, referred to as the top of the stack. This makes it possible to implement a stack as a singly linked list and a pointer to the top element.

A stack may be implemented to have a bounded capacity. If the stack is full and does not contain enough space to accept an entity to be pushed, the stack is then considered to be in an overflow state. The pop operation removes an item from the top of the stack.

import java.util.*;
public class stack
{
    int stk[], n, front;
    stack(int x)        //Parameterised constructor
    {
        n = x;
        stk = new int[n];
        front=-1;
    }

    void push(int x)
    {
        if(front==n-1)
        {
            System.out.println("Stack Overflow.");
            return;
        }
        front++;
        stk[front] = x;
    }

    void pop()
    {
        if(front==-1)
        {
            System.out.println("Stack is empty.");
            return;
        }
        System.out.println("Deleted Value:"+stk[front]);
        front--;
    }

    void display()
    {
        int i;
        if(front==-1)
        {
            System.out.println("Stack is empty.");
            return;
        }
        System.out.println("Element in the array:");
        for(i=0;i<=front;i++)
        {
            System.out.println(stk[i]);
        }
    }

    public static void main()
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the size: ");
        stack obj = new stack(sc.nextInt());
        int ch, v;
        do
        {
            System.out.println("MENU");
            System.out.println("1. Push");
            System.out.println("2. Pop");
            System.out.println("3. Display");
            System.out.println("Enter your choice:");
            ch = sc.nextInt();
            switch(ch)
            {
                case 1:
                System.out.println("Enter the value to push: ");
                v = sc.nextInt();
                obj.push(v);
                break;
                case 2:
                obj.pop();
                break;
                case 3:
                obj.display();
                break;
                default:
                System.out.println("Wrong input ");
            }//front of switch
        }while(ch>=1&&ch<=3);
    }
}