Doubly Linked List | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

Doubly Linked List

When I run the following code, it didn't pass all the test. Could anyone help me? I appreciate. import java.util.Iterator; import java.util.ConcurrentModificationException; interface List<T> extends Iterable<T> { void add(T x); // simple add T remove(int i); T get(int i); boolean contains(T x); int size(); default boolean isEmpty() { return size() == 0; } } public class DoublyLinkedList<T> implements List<T> { /** * Node is a pair containing a data field and pointers to * the previous and next nodes in the list. */ class Node { T data; Node next, prev; Node(T data) { this(data, null, null); } Node(T data, Node prev, Node next) { this.data = data; this.prev = prev; this.next = next; } } final Node head; // always points to the headnode for this list int n; // the number of nodes in this list, initially 0 /** * Creates the empty list. */ public DoublyLinkedList() { Node head = new Node(null, null, null); head.next = head; head.prev = head; this.head = head;//Save it to state } /** * Inserts the value x at the end of this list. */ public void add(T x) { n ++; Node last = head.prev; Node curr = new Node(x, last, head); last.next = curr; head.prev = curr; } public T remove(int i) { if (i < 0 || i >= size()) throw new IndexOutOfBoundsException(); if (head == null) return null; Node temp = head; if(i == 0){ head.next = temp.next.next; return temp.data; } for (int j = 0; temp!=null && j < i-1; j++) temp = temp.next; Node next = temp.next.next; temp.next =next; return next.data; } public T get(int i) { if (i < 0 || i >= size()) throw new IndexOutOfBoundsException(); Node node = head; for(int j = 0; j <i; j++)

2nd Feb 2018, 6:20 PM
Iris
Iris - avatar
0 ответов