2

我正在尝试这个程序,但我无法实现删除。执行进入无限循环。另外,我不确定我是否正确地形成了链表。

我在以下程序中缺少什么:

public class SpecificNodeRemoval {
private static class Node {
    String item;
    Node next;
    Node prev;

    private Node(String item, Node next, Node prev) {
        this.item = item;
        this.next = next;
        this.prev = prev;
    }
}

public static void main(String[] args) {
    int k = 3;

    Node fourth = new Node("Fourth", null, null);
    Node third = new Node("Third", fourth, null);
    Node second = new Node("Second", third, null);
    Node first = new Node("First", second, null);

    second.prev = first;
    third.prev = second;
    fourth.prev = third;
    Node list = first;

    Node result = removalKthNode(list, k);

    int j = 1;
    while(result.next!=null){
        System.out.println(j+": "+result.item);
    }
}

private static Node removalKthNode(Node first, int k) {
    Node temp = first;

    for(int i=1; i < k; i++) {
        temp = temp.next;
    }

    temp.prev.next = temp.next;
    temp.next.prev = temp.prev;

    return temp;
}
}

非常感谢您的回答和评论。工作程序如下所列:

public class SpecificNodeRemoval {
private static class Node {
    String item;
    Node next;
    Node prev;

    private Node(String item, Node next, Node prev) {
        this.item = item;
        this.next = next;
        this.prev = prev;
    }
}

public static void main(String[] args) {
    int k = 3;

    Node fourth = new Node("Fourth", null, null);
    Node third = new Node("Third", fourth, null);
    Node second = new Node("Second", third, null);
    Node first = new Node("First", second, null);

    second.prev = first;
    third.prev = second;
    fourth.prev = third;
    Node list = first;

    Node result = removalKthNode(list, k);

    int j = 1;
    while(result != null){
        System.out.println(j+": "+result.item);
        result = result.next;
        j++;
    }
}

private static Node removalKthNode(Node first, int k) {
    Node temp = first;

    for(int i=1; i < k; i++) {
        temp = temp.next;
    }

    temp.prev.next = temp.next;
    temp.next.prev = temp.prev;

    return first;
}
}

输出为: 1:第一 2:第二 3:第四

4

5 回答 5

4

这看起来像是罪魁祸首。

while(result.next!=null){
    System.out.println(j+": "+result.item);
}

您没有在链接列表中前进。

我不完全确定你的意图,但你可能想写如下以避免无限循环......

while(result !=null){
    System.out.println(j+": "+result.item);
    result = result.next;
    j++;
}

但是如果你想打印整个链表,你不应该用从removingKthNode函数返回的值初始化结果。你应该从first开始。

希望这是有道理的。

于 2013-10-31T06:37:27.040 回答
1

您的代码中有几个问题:

1) 该removalKthNode方法应返回列表中的第一个元素以使您的代码打印有意义的结果(或者您必须再次导航到第一个元素以输出剩余的列表。

2)while打印列表的循环在两个地方是错误的。

a) 你不增加 j,所以你总是为项目放置相同的位置。

b)您并没有真正遍历该列表,这意味着您没有重新分配您的变量result。尝试这样的事情:

 int j = 1;
 while (result != null) {
     System.out.println(j++ + ": " + result.item);
     result = result.next;
 }
于 2013-10-31T06:38:53.530 回答
0

编码

Node result = removalKthNode(list, k);
now result = Third 

并且您有 while 循环作为 while(result.next!=null) 始终在第三个元素中,因此它将进行无限循环。改变结果如下

 while(result!=null){
        System.out.println(j+": "+result.item);
        result = result.next;
    }
于 2013-10-31T06:39:26.450 回答
0

试试这个:可能会帮助你完成任务:

    package com.amazon;






class Linkedlist{

    Node head;

    public Linkedlist() {
        head = null;
    }


    public  Node addNode(int data){

        Node newNode = new Node(data);
        if(head==null) head = newNode;
        else{
            Node current = head;
            while(current.next!=null){
            current = current.next;
            }
            current.next = newNode;
        }
        return newNode;
    }

}

class Node
{
    Node next;
    int data;

    Node(int d)
    {
        data = d;
        next = null;
    }
}

public class DeleteEveryKthNodes {



    void modifyList(Node head,int k){

        Node current = head;
        Node previous = null;
        Node newHead = null;
        if(current==null)return;
        int count;
        while (current != null) {
            for (count = 1; count < k && current != null; count++) {
                previous = current;
                current = current.next; // 1--2--3--4--5
            }

            if (current != null) {
                Node temp = current;
                previous.next = current.next;
                // current = null;
                temp = null;
                current = current.next;
            }
        }

         current = head;
        while(current!=null){
            System.out.print(" "+current.data);
            current = current.next;
        }
    }

    public static void main(String args[]) {


    Linkedlist list = new Linkedlist();

    list.head = new Node(1);
    list.head.next = new Node(2);
    list.head.next.next = new Node(3);
    list.head.next.next.next = new Node(4);
    list.head.next.next.next.next = new Node(5);

    new DeleteEveryKthNodes().modifyList(list.head, 2);
    //list.head.next.next.next.next = new Node(1);



}
}
于 2016-09-28T02:03:40.820 回答
0

用于循环的简单 Java 脚本 - n=5 和 k=3,它将删除循环列表中的每 3 个元素。

public class TEST {
    
public static int killed_position(int[] newarr,int n,int a,int p) {
    int iteration=0;
    while(true) {
        if(newarr[p-1] != 0) {
            iteration++;
            if(iteration>a-1) {
                break;
            }
        }
        p++;
        if(p>n) {
            p=1;
        }
    }
    return p;
}
public static int next_position(int[] newarr,int n,int a,int p) {
    int iteration=0;
    while(iteration<1) {
        if(newarr[p-1] != 0) {
            iteration++;
        }
        else {
            p++;
            if(p>n) {
                p=1;
            }   
        }

    }
    System.out.println("NEXT START ->" + p);
    return p;
}
    public static void main(String[] args) {
        int n=5;
        int k=3;
    
        int newarr[] = new int[n];
        int a=1;
        
        for(int i=0;i<n;i++) {
            newarr[i]=i+1;
        }       
        for(int i=1;i<n;i++) {
            System.out.println("START -> " + a);
            a=killed_position(newarr, n,k,a);
            newarr[a-1]=0;
            System.out.println("KILLED -> " + a);   
            a=next_position(newarr, n,k,a);
            System.out.println("---------------------");
                
        }
        System.out.println("POSITION FINAL MAN  -> " + a);
        System.out.println("POSITION FINAL MAN NAME -> " + a);
        for(int i=0;i<n;i++) {
            System.out.print(newarr[i]);
        }           
    }
}
                
于 2020-12-19T13:25:41.777 回答