简体   繁体   English

利用链表的方法不起作用

[英]Method utilising linked list not working

The display method is my program is not displaying anything at all. 显示方法是我的程序根本不显示任何内容。

The program has to have someone entire names into a circular linked list, then backup the linked list into another circular linked list. 该程序必须将某人的全名放入循环链接列表中,然后将链接列表备份到另一个循环链接列表中。

Then it user must delete names until 1 is left, and the display the winner along with the list of original names, using the backup in the order that they were entered 然后,用户必须删除名称,直到剩下1,然后按输入顺序使用备份显示获奖者以及原始名称列表

import java.io.*;
import java.util.*;
import java.text.*;

public class Linkedlist
{
    static public class Node
    {
        Node prev, next;
        String data;
    }


    public static void delete (Node tail) throws IOException
    {

        BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
        System.out.println ("Please input a name to be deleted");
        String tobedeleted = stdin.readLine ();
        Node delete = tail;
        while (delete.prev != null)
        {
            if (delete.data.equals (tobedeleted))
            {
                String temp = delete.data;
                delete.data = tail.data;
                tail.data = temp;
                tail = tail.prev;
            }
            delete = delete.prev;
        }
    }


    public static String findvictor (Node tail) throws IOException
    {
        int size = 0;
        for (Node n = tail ; n.prev != null ; n = n.prev)
        {
            size++;
        }
        if (size == 1)
        {
            return tail.data;
        }
        else
        {
            delete (tail);
            return findvictor (tail.prev);
        }
    }


    public static void backup (Node tail, Node backuptail)
    {
        Node tobebackuped = tail;
        Node backuphead = null;
        Node backup = new Node ();
        backuptail = backup;
        while (tobebackuped.prev != null)
        {
            backup.data = tobebackuped.data;
            backuphead = backup;
            backup = new Node ();
            backup.next = backuphead;
            backuphead.prev = backup;
            tobebackuped = tobebackuped.prev;
        }
    }


    public static void display (Node tail, Node backuptail) throws IOException
    {
        System.out.println ("CONGRATULATIONS, " + findvictor (tail) + ", YOU ARE THE WINNER!");
        System.out.println ("");
        System.out.println ("This is a list of all the contestants:");
        Node current = backuptail;
        while (current.prev != null)
        {
            System.out.println (current.data);
            current = current.prev;
        }
    }


    public static void main (String[] args) throws IOException
    {
        BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
        Node head = null;
        Node node = new Node ();
        Node tail = node;
        while (true)
        {
            String str = stdin.readLine ();
            if (!str.equals ("fin"))
            {
                node.data = str;
                head = node;
                node = new Node ();
                node.next = head;
                head.prev = node;
            }
            else
            {
                break;
            }
        }
        Node backuptail = null;
        backup (tail, backuptail);
        display (tail, backuptail);
    }
}

You've fallen into a simple trap in Java. 您已经陷入了Java的简单陷阱。 You've passed a value that you want to update into a function, and expected the value to be updated in the calling method. 您已将要更新的值传递给函数,并期望该值将在调用方法中更新。 Right here: 就在这儿:

Node backuptail = null;
backup (tail, backuptail);
display (tail, backuptail);

Here's what's happening: Java is passing your pointer by value. 这是发生的事情:Java正在按值传递指针。 That means it's creating a copy of your pointer (backuptail) for use within the method backup(). 这意味着它正在创建指针的副本(backuptail),以供在backup()方法中使用。 That means your local variable in main() never gets updated. 这意味着main()中的局部变量永远不会更新。

The fix is simple. 解决方法很简单。 Change your backup method to return the value instead: 更改备份方法以返回值:

public static Node backup (Node tail)
{
    Node tobebackuped = tail;
    Node backuphead = null;
    Node backup = new Node ();
    Node backuptail = backup;
    while (tobebackuped.prev != null)
    {
        backup.data = tobebackuped.data;
        backuphead = backup;
        backup = new Node ();
        backup.next = backuphead;
        backuphead.prev = backup;
        tobebackuped = tobebackuped.prev;
    }

    return backuptail;
}

Then change your method calls apropriately: 然后适当地更改您的方法调用:

Node backuptail = backup (tail);
display (tail, backuptail);

Now the resulting backup pointer is stored locally within your main, and can be passed to display(). 现在,生成的备份指针存储在您的main内部,可以传递给display()。

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

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