我正在尝试删除链接列表中的奇数值。我的函数叫做removeOdds。我搞砸了什么?我在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;
}
}
}
答案 0 :(得分:0)
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;
}
}
}
}
这将确保删除节点。我还没有检查完整的代码,但现在看来你现在还没有处理完整的代码。尾节点。他们将被跳过。您可能还希望在方法中处理这些节点。