简体   繁体   English

链表变量初始化遇到麻烦

[英]Having trouble with Linked List variable initialization

I'm having trouble with my Delete(Node p) method, I almost have it completely done but whenever I try to compile it, it says that previous may not have been previously initialized. 我在使用Delete(Node p)方法时遇到麻烦,几乎可以完全完成它,但是每当我尝试对其进行编译时,它都表示先前可能尚未初始化。 Specifically in then if statement in this method below. 在下面的方法中专门在then if语句中。 I'm brand new to linked lists so I was just trying to practice getting the syntax right. 我是链接列表的新手,所以我只是在尝试练习正确的语法。 Any help would be awesome. 任何帮助都是极好的。 Thanks Guys. 多谢你们。

public Node delete(Node p)
    {
        Node current, previous;
        current = head.next;

        while (current.info != p.info && current.next != null)
        {
            previous = current;
            current = current.next;
        }

        if (current.info == p.info)
        {
            previous.next = current.next;
            //this previous here is what is giving me trouble
        }
        return current;
    }

//end of trouble method, start of whole code //麻烦方法结束,整个代码开始

public class LinkedList
    {
        Node head;

        public class Node
        {
            int info;
            Node next;
            Node(int d)
            {
                info = d;
                next = null;
            }
        }

        public void insertAfter(Node prevnode, int new_info)
        {
            if (prevnode == null)
            {
                System.out.println("The given previous node cannot be null");
                return;
            }
            Node newnode = new Node(new_info);
            newnode.next = prevnode.next;
            prevnode.next = newnode;
        }

        public void insertEnd(int new_info)
        {
            Node newnode = new Node(new_info);
            if (head == null)
            {
                head = new Node(new_info);
                return;
            }
            newnode.next = null;
            Node last = head;
            while(last.next != null)
            {
                last = last.next;
            }
            last.next = newnode;
            return;
        }

        public void insertFirst(int new_info)
        {
            Node newnode = new Node(new_info);
            newnode.next = head;
            head = newnode;
        }

        public Node delete(Node p)
        {
            Node current, previous;
            current = head.next;

            while (current.info != p.info && current.next != null)
            {
                previous = current;
                current = current.next;
            }

            if (current.info == p.info)
            {
                previous.next = current.next;
            }
            return current;
        }

        public void printList()
        {
            Node tnode = head;
            while (tnode != null)
            {
                System.out.print(tnode.info + " ");
                tnode = tnode.next;
            }
        }

        public static void main(String[] args)
        {
            LinkedList slist = new LinkedList();
            slist.insertEnd(6);
            slist.insertFirst(7);
            slist.insertFirst(1);
            slist.insertEnd(4);
            slist.insertAfter(slist.head.next, 8);

            System.out.println("Top Ten Gaming Scores Are: ");
            slist.printList();

        }
    }

If the condition in your while loop: while (current.info != p.info && current.next != null) never evaluates to true, you do not enter the loop and as the warning says previous may not be initialized when you go to use it. 如果while循环中的条件: while (current.info != p.info && current.next != null)从未求值为true,则您不会进入循环,并且由于警告说您进去时可能未初始化previous的循环使用它。

You should make sure to check that previous != null before you use it in the if statement. 您应该确保在if语句中使用previous != null之前进行检查。

You have to always initialize local variables . 您必须始终初始化local variables Setting previous =null before the while block will resolve your issues. 在while块之前设置previous =null将解决您的问题。

public Node delete(Node p)
    {
        Node current; 
        Node previous =null;
        current = head.next;

        while (current.info != p.info && current.next != null)
        {
            previous = current;
            current = current.next;
        }

        if (current.info == p.info)
        {
            previous.next = current.next;
            //this previous here is what is giving me trouble
        }
        return current;
    }

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

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