Java 从给定的单链表中反转偶数(仅)
我想在java中反转单链表中的偶数,但要获得正确的输出,我面临一些困难。 例如 输入:2,18,24,3,5,7,9,6,12 该方法应仅反转偶数即{2,18,24}和{6,12} 正确的输出:24、18、2、3、5、7、9、12、6 但是,我的输出:24 18 3 5 7 9 12 6这是错误的 主要方法Java 从给定的单链表中反转偶数(仅),java,linked-list,reverse,singly-linked-list,Java,Linked List,Reverse,Singly Linked List,我想在java中反转单链表中的偶数,但要获得正确的输出,我面临一些困难。 例如 输入:2,18,24,3,5,7,9,6,12 该方法应仅反转偶数即{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
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();
}
publicstaticvoidmain(字符串[]args)引发异常{
SLL p=新的SLL();
int[]e={2,18,24,3,5,7,9,6,12,5,4,3,2,6,8};
for(int i=0;i
这是一种方法(无法正常工作)
公共静态SLL反转(SLL p){
SLL returnList=新的SLL();
堆栈stk=新堆栈();
for(SLLNode tmp=p.getHead();tmp!=null;tmp=tmp.next){
如果(((整数)tmp.info)%2)!=0){
returnList.addToHead((整数)tmp.info);
p、 deleteFromHead();
}如果(((整数)tmp.info)%2)==0,则为else{
stk.push((整数)tmp.info);
p、 deleteFromHead();
}
if(stk.getLSize()>=2){
而(!(stk.isEmpty()){
returnList.addToHead((整数)stk.pop());
}
}
}
退货清单;
}
这是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;
}
公共类SLLNode{
公共信息;
公共SLLNode next;
公共SLLNode(){
这个(空,空);
}
公共SLLNode(T el){
这(el,null);
}
公共SLLNode(T el,SLLNode 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;
}
公共类SLL{
受保护的SLLNode头、尾;
公共SLL(){
头=尾=空;
}
公共布尔值为空(){
返回头==null;
}
公共无效地址(T el){
头部=新SLLNode(el,头部);
if(tail==null)
尾=头;
}
公共SLLNode getHead(){
回流头;
}
公共无效地址(T el){
如果(!isEmpty()){
tail.next=新SLLNode(el);
tail=tail.next;
}否则
头部=尾部=新SLLNode(el);
}
public T deleteFromHead(){//删除头部并返回其信息;
if(isEmpty())
返回null;
T el=head.info;
if(head==tail)//如果列表上只有一个节点;
头=尾=空;
其他的
head=head.next;
返回el;
}
public T deleteFromTail(){//删除尾部并返回其信息;
if(isEmpty())
返回null;
T el=tail.info;
if(head==tail)//如果列表中只有一个节点;
头=尾=空;
else{//如果列表中有多个节点,
SLLNode tmp;//查找tail的前一个;
for(tmp=head;tmp.next!=tail;tmp=tmp.next)
;
tail=tmp;//tail的前置成为tail;
tail.next=null;
}
返回el;
}
public void delete(tel){//删除带有元素el的节点;
如果(!isEmpty())
if(head==tail&&el.equals(head.info))//如果只有一个
head=tail=null;//列表上的节点;
else if(el.equals(head.info))//如果列表上有多个节点;
head=head.next;//且el在head节点中;
else{//如果列表中有多个节点
SLLNode pred,tmp;//和el位于非头节点中;
for(pred=head,tmp=head.next;tmp!=null
&&!tmp.info.equals(el);pred=pred.next,tmp=tmp.next)
;
如果(tmp!=null){//如果找到了el;
pred.next=tmp.next;
if(tmp==tail)//如果el在最后一个节点中;
tail=pred;
}
}
}
public void printAll(){
for(SLLNode tmp=head;tmp!=null;tmp=tmp.next)
系统输出打印(tmp.info+);
}
公共布尔isInList(TEL){
sllnodetmp;
for(tmp=head;tmp!=null&&!tmp.info.equals(el);tmp=tmp.next)
;
返回tmp!=null;
}
公共整数长度(){
整数长度=0;
for(SLLNode tmp=head;tmp!=null;tmp=tmp.next){
长度+=1;
}
返回长度;
}
运行反向方法会得到与您所看到的不同的输出。因此,我怀疑您的输出来自稍微不同的代码
我得到:24,6,9,7,5,3,2,18
在反向方法中,当堆栈上有2个时,您开始将偶数添加到返回列表中。如果要反向所有偶数,您需要等待堆栈上有所有连续偶数。或者换句话说,当您得到奇数或没有剩余数字时,您可以将所有偶数从st中弹出阿克
我认为你也应该使用addTail而不是addHead
大概是
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) {
// add any stacked even numbers
while (!(stk.isEmpty())) {
returnList.addToTail((Integer) stk.pop());
}
// add the odd number
returnList.addToTail((Integer) tmp.info);
} else if ((((Integer) tmp.info) % 2) == 0) {
System.out.println("even " + tmp.info);
stk.push((Integer) tmp.info);
}
}
// add any remaining even numbers from the stack
while (!(stk.isEmpty())) {
returnList.addToTail((Integer) stk.pop());
}
return returnList;
}
公共静态SLL反转(SLL p){
SLL returnList=新的SLL();
堆栈stk=新堆栈();
for(SLLNode tmp=p.getHead();tmp!=null;tmp=tmp.next){
如果(((整数)tmp.info)%2)!=0){
//添加任意堆叠的偶数
而(!(stk.isEmpty()){
returnList.addToTail((整数)stk.pop());
}
//将奇数相加
returnList.addToTail((整数)tmp.info);
}如果(((整数)tmp.info)%2)==0,则为else{
System.out.println(“偶数”+tmp.info);
stk.push((整数)tmp.info);
}
}
//添加堆栈中剩余的偶数
而(!(stk.isEmpty()){
returnList.addToTail((整数)stk.pop());
}
退货清单;
}
我是用python列表编写的,我只是想知道我是否可以不用链表来编写这个程序。虽然复杂度可能很高,但它只是一个实验性的实现,可以工作
arr = [2,18,24,3,5,7,9,6,12]
arr_final = []
temp = []
for i in arr:
if i%2 == 0:
temp.append(i)
else:
if temp!=[]:
temp.reverse()
for j in temp:
arr_final.append(j)
temp = []
arr_final.append(i)
if temp!=[]:
temp.reverse()
for j in temp:
arr_final.append(j)
temp = []
print(arr_final)
你能展示一下你用来确定的代码吗
arr = [2,18,24,3,5,7,9,6,12]
arr_final = []
temp = []
for i in arr:
if i%2 == 0:
temp.append(i)
else:
if temp!=[]:
temp.reverse()
for j in temp:
arr_final.append(j)
temp = []
arr_final.append(i)
if temp!=[]:
temp.reverse()
for j in temp:
arr_final.append(j)
temp = []
print(arr_final)