【问题标题】:Singly linked list append method单链表追加方法
【发布时间】:2013-03-01 04:33:17
【问题描述】:

大家好,我在尝试实现单链表的附加方法时遇到了麻烦。 这是代码:

public void append ( int item ) {
//inserts item to the end of the list
        if ( head == null){
            head = new LinkInt();
            curr = head;
            curr.elem = item;
        }
        else{
        LinkInt temp = head;
        while ( temp.next != null){
        temp = temp.next;}
        temp.elem = item;
        }


}

这是我的打印方法(不确定是否正确):

public void print () {
//outprint the array 
    //ie. <1, 2, |3, 4>
    if (  head == null) {
        System.out.print("<");
        System.out.print(">");
    }
    else{
    LinkInt temp = head;
    System.out.print("<");
    while ( temp != null) {
        if ( temp == curr){
                System.out.print( "|" + temp.elem + ","); }
        else{
        System.out.print( temp.elem );
        System.out.print(",");}
        temp = temp.next;
    }
    System.out.print(">");
    }
}

}

问题来了:

假设附加了 3 ->>> 我得到了 但是如果我在 ->>>> 之后添加了 5,我会得到 删除我的第一个项目。

请帮帮我:(

【问题讨论】:

  • 为什么不存储对尾元素的引用。会让事情变得更快。

标签: java add


【解决方案1】:

在这些声明之后:

while ( temp.next != null)
{
    temp = temp.next;
}

这样做:

tmp1= new LinkInt();
tmp1.elem = item;
tmp1.next = null

tmp.next = tmp1

而不是这个:

temp.elem = item;

试试这个打印方法:

public void print () 
{
    //outprint the array 
    //ie. <1, 2, |3, 4>
    if (  head == null) 
    {
        System.out.print("<");
        System.out.print(">");
    }
    else
    {
        LinkInt temp = head;
        System.out.print("<");
        while ( temp->next != null) 
        {
            System.out.print( "|" + temp.elem + ","); 
            temp = temp.next;
        }
        System.out.print("|" + temp.elem);}
        System.out.print(">");
    }

}

【讨论】:

  • 还有一个问题我如何修改我的 print() 以便在最后一个元素之后没有逗号? :D
  • 我已经编辑了我的答案以包含 print()。检查它是否正确。告诉我
【解决方案2】:
LinkInt temp = head;
while ( temp.next != null){
    temp = temp.next;
}
temp.elem = item;

它的作用是 - temp.next is null3 已经插入时。因此,它转到temp.elem = item 并覆盖您现有的值。做这样的事情:-

LinkInt temp = head;
while ( temp.next != null){
    temp = temp.next;
}
//temp.elem = item; -Not needed.

temp1= new LinkInt();
temp1.elem = item;
temp1.next = null;
temp.next = temp1;

【讨论】:

  • 为什么不赞成,这个答案对我来说似乎是正确的?任何解释为什么它是错误的,这样我也可以消除我的疑虑:-)
  • 我的意思是点击支票,但我不小心点击了否决票:(对不起,还有一个问题我如何修改我的 print() 以便在最后一个没有逗号的元素之后?:D跨度>
  • @user2059472 :您可以为此添加另一个条件,我想,例如if (temp.next == null) System.out.print(temp.elem + "&gt;") else System.out.print(temp.elem + ", ")
  • @user2059472 - 您可以再次单击downvote 以撤消它:)
【解决方案3】:

有这种方法

public void append(int item)  
{  
    LinkInt l = new LinkInt();  
    l.elem = item;  
    if ( head == null )  
        head = l;  
    else {  
        LinkInt tmp = head;  
        while ( tmp.next != null)  
            tmp = tmp.next;  
        tmp.next = l;  
}

【讨论】:

  • 你没有向节点的下一部分添加任何东西,到新创建的节点,我猜你假设new LinkInt() 实际上给了一个具有null 值的节点next部分。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-14
  • 2018-03-28
  • 1970-01-01
  • 1970-01-01
  • 2020-11-25
相关资源
最近更新 更多