How to Implement Generics Stack Using Linked List in Java [Generics Stack Implementation Using Linked List ]πŸ’₯πŸ’₯

Implementation of Generics Stack using Linked List in Java [Generics Stack Implementation Using Linked List ]πŸ’₯πŸ’₯


πŸ‘DATA STRUCTURE [ STACK  IMPLEMENTATION ]



Stack Implementation Using Linked list Output
Generics Stack Implementation Using Linked List Program Output


This post shows how to code in JAVA to implementing a Generic Stack using the Linked list.

So, lets start with Stack Basic definition πŸ‘‰


Stack picture

What is the Stack?


Stack: The stack is a very common and non-primitive data structure used in programs. A stack is also called LIFO(Last-In-First-Out).

Stack is a linear list of items in which all insertions and deletion restricted to one end that is Top(Top of the stack).

The following two operations perform on Stack-

  1. Push (Insertion)
  2. Pop (Deletion)



Generics:


Java Generics is a set of related methods or a set of similar types. Generics allow types Integer, String, or even user-defined types to be passed as a parameter to classes, methods, or interfaces. Generics are mostly used by classes like HashSet or HashMap.


Generics advantages
Generics Advantages



Generics ensure compile-time safety which allows the programmer to catch the invalid types while compiling the code.



Generics Stack Implementation Using Linked List  Complete CodeπŸ‘‰


class StackImplUsingLinkedList<E>{
    private Node head;
    private int length;
   
//In linked list a node has two part- data part and address/reference part.
    class Node
    {
        private E data;
        Node next;
        public Node(E data){
            this.data = data;
        }
    }
   
//Default constructor of outer class.
    public StackImplUsingLinkedList(){
        this.head = null;
        this.length = 0;
    }

//This function return the number of an items in stack.
    public int length(){
        return length;
    }

//This function tells, stack is empty or not.
    public boolean isEmpty(){
        return length == 0;
    }

//This method insert an item to the Top of the stack.
    public void push(E data){
        Node temp = new Node(data);
        temp.next = head;
        head = temp;
        length++;
   
    }

// This method displays items of the stack.
    public void show(){
        if(isEmpty())
            System.out.println("Stack is Empty!!");
        else{
            Node temp = head;
            System.out.print("Stack Elements : ");
            while(temp!= null){
                System.out.print(temp.data+" ");
                temp = temp.next;
           }
        }   
    }
 

//This method returns the Top item of the Stack.
  public E tos(){
        if(isEmpty()){
            System.out.println("Stack is Empty!!");
        }
        return head.data;   
    }

//This method delete an item from the Top of the stack.
    public E pop(){
        if(isEmpty()){
            System.out.println("Stack is Empty!!");
        }
            E result = head.data;
            head = head.next;
            length--;
            return result;
    }

//Main method
    public static void main(String aa[]){
        StackImplUsingLinkedList<Number> stack= new StackImplUsingLinkedList<Number>();
        //StackImplUsingLinkedList<Object> stack= new StackImplUsingLinkedList<Object>();
       // stack.push("p2d");
       // stack.push('A');

        stack.push(20.00);
        stack.push(100);
        stack.push(40.12f);
       stack.push(50);
        stack.show();
        System.out.println("\nTop of stack : "+stack.tos());
        System.out.println("length : "+stack.length());
        stack.pop();
        stack.show();
        System.out.println("\nTop of stack : "+stack.tos());
        System.out.println("length : "+stack.length());
        stack.pop();
        stack.pop();
        stack.pop();
        stack.show();   
        System.out.println("length : "+stack.length());
      }
}


Note
: To run this code in your system ,Save this program with StackImplUsingLinkedList.java
Compile and run with same name.




Follow Program2Dcode on


Youtube icon
Pinterest iconinstagram icon
facebook icon

Click on it to redirect to Program2Dcode



If you have any problems/doubts regarding this, Please let me know in comment section. 

Post a Comment

0 Comments