Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
删除链表java中的奇数值_Java - Fatal编程技术网

删除链表java中的奇数值

删除链表java中的奇数值,java,Java,我正在尝试删除链接列表中的奇数值。我的函数叫做RemoveOffics。我搞砸了什么?我在if语句中调用cur.next。这不应该奏效吗 这是我的密码: public class SLinkedList { public void editAtIndex(int index, int newElement) { if (index < 0 || index >= size) { return; } else {

我正在尝试删除链接列表中的奇数值。我的函数叫做RemoveOffics。我搞砸了什么?我在if语句中调用cur.next。这不应该奏效吗

这是我的密码:

public class SLinkedList {

    public void editAtIndex(int index, int newElement) {
        if (index < 0 || index >= size) {
            return;
        } else {
            Node cur = head;
            while (index > 0) {
                cur = cur.next;
                index--;
            }
            cur.setElement(newElement);
        }
    }

    public static void main(String[] args) {
        Node test = new Node(5);
        test.setElement(5);

        SLinkedList myList = new SLinkedList();
        //System.out.println(myList.contains(5));
        myList.addFirst(5);
        myList.addFirst(7);
        myList.addFirst(9);
        myList.addLast(27);
        myList.addLast(3);
        myList.addLast(453);
        myList.addLast(32);
        myList.addLast(83);
        myList.addLast(43);
        myList.addLast(10);




        myList.removeOdds();
        myList.printList();



        //myList.printList();


    }

    private Node head, tail, nextNode;

    public Node getNextNode() {
        return nextNode;
    }

    public void setNextNode(Node nextNode) {
        this.nextNode = nextNode;
    }
    private int size;

    public SLinkedList() {

    }

    public int size() {
        int value = 0;
        Node temp = head;

        while (temp != null) {
            temp = temp.next;
            value++;
        }

        return value;
    }

    public void addFirst(int element) {
        head = new Node(element, head);
        size++;
        if (size == 1) {
            tail = head;
        }
    }

    public void addLast(int element) {
        Node last = new Node(element);
        if (size == 0) {
            head = last;
            tail = last;
        } else {
            tail.setNext(last);
            tail = last;
        }
        size++;
    }

    public void removeFirst() {
        if (size == 0) {
            return;
        }
        head = head.getNext();
        size--;
        if (size == 0) {
            tail = null;
        }
    }

    public void removeLast() {
        if (size <= 1) {
            head = null;
            tail = null;
            size = 0;
        } else {
            Node cur = head;
            while (cur.next != tail) {
                cur = cur.next;
            }
            tail = cur;
            size--;
            tail.next = null;
        }
    }

    public void removeOdds() {

        if (size <= 1) {
            return;
        } else {
            Node cur = head;
            while (cur.next != tail) {
                cur = cur.next;
                if (cur.getElement() % 2 != 0) {

                    size--;
                    cur = cur.next;

                }

            }

        }

    }

    public boolean contains(int key) {
        Node cur = head;
        while (cur != null && cur.getElement() != key) {
            cur = cur.next;
        }
        if (cur == null) {
            return false;
        }
        return true;
    }

    public int indexOf(int key) {
        Node cur = head;
        int index = 0;
        while (cur != null) {
            if (cur.getElement() == key) {
                return index;
            }
            cur = cur.next;
            index++;
        }
        return -1;
    }

    public void printList() {
        System.out.println("A list of size " + size);
        Node temp = head;
        while (temp != null) {
            System.out.print(temp.getElement() + " ");
            temp = temp.next;
        }
        System.out.println();

    }

    public Node getHead() {
        return head;
    }

    public void setHead(Node head) {
        this.head = head;
    }

    public Node getTail() {
        return tail;
    }

    public void setTail(Node tail) {
        this.tail = tail;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;

    }

    private static class Node {

        private int element;
        private Node next;

        public Node(int element) {
            this.element = element;
        }

        public Node(int element, Node next) {
            this.element = element;
            this.next = next;
        }

        public Node() {
            element = 0;
        }

        public int getElement() {
            return element;
        }

        public void setElement(int element) {
            this.element = element;
        }

        public Node getNext() {
            return next;
        }

        public void setNext(Node next) {
            this.next = next;
        }

    }
}
cur=cur.next;只需将cur变量替换为下一个节点。这并不意味着删除了前面的变量。参考资料仍然保留

您需要在要删除的节点之前保留对该节点的引用

public void removeOdds() {
    if (size <= 1) {
        return;
    } else {
        Node cur = head;
        Node previous = cur;
        while (cur.next != tail) {
            cur = cur.next;
            if (cur.getElement() % 2 != 0) {
                size--;
                previous.next = cur.next;
            } else {
                previous = previous.next;
            }
        }
    }
}
这将确保删除节点。我还没有检查完整的代码,但是现在看起来您没有处理head&tail节点。他们将被跳过。您可能也希望在方法中处理这些节点