Question: complete class SinglyLinkedList, specifically, its incomplete method given below. And you may, during testing, comment out portion of code in classSingllyLinkedListTest and add more code
complete class SinglyLinkedList, specifically, its incomplete method given below. And you may, during testing, comment out portion of code in classSingllyLinkedListTest and add more code in testing the other well-defined, given, methods.
Method reverse(): reverse the linked list.
(Note: you may not use any JCF defined/ready data structures for this project. Use only basic Java features such given in the sample code.) CODE:
public class SinglyLinkedList { public static class Node{ private E element; private Node next; public Node(E e, Node n){ element = e; next = n; } public E getElement(){return element;} public Node getNext() {return next;} public void setNext(Node n){next = n;} }
private Node head = null, tail = null; private int size = 0; public SinglyLinkedList() {} public int size() {return size; } public boolean isEmpty() { return size == 0;} public Node getHead() {return head;} public Node getTail() {return tail;} public E first() { if (isEmpty()) return null; return head.getElement(); } public E last() { if (isEmpty()) return null; return tail.getElement(); } public void addFirst(E e){ head = new Node<>(e, head); if (size == 0) tail = head; size++ ; } public void addLast(E e) { Node newest = new Node<>(e, null); if (isEmpty()) head = newest; else tail.setNext(newest); tail = newest; size++; } public E removeFirst() { if (isEmpty()) return null; E answer = head.getElement(); head = head.getNext(); size--; if (size == 0) tail = null; return answer; }
public String toString() { StringBuilder sb = new StringBuilder("["); Node walk = head; while (walk != null) { sb.append(walk.getElement()); if (walk != tail) sb.append(", "); walk = walk.getNext(); } sb.append("]"); return sb.toString(); } //NEW-Solution public void reverse( ) { System.out.println("To be done!"); }
} 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 SingllyLinkedListTest{ public static void main(String[] args) { SinglyLinkedList listGE1,listGE2;// String[] names = {"A", "B", "C", "D", "E", "F", "G", "H", "B", "D", "B"}; int[] scores = {7, 1, 5, 4, 8, 6, 2, 9, 1, 4, 1}; GameEntry gE; //Practice!! //Delete addFirst() and then rewrite addFirst() and test it System.out.println("Testing addFirst() method:"); listGE1 = new SinglyLinkedList (); for (int i=0; i < 3; i++) { gE = new GameEntry(names[i], scores[i]); listGE1.addFirst(gE); } System.out.println("LG1: after adding 3 entries: " + listGE1); //Practice!! //Delete removeFirst() and then rewrite removeFirst() and test it System.out.println(" Testing removeFirst() method:"); System.out.println("Starting LG1: " + listGE1); for (int i=0; i < 3; i++) { listGE1.removeFirst(); System.out.println("LG1- after the first removed: " + listGE1); } //Practice!! //Delete addLast() and then rewrite addLast() and test it System.out.println(" Testing addLast() method on a new linked list:"); listGE2 = new SinglyLinkedList (); for (int i=0; i < 3; i++) { gE = new GameEntry(names[i], scores[i]); listGE2.addLast(gE); } System.out.println("LG2: after adding 3 entries: " + listGE2);
//Practice MORE MORE... // on testing isEmpty(), get*methods. //NOW--following--LAB tasks!!! //Working on reverse() and then test it!! listGE2.reverse(); System.out.println(" LG2-reversed: " + listGE2); } }