My main question is, what is the process map(what each part is doing) of the fol
ID: 3664252 • Letter: M
Question
My main question is, what is the process map(what each part is doing) of the following code? What I'm really trying to get answered is the sub-parts of the question though. The sub questions are listed as comments above or next to the code that is most confusing. Please explain in a very simple manner (because I have no idea what's going on where I left comments), if it seems something too annoying to accomplish, I ask you to pass this question to someone else that would not mind answering it. Thanks
package linkedList;
public class Link {
public String bookName;
public int millionsSold;
public Link next; // What is the reason/mechanism of creating this instance?
public Link(String bookName, int millionsSold){
this.bookName = bookName;
this.millionsSold = millionsSold;
}
public void display(){
System.out.println(bookName + ": " + millionsSold + " ,000,000");
}
// What is the point of toString if there's already a display method?
public String toString(){
return bookName;
}
public static void main(String args[]){
LinkList theLinkedList = new LinkList();
theLinkedList.insertFirstLink("Dont Quixote", 500);
theLinkedList.insertFirstLink("Harry Potter volume", 100);
theLinkedList.insertFirstLink("The Lord of the rings", 150);
theLinkedList.insertFirstLink("A Tale of Two Cities", 200);
theLinkedList.display();
}
}
///////////////////////////////////end of class link // beginning of linkedlist
class LinkList{
public Link firstLink;
LinkList(){
firstLink = null;
}
public boolean isEmpty(){
return(firstLink == null);
}
public void insertFirstLink(String bookName, int millionsSold){
Link newLink = new Link(bookName, millionsSold);
// What is happening here when .next is invoked? What is newLink.next = firstLink; doing?
newLink.next = firstLink;
firstLink =newLink;
}
public Link removeFirst(){
Link linkReference = firstLink;
if(!isEmpty()){
// What is happening here when .next is invoked?
firstLink = firstLink.next;
}
else{
System.out.println("Empty LinkedList");
}
return linkReference;
}
public void display(){
Link theLink = firstLink;
while(theLink != null){
theLink.display();
System.out.println("Next link: " + theLink.next);
// What is happening here when .next is invoked?
theLink = theLink.next;
System.out.println();
}
}
public Link find(String bookName){
Link theLink = firstLink;
if(!isEmpty()){
// What is happening here when .next is invoked?
while(theLink.bookName != bookName){
if(theLink.next == null){
return null;
} // end if
else{
theLink = theLink.next;
} // end else
} //end while
} // if
else{
System.out.println("Empty LinkedList");
} //end else
return theLink;
}
public Link removeLink(String bookName){
Link currentLink = firstLink;
Link previousLink = firstLink;
while(currentLink.bookName != bookName){
if(currentLink.next == null){
return null;
}
else{
previousLink = currentLink;
currentLink = currentLink.next;
}
}
if(currentLink == firstLink){
firstLink = firstLink.next;
}
else{
// Why are there two .next here?
previousLink.next = currentLink.next;
}
return currentLink;
}
}
Explanation / Answer
The process main part is dealing in LinkList Class methods.
1st question: what is reason of creating this instance?
Answer: Linked list is datas structure where elements are linked to each other. To access The
data .. we must go to next element. So to get next element we will use this next instance.
--------------------------------------------------------------------------------------
What is the point of toString if there is already a display method?
Answer: toString() method is used to display instance data .. It will pack all data variables
into string format to display in console. Where in display we have to explicitly cast to string
and have to display in console.
---------------------------------------------------------------------------------------
What is happening here when .next is invoked? What is newLink.next = firstLink; doing?
Answer: When .next is invoked then pointer is moved to point out next element.
newLink.next = firstLink .. means setting head node to next current node.
--------------------------------------------------------------------------------------------
What is happening here when .next is invoked?
Answer: When .next is invoked then pointer is moved to point out next element.
--------------------------------------------------------------------------------------
public Link find(String bookName){
Link theLink = firstLink;
if(!isEmpty()){
// What is happening here when .next is invoked?
//answer: .next it will process all elements in list .. by moving pointer to next
while(theLink.bookName != bookName){
if(theLink.next == null){
return null;
} // end if
------------------------------------------------------------------------------------
// Why are there two .next here?
previousLink.next = currentLink.next;
Answer: It is setting next element to current position.
previousLink.next gives current position
and currentLink.next means current position next element.
So these .next are to save data ..all elements one step backward.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.