java删除奇数文件_java - 删除链表java中的奇数值 - 堆栈内存溢出

我正在尝试删除链表中的奇数值。 我的函数叫做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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值