帮助在Java中创建单链表

帮助在Java中创建单链表,java,linked-list,Java,Linked List,这是家庭作业,但请知道,我已经在网上寻求帮助(如)和我的课本,但我仍然有一些问题 任何帮助都将不胜感激 现在我正试图在中插入值,但没有任何效果。无论它是第一项,还是作为最后一项添加,或者介于两者之间 Node header = null; // First element of list. Node back = null; // Last element of list. public void insert(int i, double value){ //insert val

这是家庭作业,但请知道,我已经在网上寻求帮助(如)和我的课本,但我仍然有一些问题

任何帮助都将不胜感激

现在我正试图在中插入值,但没有任何效果。无论它是第一项,还是作为最后一项添加,或者介于两者之间

Node header = null;    // First element of list.
Node back  = null;    // Last element of list.

public void insert(int i, double value){ //insert value before i-th element
  Node e = new Node();
  e.num = value;
  Node curr = header;
  for(int x=0;x<i;x++) {
   if (i == 1) { //we want to insert as first thing
    if (size == 0) { //its the FIRST time we add something
     header.next = e;
     e.next = back;
     break;
    } else if (size == 1){
     e.next = header.next; //i.e. the second thing in the list
     header.next = e;
     break;
    } else {
     e.next = header.next.next; //i.e. the second thing in the list
     header.next = e;
     break;
    }
   }
   else if (x == (i-1)) {
    e.next = curr.next;
    curr.next = e;
    break;
   }
   curr = curr.next;
  }
  size = size+1;
 }
节点头=null;//列表的第一个元素。
Node back=null;//列表的最后一个元素。
公共void insert(int i,double value){//在第i个元素之前插入值
节点e=新节点();
e、 num=值;
节点curr=头;
对于(int x=0;x一些建议:

  • 实现java.util.List
  • 想想泛型
  • 阅读
  • 在考虑“在i处插入”之前,先从“在末尾插入”开始。有几点建议:

  • 实现java.util.List
  • 想想泛型
  • 阅读
  • 在考虑“在i处插入”之前,先从“在末尾插入”开始

  • e.next=header.next.next
    行中,如果
    header.next
    指向一个“null”,会发生什么?是否可能到达那里
  • 你必须处理哪些角落的案例?你是否已将它们全部考虑在内
  • 能否先从最简单的情况开始,在前面添加一个元素或在后面添加一个元素?然后使用这些函数实现插入
  • e.next=header.next.next
    行中,如果
    header.next
    指向一个“null”,会发生什么?是否可能到达那里
  • 你必须处理哪些角落的案例?你是否已将它们全部考虑在内
  • 能否先从最简单的情况开始,在前面添加一个元素或在后面添加一个元素?然后使用这些函数实现插入

  • 出于某种原因,那些仍在学习编程的人会让事情变得比他们需要的复杂得多。我在学习java的时候做过,我在刚开始学习一门新语言的时候也做过,我标记的学生会找到新的、令人惊奇的方法来做。例如,你在插入中会有更多的内容e、 在特定索引处插入值的方法不应该检查它是否是要插入的第一项(不是说它不应该检查边界)

    insert(index, value)
        if index>size
            throw null pointer
        traverse to index -1 //lets call this nodeI
        create newnode and set value
        set newnode.next to nodeI.next
        set nodeI.next to newnode
        increase size.
    
    对您来说,有两个方便的提示,您应该有一个函数从链接列表中获取一个元素,返回一个节点?例如,public node elementAt(int index)?使用该函数遍历链接列表。如果您想附加到链接列表,请尝试以下操作

    append(value)
        insert(size-1,value)
    
    如果你想在开头插入?同样的想法

    insert(value)
        insert(0,value)
    

    出于某种原因,那些仍在学习编程的人会让事情变得比他们需要的复杂得多。我在学习java的时候做过,我在刚开始学习一门新语言的时候也做过,我标记的学生会找到新的、令人惊奇的方法来做。例如,你在插入中会有更多的内容e、 在特定索引处插入值的方法不应该检查它是否是要插入的第一项(不是说它不应该检查边界)

    insert(index, value)
        if index>size
            throw null pointer
        traverse to index -1 //lets call this nodeI
        create newnode and set value
        set newnode.next to nodeI.next
        set nodeI.next to newnode
        increase size.
    
    对您来说,有两个方便的提示,您应该有一个函数从链接列表中获取一个元素,返回一个节点?例如,public node elementAt(int index)?使用该函数遍历链接列表。如果您想附加到链接列表,请尝试以下操作

    append(value)
        insert(size-1,value)
    
    如果你想在开头插入?同样的想法

    insert(value)
        insert(0,value)
    

    我已经尝试了一个简单的程序,这将对你们有用,我也在学习Java,请容忍我的任何错误,但这个程序运行良好

    我正在用Java发布一个非常简单的单链表程序,我今天试用了它。 我希望这对大家都有帮助

    LinkList.java

    class LinkList
    {
     public static void main(String args[])
     {
      Node node = new Node(1);
      node.addAtLast(2);
      node.addAtLast(3);
      node.addAtLast(4);
      node.addAtLast(5);
      node.printList();
     }
    
    }
    
    class Node
    {
     private int data;
     private Node link;
    
     public Node(int mydata)
     {
      data = mydata;
      link = null;
     }
    
     public void printList()
    {
     System.out.print("|"+data+"|"+"->");
     if(link != null)  
     {
    //recursive call
     link.printList();
    
     }
    else
     {
    //marking end of list as NULL
     System.out.print("|NULL|"); 
     }
    }
    
    public void addAtLast(int mydata)
    {
     if(link == null)
     {
    
      link = new Node(mydata);
     }
     else
     {
      link.addAtLast(mydata);
     }
    
    }
    
    }
    
    Node.java

    class LinkList
    {
     public static void main(String args[])
     {
      Node node = new Node(1);
      node.addAtLast(2);
      node.addAtLast(3);
      node.addAtLast(4);
      node.addAtLast(5);
      node.printList();
     }
    
    }
    
    class Node
    {
     private int data;
     private Node link;
    
     public Node(int mydata)
     {
      data = mydata;
      link = null;
     }
    
     public void printList()
    {
     System.out.print("|"+data+"|"+"->");
     if(link != null)  
     {
    //recursive call
     link.printList();
    
     }
    else
     {
    //marking end of list as NULL
     System.out.print("|NULL|"); 
     }
    }
    
    public void addAtLast(int mydata)
    {
     if(link == null)
     {
    
      link = new Node(mydata);
     }
     else
     {
      link.addAtLast(mydata);
     }
    
    }
    
    }
    
    输出:

    下面是我们的输出

    |1 |->2 |->3 |->4 |->5 |->空|


    我已经尝试了一个简单的程序,这将对你们有用,我也在学习Java,请容忍我的任何错误,但这个程序运行良好

    我正在用Java发布一个非常简单的单链表程序,我今天试用了它。 我希望这对大家都有帮助

    LinkList.java

    class LinkList
    {
     public static void main(String args[])
     {
      Node node = new Node(1);
      node.addAtLast(2);
      node.addAtLast(3);
      node.addAtLast(4);
      node.addAtLast(5);
      node.printList();
     }
    
    }
    
    class Node
    {
     private int data;
     private Node link;
    
     public Node(int mydata)
     {
      data = mydata;
      link = null;
     }
    
     public void printList()
    {
     System.out.print("|"+data+"|"+"->");
     if(link != null)  
     {
    //recursive call
     link.printList();
    
     }
    else
     {
    //marking end of list as NULL
     System.out.print("|NULL|"); 
     }
    }
    
    public void addAtLast(int mydata)
    {
     if(link == null)
     {
    
      link = new Node(mydata);
     }
     else
     {
      link.addAtLast(mydata);
     }
    
    }
    
    }
    
    Node.java

    class LinkList
    {
     public static void main(String args[])
     {
      Node node = new Node(1);
      node.addAtLast(2);
      node.addAtLast(3);
      node.addAtLast(4);
      node.addAtLast(5);
      node.printList();
     }
    
    }
    
    class Node
    {
     private int data;
     private Node link;
    
     public Node(int mydata)
     {
      data = mydata;
      link = null;
     }
    
     public void printList()
    {
     System.out.print("|"+data+"|"+"->");
     if(link != null)  
     {
    //recursive call
     link.printList();
    
     }
    else
     {
    //marking end of list as NULL
     System.out.print("|NULL|"); 
     }
    }
    
    public void addAtLast(int mydata)
    {
     if(link == null)
     {
    
      link = new Node(mydata);
     }
     else
     {
      link.addAtLast(mydata);
     }
    
    }
    
    }
    
    输出:

    下面是我们的输出

    |1 |->2 |->3 |->4 |->5 |->空|


    这对于在i
    插入的
    来说太多了。有两件事:1)如果可以的话,可以这样构造节点:
    node node=new node(val);
    ,2)简化逻辑。将其分解为:A)找到要插入的节点,或者抛出异常/返回null(无论约定如何),B)执行插入(事实上,这应该是node类本身的一个方法。你应该在
    节点
    和'LinkedList'类之间划分责任)。顺便说一下,LL的标准名称应该是“head”和“tail”,而不是“header”和“back”。想想snake,而不是Excel。header到底是什么?@Harnish-很好的评论。+1只是为了“这对于在i插入的代码来说太多了”。请注意。我正要说“带代码的家庭作业,大-大+1用于罕见的情况”。然后我没有看到错误消息…对于在i插入的
    来说,这太多了。两件事:1)如果可以的话,像这样构造节点:
    节点=新节点(val);
    ,2)简化逻辑。将其分解为:A)找到要插入的节点,或抛出异常/返回null(无论惯例如何),B)执行插入(实际上这应该是节点类本身的一种方法。您应该在
    节点
    和“LinkedList”类之间分担责任)顺便说一句,LL的标准名称应该是“头”和“尾”,而不是“头”和“背”。想想蛇,而不是Excel。这到底是怎么回事