Debug This Java code. /* Purpose: * Adds a new method to LinkedList that removes
ID: 638577 • Letter: D
Question
Debug This Java code.
/* Purpose:
* Adds a new method to LinkedList that removes the center element(s).
*/
public class NewLinkedList implements LinkedList {
// removes the center of the list
public void removeCenter(LinkedList list){
int half;
if(list.size() > 2){ // dont do anything if the size is less than three
half = list.size() / 2;
if(list.size() % 2 == 0){ //remove two in center if even
list.remove(half - 1);
list.remove(half);
}else{
list.remove(half - 1); // remove the center if odd
}
}
}
public static void main(String[] args){
LinkedList nll = new LinkedList();
nll.add("aaa");
nll.add("bbb");
nll.add("ccc");
nll.add("ddd");
nll.add("eee");
nll.add("fff");
nll.add("ggg");
nll.add("hhh");
nll.add("iii");
nll.add("jjj");
nll.add("kkk");
System.out.println("before removeCenter(): " + nll);
nll.removeCenter(nll);
System.out.println("after removeCenter(): " + nll);
}
}
Explanation / Answer
We have a list looking like: ... -> Node(i-1) -> Node(i) -> Node(i+1) -> ... and we need to delete Node(i).
Pseudocode:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.