Question

In Java, give a complete  example of implementing the NODE of singly linked list. (Screenshot) Thanks.

In Java, give a complete  example of implementing the NODE of singly linked list. (Screenshot)

Thanks.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
In below code i have given for inserting node and printing the nodes
please do comment if u have any concern..


/**
 * SinglyLinkedList.java
 */
public class SinglyLinkedList {
    // head of singly linked list
    LinkedListNode head;

    /**
     * function to insert new node to singly linked list
     * @param singlyLinkedList
     * @param nodeValue
     * @return linked list with new node inserted
     */
    public static SinglyLinkedList addNewNode(SinglyLinkedList singlyLinkedList, int nodeValue) {
        // creating new node with node value
        LinkedListNode node = new LinkedListNode(nodeValue);
        node.nextNode = null;
        // checking if head is null 
        // if head is null then making new node to head
        if (singlyLinkedList.head == null) {
            singlyLinkedList.head = node;
        } else {
            // if head is not null 
            // then adding head to next of new node
            // and making new node as head
            LinkedListNode temp = singlyLinkedList.head;
            while (temp.nextNode != null) {
                temp = temp.nextNode;
            }
            temp.nextNode = node;
        }
        return singlyLinkedList;
    }

    /**
     * function ot print singly linked list
     * @param singlyLinkedList
     */
    public static void printSinglyLinkedList(SinglyLinkedList singlyLinkedList) {
        LinkedListNode temp = singlyLinkedList.head;
        while (temp != null) {
            System.out.print(temp.nodeValue + "->");
            temp = temp.nextNode;
        }
        System.out.println("null");
    }
    // inner class for Linked Node 
    // which containg node value and reference to next node
    static class LinkedListNode {

        int nodeValue;
        LinkedListNode nextNode;
        LinkedListNode(int nodeValue) {
            this.nodeValue = nodeValue;
            nextNode = null;
        }
    }

    // main function for 
    // running the above program
    public static void main(String ... args) {
        // creating object of singly linked list
        SinglyLinkedList singlyLinkedList = new SinglyLinkedList();

        // adding nodes to list
        singlyLinkedList = addNewNode(singlyLinkedList, 4);
        singlyLinkedList = addNewNode(singlyLinkedList, 32);
        singlyLinkedList = addNewNode(singlyLinkedList, 54);
        singlyLinkedList = addNewNode(singlyLinkedList, 6);
        singlyLinkedList = addNewNode(singlyLinkedList, 85);
        singlyLinkedList = addNewNode(singlyLinkedList, 21);
        // printing the list
        printSinglyLinkedList(singlyLinkedList);
    }
} 

// OUT

Add a comment
Know the answer?
Add Answer to:
In Java, give a complete  example of implementing the NODE of singly linked list. (Screenshot) Thanks.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT