SlideShare a Scribd company logo
1 of 2
Download to read offline
I need help understanding how this linked list set method works in java,
* Set value stored at location i to object o, returning old value.
*
* @pre 0 <= i < size()
* @post sets ith entry of list to value o, returns old value
* @param i location of entry to be changed.
* @param o new value
* @return former value of ith entry of list.
*/
public E set(int i, E o)
{
if (i >= size()) return null;
Node finger = head;
// search for ith element or end of list
while (i > 0)
{
finger = finger.next();
i--;
}
// get old value, update new value
E result = finger.value();
finger.setValue(o);
return result;
Solution
Hi friend,
Please go through comment, you can uderstand logic of set method.
public E set(int i, E o)
{
// if given position number (i) is greater than current size of list,
// then you can not set, because this is not valid position, so return null
if (i >= size()) return null;
Node finger = head; // initializing finger with head node of list
// search for ith element or end of list
while (i > 0) // traverse until we not hit ith node in list
{
finger = finger.next();
i--;
}
// now after above while loop, finger points ith node in list
// get old value, update new value
E result = finger.value(); // getting old value of ith node
finger.setValue(o); // setting new value in ith node
return result;
}

More Related Content

Similar to I need help understanding how this linked list set method works in j.pdf

Javai have to make a method that takes a linked list and then retu.pdf
Javai have to make a method that takes a linked list and then retu.pdfJavai have to make a method that takes a linked list and then retu.pdf
Javai have to make a method that takes a linked list and then retu.pdf
stopgolook
 
hi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfhi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdf
archgeetsenterprises
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdf
ravikapoorindia
 
Hi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfHi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdf
annaelctronics
 
Please write this in java using a GUIImplement a class cal.pdf
Please write this in java using a GUIImplement a class cal.pdfPlease write this in java using a GUIImplement a class cal.pdf
Please write this in java using a GUIImplement a class cal.pdf
aniarihant
 
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdfAssignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
Footageetoffe16
 
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
  import java.util.Iterator; import java.util.NoSuchElementException; .pdf  import java.util.Iterator; import java.util.NoSuchElementException; .pdf
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
deepakangel
 
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdfimport java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
Stewart29UReesa
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
rohit219406
 
The hashtable youll be making will use Strings as the keys and Obje.pdf
The hashtable youll be making will use Strings as the keys and Obje.pdfThe hashtable youll be making will use Strings as the keys and Obje.pdf
The hashtable youll be making will use Strings as the keys and Obje.pdf
vicky309441
 
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
arihantelehyb
 
4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf
mumnesh
 
Note- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdfNote- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdf
Stewart29UReesa
 

Similar to I need help understanding how this linked list set method works in j.pdf (20)

Javai have to make a method that takes a linked list and then retu.pdf
Javai have to make a method that takes a linked list and then retu.pdfJavai have to make a method that takes a linked list and then retu.pdf
Javai have to make a method that takes a linked list and then retu.pdf
 
hi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfhi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdf
 
dynamicList.ppt
dynamicList.pptdynamicList.ppt
dynamicList.ppt
 
Chap 2 Arrays and Structures.ppt
Chap 2  Arrays and Structures.pptChap 2  Arrays and Structures.ppt
Chap 2 Arrays and Structures.ppt
 
Chap 2 Arrays and Structures.pptx
Chap 2  Arrays and Structures.pptxChap 2  Arrays and Structures.pptx
Chap 2 Arrays and Structures.pptx
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdf
 
Hi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfHi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdf
 
Please write this in java using a GUIImplement a class cal.pdf
Please write this in java using a GUIImplement a class cal.pdfPlease write this in java using a GUIImplement a class cal.pdf
Please write this in java using a GUIImplement a class cal.pdf
 
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdfAssignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
 
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
  import java.util.Iterator; import java.util.NoSuchElementException; .pdf  import java.util.Iterator; import java.util.NoSuchElementException; .pdf
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
 
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdfimport java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
 
Seo Expert course in Pakistan
Seo Expert course in PakistanSeo Expert course in Pakistan
Seo Expert course in Pakistan
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
 
linkedlistwith animations.ppt
linkedlistwith animations.pptlinkedlistwith animations.ppt
linkedlistwith animations.ppt
 
The hashtable youll be making will use Strings as the keys and Obje.pdf
The hashtable youll be making will use Strings as the keys and Obje.pdfThe hashtable youll be making will use Strings as the keys and Obje.pdf
The hashtable youll be making will use Strings as the keys and Obje.pdf
 
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
 
Complete code in Java The hashtable you'll be making will use String.pdf
Complete code in Java   The hashtable you'll be making will use String.pdfComplete code in Java   The hashtable you'll be making will use String.pdf
Complete code in Java The hashtable you'll be making will use String.pdf
 
4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf
 
Note- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdfNote- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdf
 
Chapter14
Chapter14Chapter14
Chapter14
 

More from eyevisioncare1

What is investigative reporting How is it relevant to white collar .pdf
What is investigative reporting How is it relevant to white collar .pdfWhat is investigative reporting How is it relevant to white collar .pdf
What is investigative reporting How is it relevant to white collar .pdf
eyevisioncare1
 
What impact did the Industrial Revolution have on American society b.pdf
What impact did the Industrial Revolution have on American society b.pdfWhat impact did the Industrial Revolution have on American society b.pdf
What impact did the Industrial Revolution have on American society b.pdf
eyevisioncare1
 
The memory cycle times have been much longer than processor cycle ti.pdf
The memory cycle times have been much longer than processor cycle ti.pdfThe memory cycle times have been much longer than processor cycle ti.pdf
The memory cycle times have been much longer than processor cycle ti.pdf
eyevisioncare1
 
Suppose you land on a previously unexplored South Pacific island. Yo.pdf
Suppose you land on a previously unexplored South Pacific island. Yo.pdfSuppose you land on a previously unexplored South Pacific island. Yo.pdf
Suppose you land on a previously unexplored South Pacific island. Yo.pdf
eyevisioncare1
 
State two factors that have contributed to the development of emergi.pdf
State two factors that have contributed to the development of emergi.pdfState two factors that have contributed to the development of emergi.pdf
State two factors that have contributed to the development of emergi.pdf
eyevisioncare1
 
Research and prepare a presentation to demonstrate a scenario in whi.pdf
Research and prepare a presentation to demonstrate a scenario in whi.pdfResearch and prepare a presentation to demonstrate a scenario in whi.pdf
Research and prepare a presentation to demonstrate a scenario in whi.pdf
eyevisioncare1
 
QUESTION 1Which ecological niche described below possesses the gre.pdf
QUESTION 1Which ecological niche described below possesses the gre.pdfQUESTION 1Which ecological niche described below possesses the gre.pdf
QUESTION 1Which ecological niche described below possesses the gre.pdf
eyevisioncare1
 

More from eyevisioncare1 (20)

Which of the following does NOT describe a mechanism that cells use t.pdf
Which of the following does NOT describe a mechanism that cells use t.pdfWhich of the following does NOT describe a mechanism that cells use t.pdf
Which of the following does NOT describe a mechanism that cells use t.pdf
 
Which of the following is NOT a reason to use frequency distribution.pdf
Which of the following is NOT a reason to use frequency distribution.pdfWhich of the following is NOT a reason to use frequency distribution.pdf
Which of the following is NOT a reason to use frequency distribution.pdf
 
When the body needs to conserve water, the kidneys excrete a hyperton.pdf
When the body needs to conserve water, the kidneys excrete a hyperton.pdfWhen the body needs to conserve water, the kidneys excrete a hyperton.pdf
When the body needs to conserve water, the kidneys excrete a hyperton.pdf
 
What would be a typical phenotypic monohybrid ratio in which a letha.pdf
What would be a typical phenotypic monohybrid ratio in which a letha.pdfWhat would be a typical phenotypic monohybrid ratio in which a letha.pdf
What would be a typical phenotypic monohybrid ratio in which a letha.pdf
 
What two things are added to NADP+ to convert it into NADPH H+ and .pdf
What two things are added to NADP+ to convert it into NADPH  H+ and .pdfWhat two things are added to NADP+ to convert it into NADPH  H+ and .pdf
What two things are added to NADP+ to convert it into NADPH H+ and .pdf
 
what is the medical term used for a dilated hair follicle filled .pdf
what is the medical term used for  a dilated hair follicle filled .pdfwhat is the medical term used for  a dilated hair follicle filled .pdf
what is the medical term used for a dilated hair follicle filled .pdf
 
What is investigative reporting How is it relevant to white collar .pdf
What is investigative reporting How is it relevant to white collar .pdfWhat is investigative reporting How is it relevant to white collar .pdf
What is investigative reporting How is it relevant to white collar .pdf
 
What impact did the Industrial Revolution have on American society b.pdf
What impact did the Industrial Revolution have on American society b.pdfWhat impact did the Industrial Revolution have on American society b.pdf
What impact did the Industrial Revolution have on American society b.pdf
 
What are the major inputs to the MRP systemSolutionMaterial r.pdf
What are the major inputs to the MRP systemSolutionMaterial r.pdfWhat are the major inputs to the MRP systemSolutionMaterial r.pdf
What are the major inputs to the MRP systemSolutionMaterial r.pdf
 
Type I alveolar cells(a) allow rapid diffusion of gases through t.pdf
Type I alveolar cells(a) allow rapid diffusion of gases through t.pdfType I alveolar cells(a) allow rapid diffusion of gases through t.pdf
Type I alveolar cells(a) allow rapid diffusion of gases through t.pdf
 
The memory cycle times have been much longer than processor cycle ti.pdf
The memory cycle times have been much longer than processor cycle ti.pdfThe memory cycle times have been much longer than processor cycle ti.pdf
The memory cycle times have been much longer than processor cycle ti.pdf
 
Suppose you land on a previously unexplored South Pacific island. Yo.pdf
Suppose you land on a previously unexplored South Pacific island. Yo.pdfSuppose you land on a previously unexplored South Pacific island. Yo.pdf
Suppose you land on a previously unexplored South Pacific island. Yo.pdf
 
State two factors that have contributed to the development of emergi.pdf
State two factors that have contributed to the development of emergi.pdfState two factors that have contributed to the development of emergi.pdf
State two factors that have contributed to the development of emergi.pdf
 
Recall the unambiguous grammar for arithmetic expressions discussed .pdf
Recall the unambiguous grammar for arithmetic expressions discussed .pdfRecall the unambiguous grammar for arithmetic expressions discussed .pdf
Recall the unambiguous grammar for arithmetic expressions discussed .pdf
 
Research and prepare a presentation to demonstrate a scenario in whi.pdf
Research and prepare a presentation to demonstrate a scenario in whi.pdfResearch and prepare a presentation to demonstrate a scenario in whi.pdf
Research and prepare a presentation to demonstrate a scenario in whi.pdf
 
QUESTION 1Which ecological niche described below possesses the gre.pdf
QUESTION 1Which ecological niche described below possesses the gre.pdfQUESTION 1Which ecological niche described below possesses the gre.pdf
QUESTION 1Which ecological niche described below possesses the gre.pdf
 
Practice assembly question need helpGiven the following array decl.pdf
Practice assembly question need helpGiven the following array decl.pdfPractice assembly question need helpGiven the following array decl.pdf
Practice assembly question need helpGiven the following array decl.pdf
 
Peripheral proteins function primarily as ion channels and active tra.pdf
Peripheral proteins function primarily as ion channels and active tra.pdfPeripheral proteins function primarily as ion channels and active tra.pdf
Peripheral proteins function primarily as ion channels and active tra.pdf
 
One of the main purposes of a bus system is to allow the exchange of.pdf
One of the main purposes of a bus system is to allow the exchange of.pdfOne of the main purposes of a bus system is to allow the exchange of.pdf
One of the main purposes of a bus system is to allow the exchange of.pdf
 
Most Americans are immune to the bacterium that causes Leprosy, do.pdf
Most Americans are immune to the bacterium that causes Leprosy, do.pdfMost Americans are immune to the bacterium that causes Leprosy, do.pdf
Most Americans are immune to the bacterium that causes Leprosy, do.pdf
 

Recently uploaded

SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MysoreMuleSoftMeetup
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdfContoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
cupulin
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
Peter Brusilovsky
 

Recently uploaded (20)

Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdfRich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdfContoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 

I need help understanding how this linked list set method works in j.pdf

  • 1. I need help understanding how this linked list set method works in java, * Set value stored at location i to object o, returning old value. * * @pre 0 <= i < size() * @post sets ith entry of list to value o, returns old value * @param i location of entry to be changed. * @param o new value * @return former value of ith entry of list. */ public E set(int i, E o) { if (i >= size()) return null; Node finger = head; // search for ith element or end of list while (i > 0) { finger = finger.next(); i--; } // get old value, update new value E result = finger.value(); finger.setValue(o); return result; Solution Hi friend, Please go through comment, you can uderstand logic of set method. public E set(int i, E o) { // if given position number (i) is greater than current size of list, // then you can not set, because this is not valid position, so return null if (i >= size()) return null; Node finger = head; // initializing finger with head node of list
  • 2. // search for ith element or end of list while (i > 0) // traverse until we not hit ith node in list { finger = finger.next(); i--; } // now after above while loop, finger points ith node in list // get old value, update new value E result = finger.value(); // getting old value of ith node finger.setValue(o); // setting new value in ith node return result; }