Question

In: Computer Science

Adapt the original LinkList class to use the generic Gnode class Code for LinkList below class...

  • Adapt the original LinkList class to use the generic Gnode class

Code for LinkList below

class LinkList {
    Node llist;

    LinkList( int sz ) {
   if ( sz <= 0 ) {
       llist = null;
   }
   else {
       // start with list of size 1
       llist = new Node( "0", null ); 
       Node current = llist; // temp node for loop
       // add further nodes
       for ( int i=1; i<sz; ++i ) {
      // create node and attach it to the list
      Node node2Add = new Node( Integer.toString(i), null );
      current.setNext(node2Add);   // add first node
      current=node2Add;
       }
   }
    }
    
    /**
     * Print all the elements of the list assuming that they are Strings
     */
    public void print() {
   /* Print the list */
   Node current = llist; // point to the first node
   while (current != null) {
       System.out.print((String)current.getElement() + " ");  
       current = current.getNext(); // move to the next
   }
   System.out.println();  
    }

    public void deleteFirst() {
   if ( llist != null ) {
       llist = llist.getNext();
   }
    }

    public void deleteLast() {
   if ( llist == null ) return; // no node
   Node prev = llist;
   Node current = prev.getNext(); 
   if ( current == null ) { // only 1 node
       llist = null;
       return;
   }
   while ( current.getNext() != null ) { // more than 1 node
       prev = current;
       current = current.getNext();
   }
   prev.setNext( null );
   return;
    }

    // create and display a linked list
    public static void main(String [] args){
   /* Create the list */
   LinkList llist = new LinkList( 5 );
   /* Print the list */
   llist.print();
   /* delete first and print */
   llist.deleteFirst();
   llist.print();
   /* delete last and print 5 times */
   for ( int i=0; i< 5; ++i ) {
       llist.deleteLast();
       llist.print();
   }
    }
}

Code for GNODE below

public class GNode<E> {
  // Instance variables:
  private E element;
  private GNode<E> next;
  /** Creates a node with null references to its element and next node. */
  public GNode() {
    this(null, null);
  }
  /** Creates a node with the given element and next node. */
  public GNode(E e, GNode<E> n) {
    element = e;
    next = n;
  }
  // Accessor methods:
  public E getElement() {
    return element; 
  }
  public GNode<E> getNext() { 
    return next;
  }
  // Modifier methods:
  public void setElement(E newElem) { 
    element = newElem; 
  }
  public void setNext(GNode<E> newNext) {
    next = newNext; 
  }
}

Solutions

Expert Solution

CODE

class LinkList {
   GNode<String> llist;

    LinkList( int sz ) {
   if ( sz <= 0 ) {
       llist = null;
   }
   else {
       // start with list of size 1
       llist = new GNode<>( "0", null );
       GNode<String> current = llist; // temp node for loop
       // add further nodes
       for ( int i=1; i<sz; ++i ) {
      // create node and attach it to the list
           GNode<String> node2Add = new GNode<>( Integer.toString(i), null );
      current.setNext(node2Add);   // add first node
      current=node2Add;
       }
   }
    }
  
    /**
     * Print all the elements of the list assuming that they are Strings
     */
    public void print() {
   /* Print the list */
       GNode<String> current = llist; // point to the first node
   while (current != null) {
       System.out.print((String)current.getElement() + " ");
       current = current.getNext(); // move to the next
   }
   System.out.println();
    }

    public void deleteFirst() {
   if ( llist != null ) {
       llist = llist.getNext();
   }
    }

    public void deleteLast() {
   if ( llist == null ) return; // no node
   GNode<String> prev = llist;
   GNode<String> current = prev.getNext();
   if ( current == null ) { // only 1 node
       llist = null;
       return;
   }
   while ( current.getNext() != null ) { // more than 1 node
       prev = current;
       current = current.getNext();
   }
   prev.setNext( null );
   return;
    }

    // create and display a linked list
    public static void main(String [] args){
   /* Create the list */
   LinkList llist = new LinkList( 5 );
   /* Print the list */
   llist.print();
   /* delete first and print */
   llist.deleteFirst();
   llist.print();
   /* delete last and print 5 times */
   for ( int i=0; i< 5; ++i ) {
       llist.deleteLast();
       llist.print();
   }
    }
}


Related Solutions

Change the LinkList class to work with DNode, in particular, adapt the implementation of the void...
Change the LinkList class to work with DNode, in particular, adapt the implementation of the void LinkList.deleteLast() method. LINKLIST CLASS BELOW /** * Builds a singly linked list of size 5 and prints it to the console. * * @author Jochen Lang */ class LinkList { DNode llist; LinkList( int sz ) {    if ( sz <= 0 ) {    llist = null;    }    else {    // start with list of size 1    llist...
Use Javascript to implement the class below to the code below. Create a class Order which...
Use Javascript to implement the class below to the code below. Create a class Order which contains one array member variable that stores Sandwich objects. Initially, this array is empty. Add a function named add that adds a sandwich to the array. Add a getter function named price which iterates over the array and sums up the price of each  sandwich in the array. class Sandwich { constructor(price) { this.price = price; } toString() { return "Sandwich", this.price; } } class...
Implement a generic queue.  Use the code below for main(). main() { int i, x; float y;...
Implement a generic queue.  Use the code below for main(). main() { int i, x; float y; char z; Queue<int> A; Queue<float> B; Queue<char> C; ifstream in; in.open("int.txt"); for (i = 0; i < 100; i++){         in >> x;         A.enqueue(x);     } cout << A.dequeue() << endl;; for (i = 0; i < 12; i++)         A.enqueue(i); cout << A.dequeue() << endl; cout << A.dequeue() << endl; cout << "Dequeueing: "<< A.dequeue()<< endl; while (!A.isEmpty())      cout  << A.dequeue() << "  "; if (A.isEmpty())       cout <<...
class LinkListTest { public static void main(String[] args) { LinkList theList = new LinkList(); theList.insertFirst(7); theList.insertFirst(6);...
class LinkListTest { public static void main(String[] args) { LinkList theList = new LinkList(); theList.insertFirst(7); theList.insertFirst(6); theList.insertFirst(5); theList.insertFirst(4); theList.insertFirst(3); theList.insertFirst(2); theList.insertFirst(1); theList.displayList(); System.out.println("delete(4)"); theList.delete(4); System.out.println("delete(16)"); theList.delete(16); theList.displayList(); System.out.println("insertAfter(2, 12)"); theList.insertAfter(2, 12); System.out.println("insertAfter(4, 14)"); theList.insertAfter(4, 14); System.out.println("insertAfter(7, 17)"); theList.insertAfter(7, 17); theList.displayList(); System.out.println("insertLast(20)"); theList.insertLast(20); theList.displayList(); } } class Link { public int iData; // data item public Link next; // next link in list // ------------------------------------------------------------- public Link(int id) // constructor { iData = id; // initialize data next = null; }...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document { private int words; /** * constructor * pre: none * post: A Document object created. Words initialized to 0. */ public Document() { words = 0; //default words } /** * Changes the number of document words. * pre: none * post: Words has been changed. */ public void setWords(int numWords) { words = numWords; } /** * Calculates the number of pages....
Suppose a linklist consists of more than 50 node, write a code to delete the tail...
Suppose a linklist consists of more than 50 node, write a code to delete the tail node (IN JAVA)
Suppose a linklist consists of more than 50 node, write a code to delete the tail...
Suppose a linklist consists of more than 50 node, write a code to delete the tail node   in java
The Objective is to create a Custom LinkList. Implement the following methods Class Name: CustomLinkList Methods...
The Objective is to create a Custom LinkList. Implement the following methods Class Name: CustomLinkList Methods void insert (String data) – insert it the item to the last row void delete() - Deletes the last row boolean exists() - returns a Boolean if found String[] toArray() String getTail() – returns the last record  String getHead () – return the 1st record Extra Credit  int delete (String data) – delete an item in the link by position.  If an item is successfully the delete method return the number 1, else return the number 0
1. Adapt the custom array list implementation code with the following changes: (a) Add code to...
1. Adapt the custom array list implementation code with the following changes: (a) Add code to the ensureCapacity() method to print out a message including how many elements are copied to the new array on resizing Array List Implementation: public class MyArrayList<E> implements MyList<E> { public static final int INITIAL_CAPACITY = 16; private E[] data = (E[])new Object[INITIAL_CAPACITY]; private int size = 0; // Number of elements in the list public MyArrayList() { }    public MyArrayList(E[] objects) { for...
Part A: (Chapter 8) Make the following updates to the original code below: Override the toString()...
Part A: (Chapter 8) Make the following updates to the original code below: Override the toString() method and equals() method inside your class [2 points] Create a Copy Constructor in your class [2 point] Create a static field in your class called counter. It should increment every time an object is created. [1 point] Create a static method that displays the counter value. Also call this method at the end of your main method. [1 point] Use the "this" reference...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT