Question

Code must only be written in IntegerLinkedList.java. 9 Methods need to be implemented so that all...

Code must only be written in IntegerLinkedList.java. 9 Methods need to be implemented so that all the tests in the tester file passes. IntegerLinkedList.java implements IntegerNode.java

Your task is to implement the interface described in the file IntegerList.java using a doubly linked list in the file IntegerLinkedList.java.

Your implementation must be a doubly-linked list that maintains both a head and a tail reference. You've also been provided with a node class (IntegerNode.java) that includes a prev and next references.

//IntegerList.java interface//

public interface IntegerList
{
   /*
    * PURPOSE:
    *   Add the element x to the front of the list.
    *
    * PRECONDITIONS:
    *   None.
    * 
    * Examples:
    * 
    * If l is {1,2,3} and l.addFront(9) returns, then l is {9,1,2,3}.
    * If l is {} and l.addFront(3) returns, then l is {3}.
    */
   public void addFront (int x);


   /*
    * PURPOSE:
    *   Add the element x to the back of the list.
    *
    * PRECONDITIONS:
    *   None.
    * 
    * Examples:
    * 
    * If l is {1,2,3} and l.addBack(9) returns, then l is {1,2,3,9}.
    * If l is {} and l.addBack(9) returns, then l is {9}.
    */    
   public void addBack (int x);

   /*
    * PURPOSE:
    *   Add the element x at position pos in the list.  
    *
    * Note:
    *   In a list with 3 elements, the valid positions for addAt are
    *   0, 1, 2, 3.  
    *
    * PRECONDITIONS:
    *   pos >= 0 and pos <= l.size()
    * 
    * Examples:
    * 
    * If l is {} and l.addAt(9,0) returns, then l is {9}.
    * If l is {1} and l.addAt(9,0) returns, then l is {9,1}.
    * If l is {1,2} and l.addAt(9,1) returns, then l is {1,9,2}
    * If l is {1,2} and l.addAt(9,2) returns, then l is {1,2,9}
    */    
   public void addAt (int x, int pos);


   /*
    * PURPOSE:
    * Return the number of elements in the list
    *
    * PRECONDITIONS:
    * None.
    *
    * Examples:
    * If l is {7,13,22} l.size() returns 3
    * If l is {} l.size() returns 0
    */
   public int size();
   
   /* 
    * PURPOSE:
    *   Return the element at position pos in the list.
    * 
    * PRECONDITIONS:
    * pos >= 0 and pos < l.size()
    * 
    * Examples:
    * If l is {67,12,13} then l.get(0) returns 67
    * If l is    {67,12,13} then l.get(2) returns 13
    * If l is {92} then the result of l.get(2) is undefined.
    *
    */
   public int  get (int pos);
   
   /* 
    * PURPOSE:
    *   Remove all elements from the list.  After calling this
    *   method on a list l, l.size() will return 0
    * 
    * PRECONDITIONS:
    * None.
    * 
    * Examples:
    * If l is {67,12,13} then after l.clear(), l is {}
    * If l is {} then after l.clear(), l is {}
    *
    */
   public void clear();

   /* 
    * PURPOSE:
    *   Remove all instances of value from the list. 
    * 
    * PRECONDITIONS:
    * None.
    * 
    * Examples:
    * If l is {67,12,13,12} then after l.remove(12), l is {67,13}
    * If l is {1,2,3} then after l.remove(2), l is {1,3}
    * If l is {1,2,3} then after l.remove(99), l is {1,2,3}
    */
   public void remove (int value);

   /*
    * PURPOSE:
    *   Remove the element at position pos in the list.  
    *
    * Note:
    *   In a list with 3 elements, the valid positions for removeAt are
    *   0, 1, 2.  
    *
    * PRECONDITIONS:
    *   pos >= 0 and pos < l.size()
    * 
    * Examples:
    * 
    * If l is {1} and l.removeAt(0) returns, then l is {}.
    * If l is {1,2,3} and l.removeAt(1) returns, then l is {1,3}
    * If l is {1,2,3} and l.removeAt(2) returns, then l is {1,2}
    */    
   public void removeAt (int pos);

   /*
    * PURPOSE:
    * Return a string representation of the list
    * 
    * PRECONDITIONS:
    * None.
    *
    * Examples:
    * If l is {1,2,3,4} then l.toString() returns "{1,2,3,4}"
    * If l is {} then l.toString() returns "{}"
    *
    */
   public String toString();
}

//IntegerLinkedList.java// TO DO

public class IntegerLinkedList implements IntegerList
{
   public IntegerLinkedList()
   {

   }

   /*
    * PURPOSE:
    *   Add the element x to the front of the list.
    *
    * PRECONDITIONS:
    *   None.
    *
    * Examples:
    *
    * If l is {1,2,3} and l.addFront(9) returns, then l is {9,1,2,3}.
    * If l is {} and l.addFront(3) returns, then l is {3}.
    */
   public void addFront (int x)
   {
   }


   /*
    * PURPOSE:
    *   Add the element x to the back of the list.
    *
    * PRECONDITIONS:
    *   None.
    *
    * Examples:
    *
    * If l is {1,2,3} and l.addBack(9) returns, then l is {1,2,3,9}.
    * If l is {} and l.addBack(9) returns, then l is {9}.
    */
   public void addBack (int x)
   {
   }

   /*
    * PURPOSE:
    *   Add the element x at position pos in the list.
    *
    * Note:
    *   In a list with 3 elements, the valid positions for addAt are
    *   0, 1, 2, 3.
    *
    * PRECONDITIONS:
    *   pos >= 0 and pos <= l.size()
    *
    * Examples:
    *
    * If l is {} and l.addAt(9,0) returns, then l is {9}.
    * If l is {1} and l.addAt(9,0) returns, then l is {9,1}.
    * If l is {1,2} and l.addAt(9,1) returns, then l is {1,9,2}
    * If l is {1,2} and l.addAt(9,2) returns, then l is {1,2,9}
    */
   public void addAt (int x, int pos)
   {
   }

   /*
    * PURPOSE:
    * Return the number of elements in the list
    *
    * PRECONDITIONS:
    * None.
    *
    * Examples:
    * If l is {7,13,22} l.size() returns 3
    * If l is {} l.size() returns 0
    */
   public int size()
   {
      return -1;
   }

   /*
    * PURPOSE:
    *   Return the element at position pos in the list.
    *
    * PRECONDITIONS:
    * pos >= 0 and pos < l.size()
    *
    * Examples:
    * If l is {67,12,13} then l.get(0) returns 67
    * If l is    {67,12,13} then l.get(2) returns 13
    * If l is {92} then the result of l.get(2) is undefined.
    *
    */
   public int  get (int pos)
   {
      return -1;
   }

   /*
    * PURPOSE:
    *   Remove all elements from the list.  After calling this
    *   method on a list l, l.size() will return 0
    *
    * PRECONDITIONS:
    * None.
    *
    * Examples:
    * If l is {67,12,13} then after l.clear(), l is {}
    * If l is {} then after l.clear(), l is {}
    *
    */
   public void clear()
   {
   }

   /*
    * PURPOSE:
    *   Remove all instances of value from the list.
    *
    * PRECONDITIONS:
    * None.
    *
    * Examples:
    * If l is {67,12,13,12} then after l.remove(12), l is {67,13}
    * If l is {1,2,3} then after l.remove(2), l is {1,3}
    * If l is {1,2,3} then after l.remove(99), l is {1,2,3}
    */
   public void remove (int value)
   {
   }

   /*
    * PURPOSE:
    *   Remove the element at position pos in the list.
    *
    * Note:
    *   In a list with 3 elements, the valid positions for removeAt are
    *   0, 1, 2.
    *
    * PRECONDITIONS:
    *   pos >= 0 and pos < l.size()
    *
    * Examples:
    *
    * If l is {1} and l.removeAt(0) returns, then l is {}.
    * If l is {1,2,3} and l.removeAt(1) returns, then l is {1,3}
    * If l is {1,2,3} and l.removeAt(2) returns, then l is {1,2}
    */
   public void removeAt (int pos)
   {
   }

   /*
    * PURPOSE:
    * Return a string representation of the list
    *
    * PRECONDITIONS:
    * None.
    *
    * Examples:
    * If l is {1,2,3,4} then l.toString() returns "{1,2,3,4}"
    * If l is {} then l.toString() returns "{}"
    *
    */
   public String toString()
   {
      return "Not implemented.";
   }
}

//IntegerNode.java//

public class IntegerNode
{
   IntegerNode    next;
   IntegerNode    prev;
   int       value;

   public IntegerNode()
   {
      next = null;
      value = 0;
   }

   public IntegerNode (int val)
   {
      value = val;
      next = null;
   }

   public IntegerNode (int val, IntegerNode nxt)
   {
      value = val;
      next = nxt;
   }

   public IntegerNode getNext()
   {
      return next;
   }

   public IntegerNode getPrev()
   {
      return prev;
   }

   public int getValue()
   {
      return value;
   }

   public void setNext (IntegerNode nxt)
   {
      next = nxt;
   }

   public void setPrev (IntegerNode prv)
   {
      prev = prv;
   }
   
   public void setValue (int val)
   {
      value = val;
   }  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
  • As you mentioned that code that only mentioned in IntegerLinkedList.java.
  • So i have implemented the all the function inside that file.
  • I have not change the IntegerList .java and integerNode.java, so you can use it as you uploaded in the question.

Source code:-

IntegerLinkedList.java

public class IntegerLinkedList implements IntegerList
{
   IntegerNode head;
   IntegerNode tail;
   int count;

   public IntegerLinkedList()
   {
       count = 0;
       tail = null;
       head = null;
   }

   /*
   * PURPOSE:
   *   Add the element x to the front of the list.
   *
   * PRECONDITIONS:
   *   None.
   *
   * Examples:
   *
   * If l is {1,2,3} and l.addFront(9) returns, then l is {9,1,2,3}.
   * If l is {} and l.addFront(3) returns, then l is {3}.
   */
   public void addFront (int x)
   {
       IntegerNode n = new IntegerNode(x);

       if(head == null) {
           tail = n;
       }else {
           head.prev = n;
           n.next = head;
       }
       head = n;
       count++;
   }


   /*
   * PURPOSE:
   *   Add the element x to the back of the list.
   *
   * PRECONDITIONS:
   *   None.
   *
   * Examples:
   *
   * If l is {1,2,3} and l.addBack(9) returns, then l is {1,2,3,9}.
   * If l is {} and l.addBack(9) returns, then l is {9}.
   */
   public void addBack (int x)
   {
       IntegerNode n = new IntegerNode(x);

       if(tail == null) {
           head = n;
       }else{
      
           tail.next = n;
           n.prev = tail;
       }
      
       tail = n;
       count++;
   }

   /*
   * PURPOSE:
   *   Add the element x at position pos in the list.
   *
   * Note:
   *   In a list with 3 elements, the valid positions for addAt are
   *   0, 1, 2, 3.
   *
   * PRECONDITIONS:
   *   pos >= 0 and pos <= l.size()
   *
   * Examples:
   *
   * If l is {} and l.addAt(9,0) returns, then l is {9}.
   * If l is {1} and l.addAt(9,0) returns, then l is {9,1}.
   * If l is {1,2} and l.addAt(9,1) returns, then l is {1,9,2}
   * If l is {1,2} and l.addAt(9,2) returns, then l is {1,2,9}
   */
   public void addAt (int x, int pos)
   {
       if(pos == 0) {
           this.addFront(x);
           return;
       }else if (pos == count) {
           this.addBack(x);
           return;
       }

       IntegerNode n = new IntegerNode(x);
       IntegerNode p = head;

       for(int i = 0; i < pos; i++) {
           p = p.next;
       }

       p.prev.next = n;
       n.prev = p.prev;

       p.prev = n;
       n.next = p;

       count++;
   }

   /*
   * PURPOSE:
   *   Return the number of elements in the list
   *
   * PRECONDITIONS:
   *   None.
   *
   * Examples:
   *   If l is {7,13,22} l.size() returns 3
   *   If l is {} l.size() returns 0
   */
   public int size()
   {
       return count;
   }

   /*
   * PURPOSE:
   *   Return the element at position pos in the list.
   *
   * PRECONDITIONS:
   *   pos >= 0 and pos < l.size()
   *
   * Examples:
   *   If l is {67,12,13} then l.get(0) returns 67
   *   If l is   {67,12,13} then l.get(2) returns 13
   *   If l is {92} then the result of l.get(2) is undefined.
   *
   */
   public int get (int pos)
   {
       IntegerNode p = head;

       for(int i = 0; i < pos; i++) {
           p = p.next;
       }

       return p.value;
   }

   /*
   * PURPOSE:
   *   Remove all elements from the list. After calling this
   *   method on a list l, l.size() will return 0
   *
   * PRECONDITIONS:
   *   None.
   *
   * Examples:
   *   If l is {67,12,13} then after l.clear(), l is {}
   *   If l is {} then after l.clear(), l is {}
   *
   */
   public void clear()
   {
       head = null;
       tail = null;
       count = 0;
   }

   /*
   * PURPOSE:
   *   Remove all instances of value from the list.
   *
   * PRECONDITIONS:
   *   None.
   *
   * Examples:
   *   If l is {67,12,13,12} then after l.remove(12), l is {67,13}
   *   If l is {1,2,3} then after l.remove(2), l is {1,3}
   *   If l is {1,2,3} then after l.remove(99), l is {1,2,3}
   */
   public void remove (int value)
   {
       IntegerNode p = head;
       for(int i = 0; i < count; i++) {
           if(p.value == value) {
               this.removeAt(i);
               i--;
           }
           p = p.next;
       }
   }

   /*
   * PURPOSE:
   *   Remove the element at position pos in the list.
   *
   * Note:
   *   In a list with 3 elements, the valid positions for removeAt are
   *   0, 1, 2.
   *
   * PRECONDITIONS:
   *   pos >= 0 and pos < l.size()
   *
   * Examples:
   *
   * If l is {1} and l.removeAt(0) returns, then l is {}.
   * If l is {1,2,3} and l.removeAt(1) returns, then l is {1,3}
   * If l is {1,2,3} and l.removeAt(2) returns, then l is {1,2}
   */
   public void removeAt (int pos)
   {
       if(count == 1) {
           this.clear();
           return;
       }

       if(pos == 0) {  

           head = head.next;
           head.prev = null;

       }else if(pos == count - 1) {

           tail = tail.prev;
           tail.next = null;

       }else {  
          
           IntegerNode p = head;
           for(int i = 0; i < pos; i++) {
               p = p.next;
           }
          
           p.prev.next = p.next;
           p.next.prev = p.prev;
       }

       count--;
   }

   /*
   * PURPOSE:
   *   Return a string representation of the list
   *
   * PRECONDITIONS:
   *   None.
   *
   * Examples:
   *   If l is {1,2,3,4} then l.toString() returns "{1,2,3,4}"
   *   If l is {} then l.toString() returns "{}"
   *
   */
   public String toString()
   {
       String s = "{";
       IntegerNode p = head;

       while(p != null) {
           s += p.value;
          
           if(p.next != null) {
               s += ",";
           }
          
           p = p.next;
       }

       return s + "}";
   }
}

Add a comment
Know the answer?
Add Answer to:
Code must only be written in IntegerLinkedList.java. 9 Methods need to be implemented so that all...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT...

    NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT WOULD CAUSE THE HW1.java TO PRINT THE RIGHT DATA.!!! The LinkedList class implements both the List interface and the Stack interface, but several methods (listed below) are missing bodies. Write the code so it works correctly. You should submit one file, LinkedList.java. Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class. None of your methods...

  • add/ remove any methods to the program. please post new code and output Min Heap: public...

    add/ remove any methods to the program. please post new code and output Min Heap: public class MinHeap { private int[] Heap; private int size; private int maxsize; private static final int FRONT = 1; public MinHeap(int maxsize) { this.maxsize = maxsize; this.size = 0; Heap = new int[this.maxsize + 1]; Heap[0] = Integer.MIN_VALUE; } private int parent(int pos) { return pos / 2; } private int leftChild(int pos) { return (2 * pos); } private int rightChild(int pos) {...

  • Create a Java code that includes all the methods from the Lecture slides following the ADTs...

    Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDE Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS 2) ArrayList ADT that uses a linked list internally (call it LArrayList) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....

  • Create a Java code that includes all the methods from the Lecture slides following the ADTs...

    Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDES Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS: 4) Queue ADT that uses a linked list internally (call it LQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....

  • The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function...

    The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function so that it is written as a value returning function, returning the required item. If the location of the item to be returned is out of range, use the assert function to terminate the program. note: please give all code in c++ below is the class and implementation(a test program would be helpful in determining how to use it): class arrayListType { public:    ...

  • Plz help me with the code. And here are the requirement. Thanks!! You are required to...

    Plz help me with the code. And here are the requirement. Thanks!! You are required to design and implement a circular list using provided class and interface. Please filling the blank in CircularList.java. This circular list has a tail node which points to the end of the list and a number indicating how many elements in the list. And fill out the blank of the code below. public class CircularList<T> implements ListInterface<T> { protected CLNode<T> tail; // tail node that...

  • Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time...

    Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...

  • Create a complete LinkedList class which implements all of the methods listed below using dynamic memory...

    Create a complete LinkedList class which implements all of the methods listed below using dynamic memory allocation. You will need a Node class to represent each node in the list. The list should store String values. Include a main method which tests all the methods of your list. Also answer these questions: What is the time complexity (using big-O notation) of these operations in your linked list? add(String val) add(int index, String val) get(int index) remove(String val) Submit three files:...

  • Modify Dlinkedlist.java so it includes a new method named getPriorNode that accepts the String value of...

    Modify Dlinkedlist.java so it includes a new method named getPriorNode that accepts the String value of a potential element in the doubly linked list and returns the string value of the predecessor node (or an appriopriate message String if there is no such node or predecessor. class DLinkedList { /** The Node class stores a list element and a reference to the next node. */ private class Node { String value; // Value of a list element Node next; //...

  • Complete an Array-Based implementation of the ADT List including a main method and show that the...

    Complete an Array-Based implementation of the ADT List including a main method and show that the ADT List works. Draw a class diagram of the ADT List __________________________________________ public interface IntegerListInterface{ public boolean isEmpty(); //Determines whether a list is empty. //Precondition: None. //Postcondition: Returns true if the list is empty, //otherwise returns false. //Throws: None. public int size(); // Determines the length of a list. // Precondition: None. // Postcondition: Returns the number of items in this IntegerList. //Throws: None....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT