利用链表不工作的方法

时间:2016-01-15 18:57:47

标签: java linked-list display

显示方法是我的程序根本没有显示任何内容。

程序必须将某人的全名放入循环链表中,然后将链表备份到另一个循环链表中。

然后,用户必须删除名称,直到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);
    }
}

1 个答案:

答案 0 :(得分:0)

你已陷入Java的简单陷阱。您已将要更新的值传递给函数,并期望在调用方法中更新该值。就在这里:

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

以下是发生的事情:Java正在按值传递指针。这意味着它正在创建指针(backuptail)的副本,以便在方法backup()中使用。这意味着main()中的局部变量永远不会更新。

修复很简单。更改备份方法以返回值:

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

然后适当地改变你的方法调用:

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

现在生成的备份指针存储在main中,并且可以传递给display()。