Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CODE: public class DoublyLinkedList { private static class Node { private E element; private Node prev; private Node next; public Node(E e, Node p, Node

image text in transcribed 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DATABASE Administrator Make A Difference

Authors: Mohciine Elmourabit

1st Edition

B0CGM7XG75, 978-1722657802

More Books

Students also viewed these Databases questions

Question

6. Describe to a manager the different types of distance learning.

Answered: 1 week ago

Question

1. Explain how new technologies are influencing training.

Answered: 1 week ago