简体   繁体   English

反向链表问题

[英]reverse linked list problem

I have the following linked list program in java which works fine excep for the reverse linked list function. 我在Java中有以下链表程序,该链表程序可以很好地用于反向链表功能。 What am i missing? 我想念什么?

public class LinkedList {

private Link first;

public LinkedList()
{
    first = null;
}
public boolean isEmtpy()
{
    return(first==null);
}

public void insertFirst(int id, double dd)
{
    Link newLink=new Link(id,dd);
    newLink.next=first;     //new link --> old first
    first =newLink;         //first --> newLink
}
public Link deleteFirst()
{
    Link temp=first;
    first=first.next;
    return temp;
}
public void displayList()
{
    Link current=first;
    System.out.println("List (first-->last)");
    while(current!=null)
    {
        current.displayLink();
        current=current.next;
    }       
    System.out.println(" ");
}
public Link find(int key)
{
    Link current=first;

    while(current.iData!=key)
    {
        if(current.next==null)
            return null;    //last link
        else
            current=current.next;

    }
    return current;
}
public Link delete(int key)
{
    Link current=first;
    Link previous=first;

    while (current.iData!=key)
    {
        if (current.next==null)
            return null;
        else
        {
            previous=current;
            current=current.next;
        }
    }
    if(current==first)
        first=first.next;
    else
        previous.next=current.next;
    return current;     
}   

public void insertAfter(int key, int id, double dd)
{
    Link current=first;
    Link previous=first;

    Link newLink = new Link(id,dd);
    while (current.iData!=key)
    {
        if (current.next==null)
            System.out.println("At the last Node");
        else
        {
            previous=current;
            current=current.next;

        }
    }
    System.out.println("Value of previous "+ previous.iData);
    System.out.println("Value of current after which value will be inserted is " + current.iData);
    newLink.next=current.next;
    current.next=newLink;
}

public Link reverse()
{
    Link previous=null;
    Link current=first;
    Link forward;

    while(current!=null)
    {
        forward=current.next;
        current.next=previous;
        previous=current;
        current=forward;
    }
    return previous;
}
}

The problem is, reverse() does not set first to its new value, so the linked list will become corrupted (effectively it will be reduced to the former head element). 问题是, reverse()不会first设置为其新值,因此链接列表将被损坏(有效地,它将被简化为前一个head元素)。

You should add 您应该添加

first = previous;

in the end, before returning the value (or instead - do you really need to return the new head node?). 最后,在返回值之前(或者相反,您是否真的需要返回新的头节点?)。

Here is changed code. 这是更改的代码。 You need to make last's next to null and reinitialize first. 您需要将last放在null旁边,然后首先重新初始化。

public Link reverse()
{
    Link previous=null;
    Link current=first;
    Link forward;

    while(current!=null)
    {
        forward=current.next;
        current.next=previous;
        previous=current;
        current=forward;
    }
    first.next = null;
    first = previous;
    return previous;
}
}

The new head is returned, correct? 返回新的头,对吗? I think that the initial head's next should be set to null, or it will loop between the last two elements when traversing the list. 我认为初始标题的下一个应该设置为null,否则在遍历列表时它将在最后两个元素之间循环。

Heh, never mind... 嘿,没关系...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM