反向链表问题

发布于 2024-10-28 03:34:25 字数 2223 浏览 2 评论 0原文

我在java中有以下链表程序,除了反向链表功能外,它运行良好。 我缺少什么?

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

I have the following linked list program in java which works fine excep for the reverse linked list function.
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;
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深爱成瘾 2024-11-04 03:34:25

问题是,reverse() 不会将 first 设置为其新值,因此链表将被损坏(实际上它将被减少到以前的头元素)。

您应该

first = 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).

You should add

first = previous;

in the end, before returning the value (or instead - do you really need to return the new head node?).

私野 2024-11-04 03:34:25

这是更改后的代码。您需要将最后一个设置为空并首先重新初始化。

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

Here is changed code. You need to make last's next to null and reinitialize first.

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;
}
}
撕心裂肺的伤痛 2024-11-04 03:34:25
// approach 1 : Iterative
public void reverseIterative() {

    if (isEmpty() || first.next == null)
        return;

    Link q = null;
    Link p = first;
    Link r;

    while(p != null) {
        r = p.next;
        p.next = q;
        q = p;
        p = r;
    }

    first = q;
}


// approach 2 : Recursive 
// returns first Link
public Link reverseRecursive(Link first){ //returns first

    if(isEmpty() || first.next == null)
        return first;

    Link newHead = reverseRecursive(first.next);
    first.next.next = first;
    first.next = null;
    return newHead;
}
// approach 1 : Iterative
public void reverseIterative() {

    if (isEmpty() || first.next == null)
        return;

    Link q = null;
    Link p = first;
    Link r;

    while(p != null) {
        r = p.next;
        p.next = q;
        q = p;
        p = r;
    }

    first = q;
}


// approach 2 : Recursive 
// returns first Link
public Link reverseRecursive(Link first){ //returns first

    if(isEmpty() || first.next == null)
        return first;

    Link newHead = reverseRecursive(first.next);
    first.next.next = first;
    first.next = null;
    return newHead;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文