Add nodes in linked list

安稳与你 提交于 2019-12-12 00:18:20

问题


Trying to implement single-linked-list in below program, i am really not able to undertsand how to add a node in an Linked list (for start, m trying it on empty linked list).

To put it plain simple,i tried to setData and setNext but getSizeofList() return 0 everytime....its really looking like a rocket science to me now!!

Question : Can some-one tell me how to implement it....or rather, add a node to existing linked list....

What i have tried so far and why they dint worked out: i referenced multiple programs but they were too complex for me to understand(rocket science), so wrote below program from what i understood from algorithms....but even in algo's, they just show methods on how to implement and this is where i failed, as, i dont understand,what data-type and value is to be passed for adding a node...


please not that m not a java guy, so please go easy, this question comes in as an attempt to learn

package Data_S;

public class Linked_List {

    private int data;
    private Linked_List next_ptr;
    private Linked_List headNode = null;

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Linked_List ll = new Linked_List();
        //ll.setnext(25);
        ll.insert_node(24);
        ll.traverse();
        ll.getSizeofList();
    }


    //size of list
    public void getSizeofList()
    {
        int l = 0;
        Linked_List curr = headNode;
        while(curr != null)
        {
            l++;
            curr = curr.getnext();
        }
        System.out.print("Size of list is = "+l);
    }

    //insert node
    public void insert_node(/*Linked_List node, */int data)
    {
        if(headNode == null)
        {
            System.out.println("in insert"); // checking
            this.setnext(headNode);
            this.setData(data);
            System.out.print("value = "+this.getData());
        }
    }

   //set data for this node
    public void setData(int data)
    {
        this.data = data;
    }

    //return the data
    public int getData()
    {
        return this.data;
    }

    //set next pointer
    public void setnext(Linked_List next_ptr)
    {
        this.next_ptr = next_ptr;
    }

    //get next pointer
    public Linked_List getnext()
    {
        return this.next_ptr;
    }


}

回答1:


You have to make a distinction between the single chains (Node) of a linked list, and the entire container (LinkedList).

public class LinkedList {
    Node head;
    int size; // Maybe

    public void insertAtEnd(int data) {
        Node previous = null;
        for (Node current = head; current != null; current = current.next) {
            previous = current;
        }
        Node baby = new Node(data);
        if (previous == null) {
            head = baby;
        } else {
            previous.next = baby;
        }
        ++size;
    }

    public void insertInSortedList(int data) {
        Node previous = null;
        Node current = null;
        for (current = head; current != null && data < current.data;
                current = current.next) {
            previous = current;
        }
        Node baby = new Node(data);
        baby.next = current;
        if (previous == null) {
            head = baby;
        } else {
            previous.next = baby;
        }
        ++size;
    }
}

class Node {
    int data;
    Node next;
    Node(int data) {
        this.data = data;
    }
}

One may sometimes see encapsulation as:

public class LinkedList {
    private static class Node {
    }
    ...
}



回答2:


You never set headnode. In insertnode you just use setnext which does not set headnode. You are mixing the top class and the node implementation together.

Here is an example of how to implement a linked list in java for further reference: How do I create a Linked List Data Structure in Java?



来源:https://stackoverflow.com/questions/21632621/add-nodes-in-linked-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!