/* DList1.java */ /** * A DList1 is a mutable doubly-linked list. (No sentinel, not * circularly linked.) */ public class DList1 { /** * head references the first node. * tail references the last node. * * DO NOT CHANGE THE FOLLOWING FIELD DECLARATIONS. */ private DListNode1 head; private DListNode1 tail; /* DList1 invariants: * 1) For any DListNode x in a DList, if x.next == y, then y.prev == x. * 2) For any DListNode x in a DList, if x.prev == y, then y.next == x. */ /** * DList1() constructor for an empty DList1. */ public DList1() { head = null; tail = null; } /** * DList1() constructor for a one-node DList1. */ public DList1(int a) { head = new DListNode1(); tail = head; head.item = a; } /** * DList1() constructor for a two-node DList1. */ public DList1(int a, int b) { head = new DListNode1(); head.item = a; tail = new DListNode1(); tail.item = b; head.next = tail; tail.prev = head; } /** * removeFirst() removes the first node from DList1. If the list is empty, * do nothing. */ public void removeFirst() { // Your solution here. } /** * toString() returns a String representation of this DList. * * DO NOT CHANGE THIS METHOD. * * @return a String representation of this DList. */ public String toString() { String result = "[ "; DListNode1 current = head; while (current != null) { result = result + current.item + " "; current = current.next; } return result + "]"; } public static void main(String[] args) { // DO NOT CHANGE THE FOLLOWING CODE. DList1 l = new DList1(1, 2); System.out.println("List with 1 and 2 is " + l); l.removeFirst(); System.out.println("List with 2 is " + l); if (l.head.prev != null) { System.out.println("head.prev is wrong."); } l.removeFirst(); System.out.println("List with nothing is " + l); if (l.tail != null) { System.out.println("Tail reference is wrong."); } l.removeFirst(); System.out.println("List with nothing is " + l); if (l.tail != null) { System.out.println("Tail reference is wrong again."); } } }