繁体   English   中英

单链接列表:删除

[英]Singly Linked List: Removing

试图编写一种方法来从单链列表中删除值的所有实例,但似乎无法正常工作。

我试图适应头部是否包含该值,但是我不确定这是否是正确的方法:

public void remove (int value)
{
    if (head.value == value)
    {
    head = head.next;
    count--;
    }
    IntegerNode temp=head;
    while (temp !=null)
    {
        if (temp.next != null)
        {
            if (temp.next.value == value)
            {
                temp.next = temp.next.next;
                count--;
            }
        }
        temp=temp.next;
    }
}

我的代码有明显的错误吗?

这里用addremove方法用测试执行链表。

public class ListDemo {
    public static void main(String[] args) {
        MyList list = new MyList();
        list.addToEnd(1);
        list.addToEnd(2);
        list.addToEnd(3);

        list.removeByValue(2);
        list.removeByValue(3);
    }

}

class MyList {
    private IntegerNode head;
    private int count = 0;

    public void addToEnd(int value) {

        if(head == null) {
            head = new IntegerNode(value);
            count = 1;
            head.next = null;
            return;
        }
        IntegerNode current = head;
        while (current.next != null) {
            current = current.next;
        }
        IntegerNode node = new IntegerNode(value);
        node.next = null;

        count++;
        current.next = node;
    }

    public void removeByValue(int value) {
        if (count == 0) {
            return;
        } else if (count == 1) {
            if (head.value == value) {
                count = 0;
                head = null;
            }

        } else {
            IntegerNode current = this.head;
            IntegerNode next = current.next;
            while (next != null) {
                if (next.value == value) {
                    if (next.next == null) {
                        current.next = null;
                        count--;
                        return;
                    } else {
                        current.next = next.next;
                        count--;
                    }
                }
                next = next.next;
            }
        }
    }
}

class IntegerNode {
    IntegerNode(int value) {
        this.value = value;
    }

    IntegerNode next;
    int value;
}

这是从链接列表中删除的不同方法

public Node removeAtFront()
    {
        Node returnedNode = null;

        if(rootNode !=null)
        {

            if(rootNode.next !=null)
            {
                Node pointer = rootNode;
                returnedNode = rootNode;
                pointer = null;
                rootNode = rootNode.next;
            }
            else
            {
                Node pointer = rootNode;
                returnedNode = rootNode;
                pointer = null;
                rootNode = rootNode.next;
                System.out.println("removing the last node");
            }
        }else
        {
            System.out.println("the linkedlist is empty");
        }

        return returnedNode;
    }

    public Node removeAtBack()
    {
        Node returnedNode = null;

        if(rootNode != null)
        {
            //Remove the commented line if you wish to keep 1 node as minimum in the linked list
            //if(rootNode.next !=null)
            //{
                Node pointer = new students();
                pointer = rootNode;

                while(pointer.next.next !=null)
                {
                    pointer=pointer.next;
                }

                returnedNode = pointer.next.next;
                pointer.next.next = null;
                pointer.next = null;
            //}
            //else
            //{
            //  System.out.println("cant remove the last node because its the root node");
            //}
        }
        else
        {
            System.out.println("the linkedlist is empty");
        }

        return returnedNode;
    }

    public Node removeNode(String name)
    {
        Node returnedNode = null;

        if(rootNode !=null)
        {
            Node pointer = new students();
            Node previous = new students();

            pointer = rootNode;


            while(pointer !=null)
            {
                if(pointer.name.equals(name))
                {
                    previous.next = pointer.next;
                    returnedNode = pointer;
                    pointer = null;

                    break;
                }
                else
                {
                    previous = pointer;
                    pointer = pointer.next;
                }
            }

        }
        else
        {
            System.out.println("the linkedlist is empty");
        }

        return returnedNode;
    }

    public boolean isEmpty() {

        return rootNode == null; 
    }

您需要跟踪您在列表中的位置,而不是去往的位置。 像这样:

public void remove (int value)
{
    IntegerNode current = head;

    while (current !=null)
    {
        if (current.value == value)
        {
            if (head == current)
            {
                head = current.next;
            }
            else
            {
                head.next = current.next;
            }
            count--;
        }
        current=current.next;
    }
}

暂无
暂无

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

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