Question: Can I get help with the bonus question? I'm having a hard time. The instructions are listed below. Thanks in advance! * Grading: * Complete
Can I get help with the bonus question? I'm having a hard time. The instructions are listed below. Thanks in advance!
* Grading:
* Complete work inside of B1DoubleLL.java
* Complete the merge(B1DoubleLL
* Modify the betterInsert(E val, int index) method - 1pt
* Modify the betterDelete(int index) method - 1pt
* Modify the betterGet(int index) method - 1pt
* Implement opCounts across class - 2pts
B1Diver.java
public class B1Driver {
public static void main(String[] args) {
B1DoubleLL
list1.add(1);
list1.add(2);
list1.add(3);
list1.add(4);
B1DoubleLL
list2.add(5);
list2.add(6);
list2.add(7);
list2.add(8);
B1DoubleLL
list3.add(9);
list3.add(10);
list3.add(11);
list3.add(12);
list1.merge(list2);
System.out.println("List 1:"+(list1.printList().equals("1,2,3,4,5,6,7,8,")));
System.out.println("List 1 Rev:"+(list1.printListRev().equals("8,7,6,5,4,3,2,1,")));
System.out.println("List 2:"+(list2.printList().equals("5,6,7,8,")));
list1.merge(list3);
System.out.println("List 1:"+(list1.printList().equals("1,2,3,4,5,6,7,8,9,10,11,12,")));
System.out.println("List 1 Rev:"+(list1.printListRev().equals("12,11,10,9,8,7,6,5,4,3,2,1,")));
System.out.println("List 3:"+(list3.printList().equals("9,10,11,12,")));
list1.resetOpCount();//clear counts before work
list1.insert(0, 9);
System.out.println("Insert OpCount:"+list1.getOpCount());
list1.delete(9);//remove zero to make list back to the same size
list1.resetOpCount();//clear counts before work
list1.betterInsert(0, 9);
System.out.println("Better Insert OpCount:"+list1.getOpCount());
//you should test all of your better methods
}
}
/*
* Complete the merge(B1DoubleLL
* Modify the betterInsert(E val, int index) method
* Modify the betterDelete(int index) method
* Modify the betterGet(int index) method
* Implement opCounts across class
*/
/*
* Grading:
* Implement OpCounts across all methods
* -Note: Variable already exists, as does the getter and reset methods
*/
B1DoubleLL.java
public class B1DoubleLL
/*
* Grading:
* Merge lists in O(1) - 2pt
*/
public void merge(B1DoubleLL
//merge the list passed as a parameter into the existing list in O(1)
}
/*
* Grading:
* Make betterInsert run better by taking advantage of when an index is closer to the end - 1pt
*/
public void betterInsert(E val, int index) {
if(index < 0) {//fix invalid index
index = 0;
}
if(index >= count) {//goes in last position
this.add(val);
} else {
Node newItem = new Node(val);
if(index == 0) {//goes in first position
newItem.next = start;
start.prev = newItem;
start = newItem;
} else {//goes in middle
Node current = start;
for(int i = 1; i < index; i++) {
current = current.next;
}
newItem.next = current.next;
newItem.prev = current;
current.next.prev = newItem;
current.next = newItem;
}
count++;
}
}
/*
* Grading:
* Make betterDelete run better by taking advantage of when an index is closer to the end - 1pt
*/
public void betterDelete(int index) {
if(index >= 0 && index < count) {//valid index
if(index == 0) {//remove first
start = start.next;
if(start != null) {//as long as there was an item next in list
start.prev = null;
} else {//if only item was removed
end = null;
}
} else if(index == count-1) {//remove last item
end = end.prev;
end.next = null;
} else {//remove middle item
Node current = start;
for(int i = 1; i < index; i++) {
current = current.next;
}
current.next = current.next.next;
current.next.prev = current;
}
count--;
}
}
/*
* Grading:
* Make betterGet run better by taking advantage of when an index is closer to the end - 1pt
*/
public E betterGet(int index) {
if(index >= 0 && index < count) {//valid index
Node current = start;
for(int i = 0; i < index; i++) {
current = current.next;
}
return current.value;
}
return null;
}
private Node start, end;
private int count;
private long opCount = 0;
public B1DoubleLL() {
start = end = null;
count = 0;
}
public long getOpCount()
{
return opCount;
}
public void resetOpCount()
{
opCount = 0;
}
public String printList() {
String output = "";
Node current = start;
while(current != null) {
output += current.value + ",";
current = current.next;
}
return output;
}
public String printListRev() {
String output = "";
Node current = end;
while(current != null) {
output += current.value + ",";
current = current.prev;
}
return output;
}
public void add(E val) {
Node newItem = new Node(val);
if(start == null) {
start = newItem;
end = start;
count = 1;
} else {
end.next = newItem;
newItem.prev = end;
end = newItem;
count++;
}
}
public void insert(E val, int index) {
if(index < 0) {//fix invalid index
index = 0;
}
if(index >= count) {//goes in last position
this.add(val);
} else {
Node newItem = new Node(val);
if(index == 0) {//goes in first position
newItem.next = start;
start.prev = newItem;
start = newItem;
} else {//goes in middle
Node current = start;
for(int i = 1; i < index; i++) {
current = current.next;
}
newItem.next = current.next;
newItem.prev = current;
current.next.prev = newItem;
current.next = newItem;
}
count++;
}
}
public void delete(int index) {
if(index >= 0 && index < count) {//valid index
if(index == 0) {//remove first
start = start.next;
if(start != null) {//as long as there was an item next in list
start.prev = null;
} else {//if only item was removed
end = null;
}
} else if(index == count-1) {//remove last item
end = end.prev;
end.next = null;
} else {//remove middle item
Node current = start;
for(int i = 1; i < index; i++) {
current = current.next;
}
current.next = current.next.next;
current.next.prev = current;
}
count--;
}
}
public E get(int index) {
if(index >= 0 && index < count) {//valid index
Node current = start;
for(int i = 0; i < index; i++) {
current = current.next;
}
return current.value;
}
return null;
}
public int size()
{
return count;
}
public String toString() {
return this.printList();
}
private class Node {
E value;
Node next, prev;
public Node(E v) {
value = v;
next = prev = null;
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
