Add to the LList.java class implementation a member function to reverse the orde
ID: 3671411 • Letter: A
Question
Add to the LList.java class implementation a member function to reverse the order of the elements on the DLink. Display the DLink after the reverse.
================================================
LList.java
/** Linked list implementation */
class LList implements List {
private Link head; // Pointer to list header
private Link tail; // Pointer to last element
protected Link curr; // Access to current element
int cnt; // Size of list
/** Constructors */
LList(int size) { this(); } // Constructor -- Ignore size
LList() {
curr = tail = head = new Link(null); // Create header
cnt = 0;
}
/** Remove all elements */
public void clear() {
head.setNext(null); // Drop access to links
curr = tail = head = new Link(null); // Create header
cnt = 0;
}
/** Insert "it" at current position */
public void insert(E it) {
curr.setNext(new Link(it, curr.next()));
if (tail == curr) tail = curr.next(); // New tail
cnt++;
}
/** Append "it" to list */
public void append(E it) {
tail = tail.setNext(new Link(it, null));
cnt++;
}
/** Remove and return current element */
public E remove() {
if (curr.next() == null) return null; // Nothing to remove
E it = curr.next().element(); // Remember value
if (tail == curr.next()) tail = curr; // Removed last
curr.setNext(curr.next().next()); // Remove from list
cnt--; // Decrement count
return it; // Return value
}
/** Set curr at list start */
public void moveToStart()
{ curr = head; }
/** Set curr at list end */
public void moveToEnd()
{ curr = tail; }
/** Move curr one step left; no change if now at front */
public void prev() {
if (curr == head) return; // No previous element
Link temp = head;
// March down list until we find the previous element
while (temp.next() != curr) temp = temp.next();
curr = temp;
}
/** Move curr one step right; no change if now at end */
public void next()
{ if (curr != tail) curr = curr.next(); }
/** @return List length */
public int length() { return cnt; }
/** @return The position of the current element */
public int currPos() {
Link 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 }
/** @return Current element value */
public E getValue() {
if(curr.next() == null) return null;
return curr.next().element();
}
// Extra stuff not printed in the book.
/**
* Generate a human-readable representation of this list's contents
* that looks something like this: < 1 2 3 | 4 5 6 >. 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++) {
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();
}
}
===================================================
DoubleListLab.java (the DLink class here)
import java.io.*;
import java.util.Scanner;
public class DoubleListLab {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(System.in);
String getInput;
LList Dlist = new LList();
DLink myList = new DLink(p, n);
File it = new File("Src/test.dat");
BufferedReader br = null;
if (it.exists()){
try{
br = new BufferedReader(new FileReader(it));
String rc = null;
while ((rc = br.readLine()) != null){
Dlist.append(rc);
}
}
catch (IOException e){
System.err.format("IOException:, %s%n, x");
}
finally {
try{
if (br != null) {
br.close();
}
}
catch (IOException e){}
}
}
System.out.println(Dlist + " The size of Dlist is " + Dlist.length());
Dlist.moveToPos(4);
System.out.println("The value of 5th record is " + Dlist.getValue());
//Part B
do{
Dlist.moveToStart();
System.out.print("Enter a number to be removed from the list or values 't' to terminate ");
getInput = sc.nextLine();
for (int i=0; i if(getInput.equals(Dlist.getValue())){
System.out.println(Dlist.getValue() + " deleted");
Dlist.remove();
System.out.println("The size of Dlist is " + Dlist.length());
}
else{
Dlist.next();
}
}
}while(!getInput.equals("t") && Dlist.length() > 0 && Dlist != null);
do{
Dlist.moveToStart();
System.out.println("Enter a number to be added to the list or t to end");
getInput = sc.nextLine();
for (int i=0; i<= Dlist.length(); i++){
if(getInput.equals(Dlist.getValue()) && Dlist.length() != 0){
System.out.println("number already on the list ");
break;
}
else if (getInput.equalsIgnoreCase("t")){
break;
}
else if (Dlist.length() == 0 || Dlist.currPos()==Dlist.length()){
Dlist.moveToStart();
Dlist.insert(getInput);
System.out.println(Dlist + " The size of Dlst is " + Dlist.length());
break;
}
else{
Dlist.next();
}
}//end of 2nd For
}while(!getInput.equals("t") && Dlist.length() > 0 && Dlist != null);//end of main For
System.out.println("BYE");
}
}
Explanation / Answer
// by traversing the list from the tail to the head, we can reverse the list
//then the reversed list can be displayed:
public void reverseTheDoublyLinkedList()
{
DLink presentNode = lastNode;
int alreadyDisplayed = 0;
System.out.println(“The list in the reverse order:”);
int size = NUMWIDTH;
while (alredayDisplayed < size) {
StringBuffer auxString = new StringBuffer(presentNode.ledtData.toString());
while (auxString.length() < size)
auxString.insert(0, ‘ ‘);
System.out.print(auxString);
alreadyDisplayed++;
if ((alreadyDisplayed % 15 ) // get to the next line with a line feed character
System.out.print(“ ”);
presentNode = presentNode.previous; // traverse backwards
} // end while
System.out.println(“ printed in reverse order above”);
} // end function reverseTheDoublyLinkedList
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.