CODE:
public class DoublyLinkedList { private static class Node { private E element; private Node prev; private Node next; public Node(E e, Node p, Node n) { element = e; prev = p; next = n; } public E getElement() { return element; } public Node getPrev() { return prev; } public Node getNext() { return next; } public void setPrev(Node p) { prev = p; } public void setNext(Node n) { next = n; } } private Node header, trailer; private int size = 0; public DoublyLinkedList() { header = new Node(null, null, null); trailer = new Node(null, header, null); header.setNext(trailer); }
public int size() { return size; }
public boolean isEmpty() { return size == 0; }
public E first() { if (isEmpty()) return null; return header.getNext().getElement(); }
public E last() { if (isEmpty()) return null; return trailer.getPrev().getElement(); }
public void addFirst(E e) { addBetween(e, header, header.getNext()); }
public void addLast(E e) { addBetween(e, trailer.getPrev(), trailer); }
public E removeFirst() { if (isEmpty()) return null; return remove(header.getNext()); }
public E removeLast() { if (isEmpty()) return null; return remove(trailer.getPrev()); } private void addBetween(E e, Node predecessor, Node successor) { Node newest = new Node(e, predecessor, successor); predecessor.setNext(newest); successor.setPrev(newest); size++; }
private E remove(Node node) { Node predecessor = node.getPrev(); Node successor = node.getNext(); predecessor.setNext(successor); successor.setPrev(predecessor); size--; return node.getElement(); }
public String toString() { StringBuilder sb = new StringBuilder("("); Node walk = header.getNext(); while (walk != trailer) { sb.append(walk.getElement()); walk = walk.getNext(); if (walk != trailer) sb.append(", "); } sb.append(")"); return sb.toString(); } //To Be Completed!!!!!!!!!!!!!!!!!!!!!!!!!!!! public void method4test() { } } public class GameEntry { private String name; private int score; public GameEntry(String n, int s) { name = n; score = s; } public String getName() { return name; } public int getScore() { return score; } public String toString() { return "(" + name + ", " + score + ")"; } }
import java.util.Scanner;
public class DoublyLinkedListTest{ public static void main(String[] args) { DoublyLinkedList []listGE = new DoublyLinkedList [4]; String[] names = {"A", "B", "C", "D", "E", "F", "G", "H"}; int[] scores = {7, 1, 5, 4, 8, 6, 2, 9}; GameEntry [] gE = new GameEntry[8]; for (int i = 0; i (); //Testing addlast()... for (int i = 0; i For this lab, the following classes are given for this test (with what you need to do): GameEntry: the same as in the textbook/assignments/projects. DoublyLinkedList: you are to reimplement the following methods without using method addBetween: addFirst o addLast (before working on the reimplementation, you would need to understand, or better, reimplement, the methods addBetween and remove!!). Many of the methods in the class use method addBetween-practice of reimplementing those methods without using method addBetween! DoublyLinkedListTest: this is a incomplete class, you would add more testing code like the one in this class, to test the following methods given in class DoublyLinkedList: o addFirst o removeFirst o remove Last Design and implement the test class (as suggested in the test class comments) so as to fully test the methods correctly. For this lab, the following classes are given for this test (with what you need to do): GameEntry: the same as in the textbook/assignments/projects. DoublyLinkedList: you are to reimplement the following methods without using method addBetween: addFirst o addLast (before working on the reimplementation, you would need to understand, or better, reimplement, the methods addBetween and remove!!). Many of the methods in the class use method addBetween-practice of reimplementing those methods without using method addBetween! DoublyLinkedListTest: this is a incomplete class, you would add more testing code like the one in this class, to test the following methods given in class DoublyLinkedList: o addFirst o removeFirst o remove Last Design and implement the test class (as suggested in the test class comments) so as to fully test the methods correctly