SlideShare a Scribd company logo
Objective: Manipulate the Linked List Pointer.
Make acopy of LList.java and rename it to LListr.java. Add a reverse function in LListr.java to
reverse the order of the linked list.
You can either use the gamescore.txt to test the reverse function.
Llist.Java File:
/** Source code example for "A Practical Introduction to Data
Structures and Algorithm Analysis, 3rd Edition (Java)"
by Clifford A. Shaffer
Copyright 2008-2011 by Clifford A. Shaffer
*/
// Doubly linked list implementation
class LList implements List {
private DLink head; // Pointer to list header
private DLink tail; // Pointer to last element in list
protected DLink curr; // Pointer ahead of current element
int cnt; // Size of list
//Constructors
LList(int size) { this(); } // Ignore size
LList() {
curr = head = new DLink(null, null); // Create header node
tail = new DLink(head, null);
head.setNext(tail);
cnt = 0;
}
public void clear() { // Remove all elements from list
head.setNext(null); // Drop access to rest of links
curr = head = new DLink(null, null); // Create header node
tail = new DLink(head, null);
head.setNext(tail);
cnt = 0;
}
public void moveToStart() // Set curr at list start
{ curr = head; }
public void moveToEnd() // Set curr at list end
{ curr = tail.prev(); }
/** Insert "it" at current position */
public void insert(E it) {
curr.setNext(new DLink(it, curr, curr.next()));
curr.next().next().setPrev(curr.next());
cnt++;
}
/** Append "it" to list */
public void append(E it) {
tail.setPrev(new DLink(it, tail.prev(), tail));
tail.prev().prev().setNext(tail.prev());
cnt++;
}
/** Remove and return current element */
public E remove() {
if (curr.next() == tail) return null; // Nothing to remove
E it = curr.next().element(); // Remember value
curr.next().next().setPrev(curr);
curr.setNext(curr.next().next()); // Remove from list
cnt--; // Decrement the count
return it; // Return value removed
}
/** Move curr one step left; no change if at front */
public void prev() {
if (curr != head) // Can't back up from list head
curr = curr.prev();
}
// Move curr one step right; no change if at end
public void next()
{ if (curr != tail.prev()) curr = curr.next(); }
public int length() { return cnt; }
// Return the position of the current element
public int currPos() {
DLink temp = head;
int i;
for (i=0; curr != temp; i++)
temp = temp.next();
return i;
}
// Move down list to "pos" position
public void moveToPos(int pos) {
assert (pos>=0) && (pos<=cnt) : "Position out of range";
curr = head;
for(int i=0; i. The vertical
* bar represents the current location of the fence. This method
* uses toString() on the individual elements.
* @return The string representation of this list
*/
public String toString()
{
// Save the current position of the list
int oldPos = currPos();
int length = length();
StringBuffer out = new StringBuffer((length() + 1) * 4);
moveToStart();
out.append("< ");
for (int i = 0; i < oldPos; i++) {
if (getValue()!=null)
{
out.append(getValue());
out.append(" ");
}
next();
}
out.append("| ");
for (int i = oldPos; i < length; i++) {
out.append(getValue());
out.append(" ");
next();
}
out.append(">");
moveToPos(oldPos); // Reset the fence to its original position
return out.toString();
}
}
gamescore.txt
Mike,1105
Rob,750
Paul,720
Anna,660
Rose,590
Jack,510
gamescore.txt
Solution
Here is the function you need:
public void reverseList()
{
DLink oldStart = tail; //Copies the last node address.
DLink temp; //Holds a temporary pointer to hold a node.
moveToStart(); //Makes the current move to the first position.
while(oldStart != head)
{
temp = remove(); //Removes the first element in the list.
append(temp); //Appends it to the end of the list.
moveToStart(); //Makes the current move to the first position.
}
}

More Related Content

Similar to Objective Manipulate the Linked List Pointer.Make acopy of LList..pdf (20)

PPTX
Stack implementation using linked list ppt
JayasankarShyam
 
PDF
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
anwarsadath111
 
PDF
How do you stop infinite loop Because I believe that it is making a.pdf
feelinggift
 
PDF
Using the C++ programming language1. Implement the UnsortedList cl.pdf
mallik3000
 
PDF
Unit - 2.pdf
AravindAnand21
 
PDF
Data Structures in C++I am really new to C++, so links are really .pdf
rohit219406
 
PPTX
Adt of lists
Nivegeetha
 
PPTX
DS-3asdfghjklxmmcnaefiuhavbifuhablc.pptx
DRCARIBOU
 
PDF
My question is pretty simple, I just want to know how to call my ope.pdf
jeetumordhani
 
PDF
#includeiostream#includecstdio#includecstdlibusing names.pdf
KUNALHARCHANDANI1
 
PDF
All code should be in C++Using the UnsortedList class (UnsortedLis.pdf
akashenterprises93
 
PPT
강의자료10
Young Wook Kim
 
PDF
C++ Please test your program before you submit the answer.pdf
aashisha5
 
DOCX
#ifndef MYLIST_H_ #define MYLIST_H_#includeiostream #include.docx
ajoy21
 
PPT
Lec6 mod linked list
Saad Gabr
 
PDF
The LinkedList1 class implements a Linked list. class.pdf
malavshah9013
 
PDF
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
footstatus
 
DOCX
Below is a depiction of a doubly-linked list implementation of the bag.docx
gilliandunce53776
 
PDF
In C++Add the function min as an abstract function to the classar.pdf
fantoosh1
 
Stack implementation using linked list ppt
JayasankarShyam
 
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
anwarsadath111
 
How do you stop infinite loop Because I believe that it is making a.pdf
feelinggift
 
Using the C++ programming language1. Implement the UnsortedList cl.pdf
mallik3000
 
Unit - 2.pdf
AravindAnand21
 
Data Structures in C++I am really new to C++, so links are really .pdf
rohit219406
 
Adt of lists
Nivegeetha
 
DS-3asdfghjklxmmcnaefiuhavbifuhablc.pptx
DRCARIBOU
 
My question is pretty simple, I just want to know how to call my ope.pdf
jeetumordhani
 
#includeiostream#includecstdio#includecstdlibusing names.pdf
KUNALHARCHANDANI1
 
All code should be in C++Using the UnsortedList class (UnsortedLis.pdf
akashenterprises93
 
강의자료10
Young Wook Kim
 
C++ Please test your program before you submit the answer.pdf
aashisha5
 
#ifndef MYLIST_H_ #define MYLIST_H_#includeiostream #include.docx
ajoy21
 
Lec6 mod linked list
Saad Gabr
 
The LinkedList1 class implements a Linked list. class.pdf
malavshah9013
 
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
footstatus
 
Below is a depiction of a doubly-linked list implementation of the bag.docx
gilliandunce53776
 
In C++Add the function min as an abstract function to the classar.pdf
fantoosh1
 

More from rajeshjangid1865 (20)

PDF
write Ocaml programe to add all numbers in a list the solution .pdf
rajeshjangid1865
 
PDF
why is lifelong learning important for Engineers Give an example to.pdf
rajeshjangid1865
 
PDF
Which of the following is true of aldol reactions1.The thermodyna.pdf
rajeshjangid1865
 
PDF
Using at least two examples (whenever applicable), concisely discuss .pdf
rajeshjangid1865
 
PDF
Transforming Cultures from Consumerism to Sustainability - Essay.pdf
rajeshjangid1865
 
PDF
Trane has 145 marbles. He gives 20 to Katie, 52 to Gwen, and 31 to Yu.pdf
rajeshjangid1865
 
PDF
This project should be done in C# using Visual Studio - Windows Form.pdf
rajeshjangid1865
 
PDF
The table below gives the probabilities of combinations of religion a.pdf
rajeshjangid1865
 
PDF
The effects Poverty in SocietySolution Poor children are at gre.pdf
rajeshjangid1865
 
PDF
Suppose 1.01g of FeCl3 is placed in a 10.0ml volumetric glass, water.pdf
rajeshjangid1865
 
PDF
Specialized regions on the cell surface through which cells are joine.pdf
rajeshjangid1865
 
PDF
Section 404 of the Sarbanes Oxley Act requires auditors of a public .pdf
rajeshjangid1865
 
PDF
Reiji and Tuneko Okazaki conducted a now classic experiment in 1968 .pdf
rajeshjangid1865
 
PDF
Problem 2-1A Suppose the following items are taken from the 2017 bala.pdf
rajeshjangid1865
 
PDF
Prepare a 2017 income statement for Shanta Corporation based on the f.pdf
rajeshjangid1865
 
PDF
Organizations need to have a pool of managerial talent to take on jo.pdf
rajeshjangid1865
 
PDF
Militarism Alliances Imperialism Nationalism Class, the powder keg.pdf
rajeshjangid1865
 
PDF
In the subject of cryptography, what policy or organizational challe.pdf
rajeshjangid1865
 
PDF
is Google making us stupid Nicholas Carr Summarize Article. https.pdf
rajeshjangid1865
 
PDF
If the environment of propagation has be th specular and scattering c.pdf
rajeshjangid1865
 
write Ocaml programe to add all numbers in a list the solution .pdf
rajeshjangid1865
 
why is lifelong learning important for Engineers Give an example to.pdf
rajeshjangid1865
 
Which of the following is true of aldol reactions1.The thermodyna.pdf
rajeshjangid1865
 
Using at least two examples (whenever applicable), concisely discuss .pdf
rajeshjangid1865
 
Transforming Cultures from Consumerism to Sustainability - Essay.pdf
rajeshjangid1865
 
Trane has 145 marbles. He gives 20 to Katie, 52 to Gwen, and 31 to Yu.pdf
rajeshjangid1865
 
This project should be done in C# using Visual Studio - Windows Form.pdf
rajeshjangid1865
 
The table below gives the probabilities of combinations of religion a.pdf
rajeshjangid1865
 
The effects Poverty in SocietySolution Poor children are at gre.pdf
rajeshjangid1865
 
Suppose 1.01g of FeCl3 is placed in a 10.0ml volumetric glass, water.pdf
rajeshjangid1865
 
Specialized regions on the cell surface through which cells are joine.pdf
rajeshjangid1865
 
Section 404 of the Sarbanes Oxley Act requires auditors of a public .pdf
rajeshjangid1865
 
Reiji and Tuneko Okazaki conducted a now classic experiment in 1968 .pdf
rajeshjangid1865
 
Problem 2-1A Suppose the following items are taken from the 2017 bala.pdf
rajeshjangid1865
 
Prepare a 2017 income statement for Shanta Corporation based on the f.pdf
rajeshjangid1865
 
Organizations need to have a pool of managerial talent to take on jo.pdf
rajeshjangid1865
 
Militarism Alliances Imperialism Nationalism Class, the powder keg.pdf
rajeshjangid1865
 
In the subject of cryptography, what policy or organizational challe.pdf
rajeshjangid1865
 
is Google making us stupid Nicholas Carr Summarize Article. https.pdf
rajeshjangid1865
 
If the environment of propagation has be th specular and scattering c.pdf
rajeshjangid1865
 

Recently uploaded (20)

PDF
community health nursing question paper 2.pdf
Prince kumar
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
THE TAME BIRD AND THE FREE BIRD.pptxxxxx
MarcChristianNicolas
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
IMP NAAC REFORMS 2024 - 10 Attributes.pdf
BHARTIWADEKAR
 
PDF
CHILD RIGHTS AND PROTECTION QUESTION BANK
Dr Raja Mohammed T
 
PPTX
PPT on the Development of Education in the Victorian England
Beena E S
 
PPTX
BANDHA (BANDAGES) PPT.pptx ayurveda shalya tantra
rakhan78619
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
PPTX
Quarter1-English3-W4-Identifying Elements of the Story
FLORRACHELSANTOS
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
community health nursing question paper 2.pdf
Prince kumar
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
THE TAME BIRD AND THE FREE BIRD.pptxxxxx
MarcChristianNicolas
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
IMP NAAC REFORMS 2024 - 10 Attributes.pdf
BHARTIWADEKAR
 
CHILD RIGHTS AND PROTECTION QUESTION BANK
Dr Raja Mohammed T
 
PPT on the Development of Education in the Victorian England
Beena E S
 
BANDHA (BANDAGES) PPT.pptx ayurveda shalya tantra
rakhan78619
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
Quarter1-English3-W4-Identifying Elements of the Story
FLORRACHELSANTOS
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 

Objective Manipulate the Linked List Pointer.Make acopy of LList..pdf

  • 1. Objective: Manipulate the Linked List Pointer. Make acopy of LList.java and rename it to LListr.java. Add a reverse function in LListr.java to reverse the order of the linked list. You can either use the gamescore.txt to test the reverse function. Llist.Java File: /** Source code example for "A Practical Introduction to Data Structures and Algorithm Analysis, 3rd Edition (Java)" by Clifford A. Shaffer Copyright 2008-2011 by Clifford A. Shaffer */ // Doubly linked list implementation class LList implements List { private DLink head; // Pointer to list header private DLink tail; // Pointer to last element in list protected DLink curr; // Pointer ahead of current element int cnt; // Size of list //Constructors LList(int size) { this(); } // Ignore size LList() { curr = head = new DLink(null, null); // Create header node tail = new DLink(head, null); head.setNext(tail); cnt = 0; } public void clear() { // Remove all elements from list head.setNext(null); // Drop access to rest of links curr = head = new DLink(null, null); // Create header node tail = new DLink(head, null); head.setNext(tail); cnt = 0; } public void moveToStart() // Set curr at list start { curr = head; } public void moveToEnd() // Set curr at list end { curr = tail.prev(); }
  • 2. /** Insert "it" at current position */ public void insert(E it) { curr.setNext(new DLink(it, curr, curr.next())); curr.next().next().setPrev(curr.next()); cnt++; } /** Append "it" to list */ public void append(E it) { tail.setPrev(new DLink(it, tail.prev(), tail)); tail.prev().prev().setNext(tail.prev()); cnt++; } /** Remove and return current element */ public E remove() { if (curr.next() == tail) return null; // Nothing to remove E it = curr.next().element(); // Remember value curr.next().next().setPrev(curr); curr.setNext(curr.next().next()); // Remove from list cnt--; // Decrement the count return it; // Return value removed } /** Move curr one step left; no change if at front */ public void prev() { if (curr != head) // Can't back up from list head curr = curr.prev(); } // Move curr one step right; no change if at end public void next() { if (curr != tail.prev()) curr = curr.next(); } public int length() { return cnt; } // Return the position of the current element public int currPos() { DLink temp = head; int i; for (i=0; curr != temp; i++) temp = temp.next();
  • 3. return i; } // Move down list to "pos" position public void moveToPos(int pos) { assert (pos>=0) && (pos<=cnt) : "Position out of range"; curr = head; for(int i=0; i. The vertical * bar represents the current location of the fence. This method * uses toString() on the individual elements. * @return The string representation of this list */ public String toString() { // Save the current position of the list int oldPos = currPos(); int length = length(); StringBuffer out = new StringBuffer((length() + 1) * 4); moveToStart(); out.append("< "); for (int i = 0; i < oldPos; i++) { if (getValue()!=null) { out.append(getValue()); out.append(" "); } next(); } out.append("| "); for (int i = oldPos; i < length; i++) { out.append(getValue()); out.append(" "); next(); } out.append(">"); moveToPos(oldPos); // Reset the fence to its original position return out.toString();
  • 4. } } gamescore.txt Mike,1105 Rob,750 Paul,720 Anna,660 Rose,590 Jack,510 gamescore.txt Solution Here is the function you need: public void reverseList() { DLink oldStart = tail; //Copies the last node address. DLink temp; //Holds a temporary pointer to hold a node. moveToStart(); //Makes the current move to the first position. while(oldStart != head) { temp = remove(); //Removes the first element in the list. append(temp); //Appends it to the end of the list. moveToStart(); //Makes the current move to the first position. } }