Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

BuildDLL.java public class BuildDLL { DoubleLinkedNode front, rear; private static char[] letters = new char[] {'K', 'T', 'E', 'N', 'P', 'A',

imageimageimageimage

BuildDLL.java 


public class BuildDLL {
   
   DoubleLinkedNode front, rear;
   private static char[] letters = new char[] {'K', 'T', 'E', 'N', 'P', 'A', 'L'};

   public BuildDLL () {
      build();
   }
   
   

   public void remove (Character elem) {
     
      // Add code in here to remove the node with the given value.
     

   }
   
   
   private void build () {
      DoubleLinkedNode pnode, node;
     
      node = new DoubleLinkedNode(letters[0]);
      pnode = front = node;
      for (int i = 1; i < 7; i++) {
         node = new DoubleLinkedNode(letters[i]);
         pnode.setNext(node);
         node.setPrevious(pnode);
         pnode = node;
      }
      rear = node;
   }
   
   public DoubleLinkedNode getFront () {
      return front;
   }
   
   public DoubleLinkedNode getRear () {
      return rear;
   }
   
   public void printF (DoubleLinkedNode node) {
     
      DoubleLinkedNode curr = front;
      while (curr != null) {
         System.out.print(curr.getElement() + " ");
         curr = curr.getNext();
      }
      System.out.print("\n");
   }
   
   
   public static void main (String[] args) {
      BuildDLL dll = new BuildDLL();

      System.out.println("Original List:");
      dll.printF(dll.getFront());
      System.out.println("***");
     
      System.out.println("Removing an internal node:");
      dll.remove('N');
      dll.printF(dll.getFront());
      System.out.println("***");
     
      System.out.println("Removing the front node:");
      dll.remove('K');
      dll.printF(dll.getFront());
      System.out.println("***");
     
      System.out.println("Removing the rear node:");
      dll.remove('L');
      dll.printF(dll.getFront());
      System.out.println("***");
   }

}
 

BuildLinkedList.java

/**
 * Build a linked list of integers from 1 to 10
 * 
 * @author CS1027
 */
public class BuildLinkedList {

   /*
    * Print the information stored in all the nodes of the list whose first node is
    * referenced by front
    */
   private static void printList(LinearNode front) {
      LinearNode current = front;
      for (int i = 1; i <= 10; i++) {
         System.out.println(current.getElement());
         current = current.getNext();
      }
   }

   public static void main(String[] args) {

      // create a linked list that holds 1, 2, ..., 10
      // by starting at 10 and adding each node at head of list

      LinearNode front = null; // create empty linked list
      LinearNode intNode;

      for (int i = 10; i >= 1; i--) {
         // create a new node for i
         intNode = new LinearNode(new Integer(i));
         // add it at the head of the linked list
         intNode.setNext(front);
         front = intNode;
      }

      printList(front);
   }

}
 

DoubleLinkedNode.java

public class DoubleLinkedNode {

   private DoubleLinkedNode next;
   private DoubleLinkedNode previous;
   private E element;

   public DoubleLinkedNode(){
      next = null;
      previous = null;
      element = null;
   }

   public DoubleLinkedNode (E elem){
      next = null;
      previous = null;
      element = elem;
   }

   public DoubleLinkedNode getNext(){
      return next;
   }

   public DoubleLinkedNode getPrevious(){
      return previous;
   }

   public void setNext (DoubleLinkedNode node){
      next = node;
   }

   public void setPrevious (DoubleLinkedNode node){
      previous = node;
   }

   public E getElement(){
      return element;
   }

   public void setElement (E elem){
      element = elem;
   }

}
 

LinearNode.java

/**
 * LinearNode represents a node in a linked list.
 *
 * @author Dr. Lewis
 * @author Dr. Chase
 * @version 1.0, 08/13/08
 */

public class LinearNode
{
    private LinearNode next;
    private E element;
   
    /**
     * Creates an empty node.
     */
    public LinearNode()
    {
        next = null;
        element = null;
    }
   
    /**
     * Creates a node storing the specified element.
     *
     * @param elem  the element to be stored within the new node
     */
    public LinearNode (E elem)
    {
        next = null;
        element = elem;
    }
   
    /**
     * Returns the node that follows this one.
     *
     * @return  the node that follows the current one
     */
    public LinearNode getNext()
    {
        return next;
    }
   
    /**
     * Sets the node that follows this one.
     *
     * @param node  the node to be set to follow the current one
     */
    public void setNext (LinearNode node)
    {
        next = node;
    }
   
    /**
     * Returns the element stored in this node.
     *
     * @return  the element stored in this node
     */
    public E getElement()
    {
        return element;
    }
   
    /**
     * Sets the element stored in this node.
     *
     * @param elem  the element to be stored in this node
     */
    public void setElement (E elem)
    {
        element = elem;
    }
}
 

Person.java

/**
 * Class that represents a person with attributes name, email address 
 * @author CS1027
 *
 */
public class Person {
   
   /* Attribute declarations */
   private String lastName;   // last name
   private String firstName;  // first name
   
   /**
    * Constructor initializes the person's name and email address
    */
   public Person(String firstName, String lastName) {
      this.firstName = firstName;
      this.lastName = lastName;    
   }

   /**
    * getName method returns the person's full name
    * @return first name followed by last name, blank separated
    */
   public String getName () {
      return firstName + " " + lastName;
   }

   /**
    * toString method returns a string representation of the person
    * @return string with first name and last name, email address 
    */
   public String toString() {
      String s = firstName + " " + lastName;
      return s;
   }
   
}

 

TestReverse.java


public class TestReverse {
   
   
   public static void main (String[] args) {

      String[] arr1 = new String[] {"atom", "breeze", "clock", "daydream", "energy"};
      ReversibleArray revArr1 = new ReversibleArray(arr1);
      System.out.println(revArr1.toString());
      revArr1.reverse();
      System.out.println(revArr1.toString());
     
      Integer[] arr2 = new Integer[] {11, 22, 33, 44, 55, 66, 77};
      ReversibleArray revArr2 = new ReversibleArray(arr2);
      System.out.println(revArr2.toString());
      revArr2.reverse();
      System.out.println(revArr2.toString());

      Person p1 = new Person("Jerry", "Seinfeld");
      Person p2 = new Person("Salma", "Hayek");
      Person p3 = new Person("Reese", "Witherspoon");
      Person p4 = new Person("Vince", "Vaughn");
      Person[] arr3 = new Person[] {p1, p2, p3, p4};
      ReversibleArray revArr3 = new ReversibleArray(arr3);
      System.out.println(revArr3.toString());
      revArr3.reverse();
      System.out.println(revArr3.toString());
     
   }

}

Exercise 1 - Creating a class with generics 1. Create a new class called ReversibleArray. 2. In the class header, add the code immediately to the right of the class name. 3. Give this class two private instance variables: T array and int count 4. Create a constructor which takes in one parameter of type T[] and assign that parameter's value into this. array. Set count as the length of the array. 5. Add a toString() method that outputs the array values in the format: elem0, elem1, elem2, etc. (hint: add the comma and space only if you are not on the final iteration of the loop). 6. Create the most important function in this class: public void reverse () { } a. This method must swap elements within the array, without making a new array or other collection. b. The swaps should be made symmetrically about the middle of the array. This means array[0] swaps with array[n-1], array[1] swaps with array[n-2], etc. c. Use a temp variable to help with these swaps. What variable type should this temp variable be? d. How many swaps must be made to correctly reverse the array? 7. Open TestReverse.java and read over the code in its main() function.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Java Programming, Comprehensive Version

Authors: Y. Daniel Liang

10th Edition

133761312, 978-0133761313

More Books

Students also viewed these Programming questions