【发布时间】:2020-11-16 09:35:36
【问题描述】:
我想在 java 中反转单链表中的偶数,但我很难获得正确的输出。 例如, 输入:2、18、24、3、5、7、9、6、12 该方法应该反转偶数only,它们是 {2,18,24} 和 {6,12}
正确的输出:24 , 18 ,2 , 3 , 5 ,7 , 9 , 12 , 6
但是,我的输出:24 18 3 5 7 9 12 6 这是错误的
主要方法
public static void main(String[] args) throws Exception {
SLL<Integer> p = new SLL<Integer>();
int[] e = { 2, 18, 24, 3, 5, 7, 9, 6, 12,5 ,4 ,3 ,2,6,8};
for (int i = 0; i < e.length; i++) {
p.addToHead(e[i]);
}
p = reverse(p);
p.printAll();
}
这是方法(不能正常工作)
public static SLL<Integer> reverse(SLL<Integer> p) {
SLL<Integer> returnList = new SLL<Integer>();
Stack<Integer> stk = new Stack<Integer>();
for (SLLNode tmp = p.getHead(); tmp != null; tmp = tmp.next) {
if ((((Integer) tmp.info) % 2) != 0) {
returnList.addToHead((Integer) tmp.info);
p.deleteFromHead();
} else if ((((Integer) tmp.info) % 2) == 0) {
stk.push((Integer) tmp.info);
p.deleteFromHead();
}
if (stk.getLSize() >= 2) {
while (!(stk.isEmpty())) {
returnList.addToHead((Integer) stk.pop());
}
}
}
return returnList;
}
这是 SLLNode 类
public class SLLNode<T> {
public T info;
public SLLNode<T> next;
public SLLNode() {
this(null,null);
}
public SLLNode(T el) {
this(el,null);
}
public SLLNode(T el, SLLNode<T> ptr) {
info = el;
next = ptr;
}
}
这是 SLL 类
public class SLL<T> {
protected SLLNode<T> head, tail;
public SLL() {
head = tail = null;
}
public boolean isEmpty() {
return head == null;
}
public void addToHead(T el) {
head = new SLLNode<T>(el, head);
if (tail == null)
tail = head;
}
public SLLNode getHead(){
return head;
}
public void addToTail(T el) {
if (!isEmpty()) {
tail.next = new SLLNode<T>(el);
tail = tail.next;
} else
head = tail = new SLLNode<T>(el);
}
public T deleteFromHead() { // delete the head and return its info;
if (isEmpty())
return null;
T el = head.info;
if (head == tail) // if only one node on the list;
head = tail = null;
else
head = head.next;
return el;
}
public T deleteFromTail() { // delete the tail and return its info;
if (isEmpty())
return null;
T el = tail.info;
if (head == tail) // if only one node in the list;
head = tail = null;
else { // if more than one node in the list,
SLLNode<T> tmp; // find the predecessor of tail;
for (tmp = head; tmp.next != tail; tmp = tmp.next)
;
tail = tmp; // the predecessor of tail becomes tail;
tail.next = null;
}
return el;
}
public void delete(T el) { // delete the node with an element el;
if (!isEmpty())
if (head == tail && el.equals(head.info)) // if only one
head = tail = null; // node on the list;
else if (el.equals(head.info)) // if more than one node on the list;
head = head.next; // and el is in the head node;
else { // if more than one node in the list
SLLNode<T> pred, tmp;// and el is in a nonhead node;
for (pred = head, tmp = head.next; tmp != null
&& !tmp.info.equals(el); pred = pred.next, tmp = tmp.next)
;
if (tmp != null) { // if el was found;
pred.next = tmp.next;
if (tmp == tail) // if el is in the last node;
tail = pred;
}
}
}
public void printAll() {
for (SLLNode<T> tmp = head; tmp != null; tmp = tmp.next)
System.out.print(tmp.info + " ");
}
public boolean isInList(T el) {
SLLNode<T> tmp;
for (tmp = head; tmp != null && !tmp.info.equals(el); tmp = tmp.next)
;
return tmp != null;
}
public int length() {
int length = 0;
for (SLLNode tmp = head; tmp != null; tmp = tmp.next) {
length += 1;
}
return length;
}
【问题讨论】:
-
你能展示你用来确定你的输出的代码吗? reverse 方法返回一个 SLL
对象,它似乎没有简单的方法来显示结果。 -
我已经添加了
-
SLL 类似乎没有 printAll 方法。你也可以展示一下代码吗?
-
我很抱歉。您现在可以在 SLL 类中找到它
-
谢谢 - 如果您在
p=reverse(p)之前添加p.printAll();,您会看到输入实际上是12 6 9 7 5 3 24 18 2。这将是因为您使用 addHead() 而不是 addTail() 来创建输入。我现在得到和你一样的输出。我在下面建议的答案似乎对我有用。输出将显示为6 12 9 7 5 3 2 18 24,这是我对12 6 9 7 5 3 24 18 2输入的期望
标签: java linked-list reverse singly-linked-list