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

时间:2021-07-26 20:20:26

标签: java list null

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

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。

0 个答案:

没有答案