Modify the class LinkedList given in the lecture by adding to it the functions l
ID: 3628575 • Letter: M
Question
Modify the class LinkedList given in the lecture by adding to it the functions listed below for Exercise 2. In each case, the appropriate error message should be generated if an invalid condition occurs.
a. toString(): modify the display function to overload the toString function of the Object class.
b. int length(): create this function to determine the number of items in the list (accessor function).
c. void clear(): create this function to remove all of the items from the list. After this operation is completed, the length of the list is zero.
d. void insertEnd(int item): create this function to insert item at the end of the list.
e. void replace(int location, int item): create this function to replace the item in the list at the position specified by location. The item should be replaced with item.
f. int get(int location): create a function that returns the element at the position location
Here is the basic linked list given in the lecture:
public class LinkedList
{
private Node first;
public LinkedList(){...}
public boolean isEmpty(){...}
public void display(){...}
public boolean search(int x){...}
public void insert(int i){...}
public void remove(int x){...}
}
Explanation / Answer
Just a quick overview, we use the get method that we create and the length method that we create for the majority of these methods. Make sure you understand those examples first. Make sure you also delete my comments and add your own. Your instructor may require slightly different implementation (you did not supply the Node class, so I don't know how your attribute references are labeled, I assume your Node has a next and data attribute. It appears that your data should be of type int, and next will always be of type Node.) I hope this helps, it should compile correctly. Good luck! //this first set of code overloads the toString of the object class, you should call // it from display() as instructed @overload public String toString(){ //you probably want to display each of the items in the list, this is done by // walking the list if(first.next==null){ // display error message as directed in instructions System.err.println("The LinkedList is empty"); Node temp = first.next; String listOfObjects = (""+first.data.toString()); // calls the node's object's // toString while(temp!=null){ //we need to add the toString of the object held by data to the original // string listOfObjects = (listOfObjects+temp.data.toString()); temp=temp.next; // walking the list until next is null }//this walked the list and added to the string as it went return listOfObjects; }//end toString //display it how you are instructed to, I am just printing it to console here public void display(){ System.out.println(this.toString()); //calls the toString we just overloaded } //In order to get the length, we must use "walk the list" public int length(){ // if the list is empty we want to return a size of 0 if(first==null) return 0; // if the list is not empty, we want to "walk the list" Node temp = first.next; //Grabs the next node after temp int count = 1;//count is the size of the list, its 1 to start because first exists //while temp still has another unit while(temp!=null){ //temp will equal null when the linked list is //finished walking since the last node references null count+=1; //we want to increment the count as we find more objects temp = temp.next; //temp now equals the next node and the loop continues //until temp is null (which happens at end of list) } return count; }//end length // I am now doing the get method (part f) so you can use it in other methods public int get(int location){ //its an error if the location is less than length, returns -1 if(locationRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.