Implementation of Stack using Linked List in Java

In computer science, a stack or LIFO (last in, first out) is an abstract data type that serves as a collection of elements, with two principal operations: push adds an element to the collection; pop removes the last element that was added. (Via Wikipedia)

Here is java code for implementation of stack using linked list in java.

import java.io.*;
class node
{
    int data;
    node next;
    public node(int x)
    {
        data=x;
        next=null;
    }
}
class stackll
{
    node top=null;
    public boolean isEmpty()
    {
        if (top==null)
            return true;
        else
            return false;
    }
    public void push(int x)
    {
        if(isEmpty()==true)
        {
            node p=new node(x);
            top=p;
        }
        else
        {
            node p=new node(x);
            p.next=top;
            top=p;
        }
    }
    public int pop()
    {
        if(isEmpty()==true)
        {
            System.out.println("Stack is empty");
            return -1;
        }
        else
        {
            int x=top.data;
            top=top.next;
            return x;
        }
    }
    public int peek()
    {
        if(isEmpty()==true)
        {
            System.out.println("No elements in stack");
            return -1;
        }
        else
        {
            return top.data;
        }
    }
    public void display()
    {
        if(isEmpty()==true)
        {
            System.out.println("No elements in stack");
        }
        else
        {
            node current=top;
            while(current!=null)
            {
                System.out.println(current.data);
                current=current.next;
            }
        }
    }
}
public class Stackusingll 
{
    public static void main(String[] args) throws IOException
    {
      BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
      int ch,temp;
      stackll sl=new stackll();
      do
      {
        System.out.println("##MENU##");
        System.out.println("1.Insert an element in stack");
        System.out.println("2.Remove an element");
        System.out.println("3.Peek element");
        System.out.println("4.Display Stack");
        System.out.println("5.Exit");
        System.out.println("Enter Choice");
        ch=Integer.parseInt(obj.readLine());
        switch (ch)
        {
            case 1:System.out.println("Enter element to be inseted");
                temp=Integer.parseInt(obj.readLine());
                sl.push(temp);
                break;
            case 2:temp=sl.pop();
                System.out.println("Popped element is "+temp);
                break;
            case 3:temp=sl.peek();
                System.out.println("Peeked element is "+temp);
                break;
            case 4:sl.display();
        }
      }
      while(ch!=5);
    }
}

/* Output

##MENU##
1.Insert an element in stack
2.Remove an element
3.Peek element
4.Display Stack
5.Exit
Enter Choice
1
Enter element to be inseted
10
##MENU##
1.Insert an element in stack
2.Remove an element
3.Peek element
4.Display Stack
5.Exit
Enter Choice
1
Enter element to be inseted
20
##MENU##
1.Insert an element in stack
2.Remove an element
3.Peek element
4.Display Stack
5.Exit
Enter Choice
1
Enter element to be inseted
30
##MENU##
1.Insert an element in stack
2.Remove an element
3.Peek element
4.Display Stack
5.Exit
Enter Choice
1
Enter element to be inseted
40
##MENU##
1.Insert an element in stack
2.Remove an element
3.Peek element
4.Display Stack
5.Exit
Enter Choice
4
40
30
20
10
##MENU##
1.Insert an element in stack
2.Remove an element
3.Peek element
4.Display Stack
5.Exit
Enter Choice
3
Peeked element is 40
##MENU##
1.Insert an element in stack
2.Remove an element
3.Peek element
4.Display Stack
5.Exit
Enter Choice
2
Popped element is 40
##MENU##
1.Insert an element in stack
2.Remove an element
3.Peek element
4.Display Stack
5.Exit
Enter Choice
2
Popped element is 30
##MENU##
1.Insert an element in stack
2.Remove an element
3.Peek element
4.Display Stack
5.Exit
Enter Choice
4
20
10

*/

One thought on “Implementation of Stack using Linked List in Java”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.