在链表中添加节点

时间:2014-02-07 16:14:52

标签: java linked-list nodes

尝试在下面的程序中实现single-linked-list,我真的无法解除如何在链接列表中添加节点(对于开始,我在空链表上尝试)

简单来说,我试图setDatasetNext,但getSizeofList()每次都返回0 ......现在看起来真的像火箭科学!!

问题:可以告诉我如何实现它......或者更确切地说,将节点添加到现有链接列表....

到目前为止我尝试了什么以及他们为什么要解决问题:我引用了多个程序,但它们太复杂了我无法理解( rocket science ),所以写道从我从算法中理解的程序下面......但即使在算法中,他们只是展示了如何实现的方法,这就是我失败的地方,因为,我不明白,data-typevalue将被传递以添加节点...


请注意,不是一个java家伙,所以请放轻松,这个问题是为了学习

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;
    }


}

2 个答案:

答案 0 :(得分:1)

您必须区分链表的单链(Node)和整个容器(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;
    }
}

有时可能会将封装视为:

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

答案 1 :(得分:0)

您永远不会设置headnode。在insertnode中,您只使用未设置setnext的{​​{1}}。您正在将顶级类和节点实现混合在一起。

以下是如何在java中实现链表以供进一步参考的示例: How do I create a Linked List Data Structure in Java?