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...
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
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...
Generic types A class or interface that declares one or more generic variables is called a...
Generic types A class or interface that declares one or more generic variables is called a generic type. In this portion of the activity, you will make a class generic. Open GenericsC.java in jGRASP then compile it. At this point you should be familiar with the two type-safety warnings given by the compiler. You should be able to understand the source of the error: the use of the raw types List and Collection. Since the List being declared (al) is...
Create a class called Height Copy the code for Height given below into the class. Remember...
Create a class called Height Copy the code for Height given below into the class. Remember – test the methods as you go Take a few minutes to understand what the class does. There are comments where you will have to be changing code. A list of changes are given Change the setFeet and setInches mutators to make sure the height and width will not be less than 0, no matter what is passed in to it Change constructor that...
Please use original C++ code for this. This is for visual studio. Program 5: Shipping Calculator...
Please use original C++ code for this. This is for visual studio. Program 5: Shipping Calculator The Speedy Shipping Company will ship packages based on how much they weigh and how far they are being sent. They will only ship small packages up to 10 pounds. You have been tasked with writing a program that will help Speedy Shipping determine how much to charge per delivery. The charges are based on each 500 miles shipped. Shipping charges are not pro-rated;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT