Kind of confuse with this assignment, can anyone help Please! This is in java. W
ID: 3540370 • Letter: K
Question
Kind of confuse with this assignment, can anyone help Please!
This is in java.
Write the following methods on the LinkedList class. The methods should not call any other methods in your LinkedList class(that is, for example, if you need to add a node to the linked list, don't call insert method; instead, do the insertion inside the methods you will write).
/**Adds the elements in otherlist to this list.
* Returns true if this list changed as a result of the call */
public Boolean addAll(MyList otherList)
/** Removes all the elments in otherList from this list
* Returns true if this list changed as a result of the call */
public Boolean removeAll(MyList otherList)
/** Retains the elements in this list that are also in otherList
* Returns true if this list changed as a result of the call */
public Boolean retainAll(myList otherList)
Ok this is how I would do it if i could call other method, but I am not sure how to do it without clling for another method
public boolean addAll(MyList otherList) {
if (otherList != null) {
addAll(otherList);
return true;
} else
return false;
}
public boolean removeAll(MyList otherList) {
if (otherList != null) {
removeAll(otherList);
return true;
} else
return false;
}
public boolean retainAll(MyList otherList) {
if (otherList != null) {
retainAll(otherList);
return true;
} else
return false;
}
Thanks
Explanation / Answer
To build a successful linked list, we must first have a working implementation of a Node:
As your code combined the two class definitions in a single file, the the can be found below:
As your code combined the two class definitions in a single file, the the can be found below:
import java.lang.*; public class SinglyLinkeList { Node start; public SinnglyLinkedList() { this.start=null; } public void addFront(Object newData) { Node cache = this.start; //store a reference to the current start node this.start = new Node(newData,cache); //assign our start to a new node that has newData and points to our old start } public addRear(Object newData) { Node cache = start; Node current = null; while((current = cache.next) != null) //find the last Node cache = cache.next; cache.next = new Node(newData,null); //create a new node that has newData and points to null } public Object getFront() { return this.start.data; // return the front object's data } Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.