Linked List: Implement a stack using a linked list

Question: You have to try to implement push and pop functionality of a stack using a linked list

package additional_problems.linkedList;

/**
 * Created by aarushi on 29/6/21.
 */
public class P08StackLinkedList {

    public static void main (String[] args){
        //create a linked list
        LinkedList list= new LinkedList();
        ListNode node= new ListNode(4);
        //add node to stack
        push(list, node);
        node= new ListNode(2);
        //add second node to stack
        push(list, node);
        node= new ListNode(3);
        //add third node to stack
        push(list, node);
        list.printNoException();
        //delete last node
        System.out.println(pop(list).getData() + " deleted");
        list.printNoException();
        //delete last node
        System.out.println(pop(list).getData() + " deleted");
        list.printNoException();
        //delete last node
        System.out.println(pop(list).getData() + " deleted");
        list.printNoException();
    }

    //method to push elements into stack
    public static void push(LinkedList list, ListNode node){
        //is list is empty set the head equal to the node
        if(list.head==null){
            list.head= node;
            return;
        }

        //insert the node at the top of the stack (i.e at the head position of linked list)
        node.setNext(list.head);
        list.head=node;
    }

    public static ListNode pop(LinkedList list){

        //we have to remove the last node push into the stack
        //since we've inserted elements at the head of linked list
        //we have to pop nodes from the head of linked list
        ListNode nodeRemoved= list.head;
        list.head= list.head.getNext();

         return nodeRemoved;
    }

}
package additional_problems.linkedList;

/**
 * Created by aarushi on 24/6/21.
 */
public class LinkedList {
    //data fields
    //head node
    ListNode head;

    //constructor to initialize head node
    public LinkedList() {
        head = null;
    }
       
}
package additional_problems.linkedList;

/**
 * This class is the type declaration for a linked list
 * Created by aarushi on 24/6/21.
 */
public class ListNode {
    //data fields
    private int data; //stores the value of the node
    private ListNode next; //stores the reference of the next node
    private ListNode subList;

    //constructor to initialize the value of the node
    public ListNode(int data){
        this.data= data;
        this.subList=null;
    }

    //accessor method to get the value stored in data
    public int getData() {
        return this.data;
    }

    //mutator method to change the value of data
    public void setData(int data) {
        this.data = data;
    }

    //accessor method which returns the reference of the next node
    public ListNode getNext() {
        return this.next;
    }

    //mutator method which changes the reference of the next node
    public void setNext(ListNode next) {
        this.next = next;
    }

    public ListNode getSubList() {
        return subList;
    }

    public void setSubList(ListNode subList) {
        this.subList = subList;
    }
}

Leave a Reply

PHP JS HTML CSS BASH PYTHON CODE

Your email address will not be published. Required fields are marked *

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

%d
search previous next tag category expand menu location phone mail time cart zoom edit close