删除链表中的第 k 个元素(Java 实现)



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

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

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:第四

看起来像是罪魁祸首。

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

您在链表中没有前进。

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

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

但是,如果您想打印整个链表,则不应使用从 removalKthNode 函数返回的值初始化结果。你应该从第一个开始。

希望这是有道理的。

代码中有几个问题:

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

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

a) 您不递增 j,因此您始终为项目放置相同的位置。

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

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

代码

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;
    }

试试这个: 可能会帮助你伴随任务:

    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);

}
}
循环

的简单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]);
        }           
    }
}
                

相关内容

  • 没有找到相关文章

最新更新