如何删除循环双链接列表中的节点?

5sxhfpxr  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(195)

我正在尝试编写一个程序,它将在一个循环双链接列表中前进一定的次数,并在同一个列表中后退一定的次数。如果两种方法都以相同的数字结束,则该数字为“值得”,并从列表中删除。如果这些方法不在列表中的同一个元素上结束,那么它们就没有价值,也会从列表中删除。我已经多次编写了前进和后退的方法,但是一旦元素被认为是有价值的或不值得的,我就很难删除它们。这就是我目前所拥有的。任何帮助都将不胜感激。

import java.util.Arrays;
public class LinkedList {

private Node head;
private Node end;

LinkedList(){
    head = end = null;

}

public void addAtStart(int x){

    if (head == null) {  
        Node new_node = new Node(x);  
        new_node.data = x;  
        new_node.next = new_node.prev = new_node;  
        head = new_node;   
    } else if (head != null) {
        Node last = (head).prev;
        Node new_node = new Node(x);  
        new_node.data = x;
        new_node.next = head;
        (head).prev = new_node; 
        new_node.prev = last;
        last.next = new_node;
    }
}

public void printOutput(int N, int k, int m){
    System.out.println("Output" + "\n" + "------" + "\n");
    printCandidates(N,k,m);

}

public void printCandidates(int N, int k, int m){
    int unworthy[] = new int[N];
    int worthy[] = new int[N];
    int count = 0;
    int run = 0;
    Node temp = head;
    do {
        if (forwards(k) == backwards(m)){ // puts in worthy list and deletes from linked list
            worthy[count] = forwards(k);
            count += 1;
            System.out.println("hello");
            deleteElement(forwards(k));
        } else if (forwards(k) != backwards(m)){ //put in unworthy list and delete from linked list
            unworthy[run] = forwards(k);
            unworthy[run+1] = backwards(m);
            run += 2;
            System.out.println("goodbye");
            deleteElement(forwards(k));
            deleteElement(backwards(m)); 
        }
    } while (temp != null);
    System.out.println("Removed candidates from being elected");
    System.out.println(Arrays.toString(unworthy));
    System.out.println("Worthy candidates");
    System.out.println(Arrays.toString(worthy));
}

int forwards(int k){
    int run = 0;
    int x = 0;
    Node temp = head;
    while (temp.next != head){  
        if(run == (k)){
            x = temp.data;
        }
        temp = temp.next;
        run += 1;
    }
    return x;
}

int backwards(int m){
    int run = 0;
    int x = 0;
    Node temp = head;
    Node last = head.prev;  
    temp = last; 
    while (temp.next != head){  
        if(run == (m)){
            x = temp.data;
        } 
        temp = temp.next;
        run += 1;
    } 
    return x;
}

public void deleteElement(int elementToBeDeleted){
    Node temp = head;
    while (temp.next != head){  
        if(temp.data == elementToBeDeleted){
            temp.setNext(temp.next);
        }
        temp = temp.next;
    }  
}

这是我的司机:

public class Program2 {
public static void main(String[] args) {
    LinkedList ll = new LinkedList();
    for (int i = 1; i < 11; i++){
        ll.addAtStart(i);
    }
    int N = 10;
    int k = 4;
    int m = 3;

    System.out.println("N = " + N + ", " + "k = " + k + ", " + "m = " + m + "\n"); 
    ll.printOutput(N,k,m);
}

}

这是我的节点类:

public class Node {
public int data;
public Node next;
public Node prev;

// Constructor to intialize/fill data
public Node(int data){ 
    this.data = data;
}

 // set the address of next node
public void setNext(Node temp) {
    this.next = temp;
}

// get the address of next node
public Node getNext(){
    return this.next;
}

public Node getPrev(){
    return this.prev;
}
public void setPrev(Node temp) {
    this.prev = temp;
}

// to get data of current node
public int getData(){
    return this.data;
}
}

编辑:作为这个练习的一部分,我需要自己编写这个类,因此我需要实现自己的linkedlist。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题