Question: Three Java classes are given. You are to complete class SinglyLinkedList, specifically, its three incomplete methods defined below. You may not change other two classes,

Three Java classes are given. You are to complete class SinglyLinkedList, specifically, its three incomplete methods defined below. You may not change other two classes, though you may, during testing your code, temporarily comment out portion of code in class SingllyLinkedListTest.

Method addAt(E e, int i): add on the singly-linked list an entry e at the index i;

Method remove(int i): remove on the singly-linked list an entry e at the index i;

Method removeEvens(): remove on the singly-linked list all the entries at the even-indices (that is, the entries at 0, 2, 4, ).

(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.)

After you have correctly completed the methods, the outputs from the testing should look like the following: Three Java classes are given. You are to complete class SinglyLinkedList, specifically,

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 addAt(E e, int i) { System.out.println("To be done!"); } //NEW!--Solution: remove the i-th node // NOTE: when i = 0--the very first element public E remove(int i) { System.out.println("To be done!"); return null; //should be updated for this statement }

//PA---- public void removeEvens() { 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 (); for (int i=0; i

//Working on addAt(E e, int i) method: add and entry e at the index i System.out.println(" Now testing addAt() method on LG2:."); gE = new GameEntry(names[8], scores[8]); System.out.println("LG2: adding an entry at index-5 to " + listGE2); listGE2.addAt(gE, 5); System.out.println("LG2: adding an entry at index-0 to " + listGE2); listGE2.addAt(gE, 0); System.out.println("Now the list: " + listGE2); System.out.println("LG2: adding an entry at index-2 to " + listGE2); gE = new GameEntry(names[7], scores[7]); listGE2.addAt(gE, 2); System.out.println("Now the list: " + listGE2); System.out.println("LG2: adding an entry at index-5 to " + listGE2); gE = new GameEntry(names[9], scores[9]); listGE2.addAt(gE, 5); System.out.println("Now the list: " + listGE2); //Working on remove(int i): remove at the index i System.out.println(" Now testing remove() method on LG2."); System.out.println("LG2: remove entry at index-0 from " + listGE2); listGE2.remove(0); System.out.println("Now the list: " + listGE2); System.out.println("LG2: remove entry at index-9 from " + listGE2); listGE2.remove(9); //System.out.println("Now the list: " + listGE2); System.out.println("LG2: remove entry at index-4 from " + listGE2); listGE2.remove(4); System.out.println("Now the list: " + listGE2); listGE2.remove(0); listGE2.remove(0); listGE2.remove(0); listGE2.remove(0); System.out.println("LG2: removing 4 entries at index-0: " + listGE2); listGE2.remove(0); System.out.println("LG2: removing an entry at index-0: " + listGE2);

//Working on removeEvens()... System.out.println(" Testing on removeEvens(), starting with LG1: " + listGE1); listGE1 = new SinglyLinkedList (); for (int i=0; i Testing addFirst() method: LG1: after adding 3 entries: [(C, 5), (B, 1), (A, 7)] Testing removeFirst() method: Starting LG1: [(C, 5), (B, 1), (A, 7)] LG1- after the first removed: [(B, 1), (A, 7)] LG1- after the first removed: [(A, 7)] LG1- after the first removed: [] Testing addLast() method on a new linked list: yi_mod3_exit after adding 3 entries: [(A, 7), (B, 1), (C, 5)] LG2:-reversed: [(C, 5), (B, 1), (A, 7)] Now testing addAt() method on LG2:. LG2: adding an entry at index-5 to [(C, 5), (B, 1), (A, 7)] Cannot add--index out of bound !! LG2: adding an entry at index-0 to [(C, 5), (B, 1), (A, 7)] Now the list: [(B, 1), (C, 5), (B, 1), (A, 7)] LG2: adding an entry at index-2 to [(B, 1), (C, 5), (B, 1), (A, 7)] Now the list: [(B, 1), (C, 5), (H, 9), (B, 1), (A, 7)] LG2: adding an entry at index-5 to [(B, 1), (C, 5), (H, 9), (B, 1), (A, 7)] Now the list: [(B, 1), (C, 5), (H, 9), (B, 1), (A, 7), (D, 4)] Now testing remove() method on LG2. LG2: remove entry at index- from [(B, 1), (C, 5), (H, 9), (B, 1), (A, 7), (D, 4)] Now the list: [(C, 5), (H, 9), (B, 1), (A, 7), (D, 4)] LG2: remove entry at index-9 from [(C, 5), (H, 9), (B, 1), (A, 7), (D, 4)] !!! No entry at this index!! LG2: remove entry at index-4 from [(C, 5), (H, 9), (B, 1), (A, 7), (D, 4)] Now the list: [ic, 5), (H, 9), (B, 1), (A, 7)] LG2: removing 4 entries at index-o: [] !!! No entry at this index!! LG2: removing an entry at index-o: [] Testing on removeEvens(), starting with LG1: ( LG1: after adding 9 entries: [(B, 1), (H, 9), (G, 2), (F, 6), (E, 8), (D, 4), (C, 5), (B, 1), (A, 7)] LG1: after removing entries at even-indices: [(H, 9), (F, 6), (D, 4), (B, 1)] LG1: after removing entries at even-indices again: [(F, 6), (B, 1)] LG1: after removing entries at even-indices again: [(B, 1)] LG1: after removing entries at even-indices again: []

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!