简体   繁体   English

删除链表java中的奇数值

[英]deleting odd values in linked list java

I'm trying to delete odd values in a linked list. 我正在尝试删除链表中的奇数值。 My function is called removeOdds. 我的函数叫做removeOdds。 What am I messing up? 我搞砸了什么? I am calling cur.next within that if statement. 我在if语句中调用cur.next。 Shouldn't that do the trick? 不应该这样做吗?

Here is my code: 这是我的代码:

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; simply replaces the cur variable with the next node. 简单地用下一个节点替换cur变量。 That doesn't mean the earlier variable is deleted. 这并不意味着删除了早期的变量。 The reference is still held. 该引用仍然存在。

You need to hold a reference to the node before the node you wish to delete. 您需要在要删除的节点之前保留对节点的引用。

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

This will ensure the nodes get deleted. 这将确保删除节点。 I haven't checked the complete code, but it looks like right now you're not handling the head & tail nodes. 我还没有检查完整的代码,但现在看起来你没有处理头尾节点。 They'll be skipped. 他们将被跳过。 You might want to handle those nodes too in the method. 您可能还希望在方法中处理这些节点。

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

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