Question

In: Computer Science

Objective: Manipulate the Linked List Pointer. Write a java subclass to extend LList.java. Provide a reverse...

Objective: Manipulate the Linked List Pointer.

  1. Write a java subclass to extend LList.java. Provide a reverse list method in the subclass to reverse the order of the linked list.
  2. Print the original linked list and the reverse ordered linked list at the end of program.
  3. You can use the gamescore.txt to test the reverse method.

_____________________________________________________________________________________________________________________________________________________

/** 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

*/

/** List ADT */

public interface List {

  /** Remove all contents from the list, so it is once again

      empty. Client is responsible for reclaiming storage

      used by the list elements. */

  public void clear();

  /** Insert an element at the current location. The client

      must ensure that the list's capacity is not exceeded.   

      @param item The element to be inserted. */

  public void insert(E item);

  /** Append an element at the end of the list. The client

      must ensure that the list's capacity is not exceeded.   

      @param item The element to be appended. */

  public void append(E item);

  /** Remove and return the current element.

      @return The element that was removed. */

  public E remove();

  /** Set the current position to the start of the list */

  public void moveToStart();

  /** Set the current position to the end of the list */

  public void moveToEnd();

  /** Move the current position one step left. No change

      if already at beginning. */

  public void prev();

  /** Move the current position one step right. No change

      if already at end. */

  public void next();

  /** @return The number of elements in the list. */

  public int length();

  /** @return The position of the current element. */

  public int currPos();

  /** Set current position.

      @param pos The position to make current. */

  public void moveToPos(int pos);

  /** @return The current element. */

  public E getValue();

}

/** 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 {

protected DLink head;        // Pointer to list header

protected 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

}

public E getValue() {   // Return current element

  if (curr.next() == tail) 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();

  }

}

/** 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 node */

class DLink {

  private E element;         // Value for this node

  private DLink next;     // Pointer to next node in list

  private DLink prev;     // Pointer to previous node

  /** Constructors */

  DLink(E it, DLink p, DLink n)

  { element = it;  prev = p; next = n; }

  DLink(DLink p, DLink n) { prev = p; next = n; }

  /** Get and set methods for the data members */

  DLink next() { return next; }

  DLink setNext(DLink nextval)

    { return next = nextval; }

  DLink prev() { return prev; }

  DLink setPrev(DLink prevval)

    { return prev = prevval; }

  E element() { return element; }

  E setElement(E it) { return element = it; }

}

public class GameEntry {

protected String name;

protected int score;

public GameEntry(String n, int s) {

   name = n;

   score = s;

}

public String getName() {return name;}

public int getScore() {return score;}

public String toString() {

return "("+name+","+score+")";

}

}

//gamescore.txt

Mike,1105
Rob,750
Paul,720
Anna,660
Rose,590
Jack,510

Solutions

Expert Solution

        public void reverse() {
                if(cnt == 0) {
                        return;
                }
                DLink prev = null;
                DLink start = head;
                while(start != null) {
                        DLink next = start.next;
                        start.next = prev;
                        start.prev = next;
                        prev = start;
                        start = next;
                }
                tail = head;
                head = prev; 
        }
        
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

Exercise 2: Write a program in Java to manipulate a Double Linked List: 1. Create Double...
Exercise 2: Write a program in Java to manipulate a Double Linked List: 1. Create Double Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Double Linked List. 5. Insert a new node at the end of a DoubleLinked List 6. Insert a new node after the value 5 of Double Linked List 7. Delete the node with value 6. 8. Search an existing element in a...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Singly Linked List. 5. Insert a new node at the end of a Singly Linked List 6. Insert a new node after the value 5 of Singly Linked List 7. Delete the node with value 6. 8. Search an existing element in...
In Java, What is gained by having a Tail reference/pointer in your Linked List? A. The...
In Java, What is gained by having a Tail reference/pointer in your Linked List? A. The tail is superfluous and offers no benefits B. The tail allows us to speed up a few operations and gives us an end point to look for C.Since we have head and tail we can now do a Binary Search on the list D. It only makes deleting from the end of the list faster
Write java code to reverse a linked list. Fill in reverseLists() ReverseLinkedList.java package mid; public class...
Write java code to reverse a linked list. Fill in reverseLists() ReverseLinkedList.java package mid; public class ReverseLinkedList {     private static class ListNode {         int val;         ListNode next;         ListNode() {         }         ListNode(int val) {             this.val = val;         }         ListNode(int val, ListNode next) {             this.val = val;             this.next = next;         }     }     public static void printList(ListNode l1) {         ListNode cur = l1;         while(cur != null) {             System.out.println(cur.val);             cur = cur.next;         }     }     public static ListNode reverseLists(ListNode h1) {     }     public static void...
C++, Write a routine that would receive a pointer to the top of the linked list...
C++, Write a routine that would receive a pointer to the top of the linked list that has an integer for each node. Count all positive even integers in the linked list but make negatives into positives. Return the count negatives that became positives. Hint, use Node<ItemType>* to define pointers and use pointers to go through the linked list.
C++ Write a routine that would receive a pointer to the top of the linked list...
C++ Write a routine that would receive a pointer to the top of the linked list that has a string for each node. Count all strings that start with a vowel (assume lowercase) in the linked list but tack on a “?” on all non-vowel strings. Return the count. Hint, use Node<ItemType>* to define pointers and use pointers to go through the linked list.
In C++, Write a function to reverse the nodes in a linked list. You should not...
In C++, Write a function to reverse the nodes in a linked list. You should not create new nodes when you reverse the the linked list. The function prototype:          void reverse(Node*& head); Use the following Node definition: struct Node {    int data;    Node *next; }
C++: Write a reverse function that receives a reference to a integer linked list and reverses...
C++: Write a reverse function that receives a reference to a integer linked list and reverses the order of all the elements in it. For example, if the input linked list is 1 -> 4-> 2-> 3-> 6-> 5}, after processing by this function, the linked list should become 5-> 6-> 3-> 2-> 4-> 1. You need to write a main file to insert elements into the linked list and call the reverseLinkedList() function which takes the reference of first...
JAVA: Provide two different implementations, an array and a linked list, to maintain a list of...
JAVA: Provide two different implementations, an array and a linked list, to maintain a list of names (two separate programs).The following operations are available: insert rear, insert front, remove a particular element, and print the whole list. Do not implement an ADT(Do not use a class with data and operations) Just set up a fixed size array or a linked list of nodes in main and provide code in main or functions/static methods to perform insert, remove, and print. You...
Problem Description: Using Python write a Singly‐Linked List (with only the Head pointer) that supports the...
Problem Description: Using Python write a Singly‐Linked List (with only the Head pointer) that supports the following operations: 1. Adding an element at the middle of the list. 2. Removing the middle element of the list (and return the data). 3. Adding an element at a given index. 4. Removing an element at a given index (and return the data). For #1 and #2, ignore the operations if the length of the list is an even number. For #3, if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT