在链接列表中添加节点 [英] Add nodes in linked list

查看:32
本文介绍了在链接列表中添加节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

简单地说,我尝试 setData setNext ,但是 getSizeofList()每次都返回 0 ....现在对我而言,它真的像是一门火箭科学!

问题::有人可以告诉我如何实现它吗?或者,将一个节点添加到现有的链表中.

到目前为止我一直在尝试什么以及为什么要努力解决:我引用了多个程序,但是它们太复杂了,我无法理解(火箭科学),所以写道下面的程序是从我对算法的理解中得出的....但是即使在算法中,它们也只是显示了如何实现的方法,而这正是我失败的地方,因为我不理解数据类型将传递 value 来添加节点...


请不要说我不是Java专家,所以请放轻松,这个问题是为了学习而出现的

 包Data_S;公共类Linked_List {私有int数据;私有的Linked_List next_ptr;私有Linked_List headNode = null;/*** @参数args*/公共静态void main(String [] args){//TODO自动生成的方法存根Linked_List ll =新的Linked_List();//ll.setnext(25);ll.insert_node(24);ll.traverse();ll.getSizeofList();}//列表大小公共无效getSizeofList(){整数l = 0;Linked_List curr = headNode;while(curr!= null){l++;curr = curr.getnext();}System.out.print(列表大小为=" +1);}//插入节点公共无效insert_node(/* Linked_List节点,*/int数据){if(headNode == null){System.out.println(插入");//检查this.setnext(headNode);this.setData(data);System.out.print("value =" + this.getData());}}//为此节点设置数据公共无效setData(int数据){this.data =数据;}//返回数据公共诠释getData(){返回this.data;}//设置下一个指针公共无效setnext(Linked_List next_ptr){this.next_ptr = next_ptr;}//获取下一个指针公共Linked_List getnext(){返回this.next_ptr;}} 

解决方案

您必须在链接列表的单链(节点)和整个容器(LinkedList)之间进行区分.

 公共类LinkedList {节点头;整数大小;//也许公共无效insertAtEnd(int数据){节点上一个= null;for(节点current = head; current!= null; current = current.next){以前=当前;}节点婴儿=新节点(数据);如果(先前==空){头=婴儿;} 别的 {previous.next =宝贝;}++大小;}public void insertInSortedList(int data){节点上一个= null;节点当前= null;对于(current = head; current!= null& data< current.data;当前= current.next){以前=当前;}节点婴儿=新节点(数据);baby.next =当前;如果(先前==空){头=婴儿;} 别的 {previous.next =宝贝;}++大小;}}类Node {整数数据;节点下一个;节点(int数据){this.data =数据;}} 

有时可能会看到封装形式:

 公共类LinkedList {私有静态类Node {}...} 

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


}

解决方案

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 {
    }
    ...
}

这篇关于在链接列表中添加节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆