import java.util.Scanner;
public class DoubleList {
Node head;
public DoubleList(int input) {
this.head = new Node(1);
Node pre = head;
for(int i=2; i<=input; i++){
Node current = new Node(i);
pre.setNext(current);
current.setPre(pre);
pre = pre.getNext();
}
pre.setNext(head);
head.setPre(pre);
}
public void delete(int delete){
Node temp = head;
if(delete == 1){
if(head.getNext() == head){
head = null;
return;
}
head = head.getNext();
temp.getPre().setNext(head);
head.setPre(temp.getPre());
}
else{
for(int i=1; i<delete-1; i++){
temp = temp.getNext();
}
Node current = temp.getNext();
temp.setNext(current.getNext());
current.getNext().setPre(temp);
current = null;
}
}
public void print(){
if(head == null){
System.out.println("Empty");
return;
}
Node current = head;
while(current.getNext()!= head) {
System.out.print(current.getValue()+" ");
current = current.getNext();
}
System.out.println(current.getValue());
current = head.getNext();
while(current.getNext()!= head.getNext()) {
System.out.print(current.getValue()+" ");
current = current.getNext();
}
System.out.println(current.getValue());
current = head.getPre();
while(current.getNext()!= head.getPre()) {
System.out.print(current.getValue()+" ");
current = current.getNext();
}
System.out.println(current.getValue());
}
class Node {
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public Node getPre() {
return pre;
}
public void setPre(Node pre) {
this.pre = pre;
}
private int value;
private Node next;
private Node pre;
public Node(int val) {
this.value = val;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();
int delete = sc.nextInt();
//TODO check input
DoubleList dl = new DoubleList(input);
dl.delete(delete);
dl.print();
sc.close();
}
}
Java - DoubleList
最新推荐文章于 2023-02-03 16:44:11 发布