Question


Home- WebAdvisor LINKED Pass-thru for Ent X online.campuscommerce.c X Welcome, Jenna mimir.io/projects/ea5ca60b-5a91-4e8a-823

Edipsé IDE P. TestLinkedListjava LinkedListjava Nodejava 1e/** Bauthor 2 3 Implements a double-linked list with four errors 4

TestLinkedListjava LinkedListjava Nodejava 34 35 // Empties the list public void clear ( ) 36 37e 38 head.setNext (tail); ta

1 Node java TestLinkedList.java inked Listjava // Return the item at the given index return node.get Item ( ) ; } // Removes

- Eclipse IDE T LinkedListjava Node java TestLinkedListjava Node<E> node head.getNext (); 07 08 // Traverse the list looking

kedListjava Node.java TestLinkedListjava // Insert after the node we newNode.setNext (node. getNext() ); newNode.setPrev (nod

Node.java TestLinkedListjava b 7 public class Node<T> 8 f // The item stored in the node 1/ This cannot be modified once set

this.item item; this .next = next; this.prev prev; public T getItem () { return item; } public Node<T> getNext () { return ne

starter code

Node.java TestLinkedList.java 1ep. eauthor 3 * 2 * 4 5 public class TestLinkedList 6 f 7e public static void main(String [ ]

To write a program using the starter code which is TestLinkedList to see if the LinkedList program has bugs. It will produce ether a pass or fail.More information is in the first two pictures.

LinkedList.java

/**
* @author someone
*
* Implements a double-linked list with four errors
*/

public class LinkedList<E>
{
// The first and last nodes in the list
private Node<E> head, tail;
// Number of items stored in the list
private int size;

// Create an empty linked list
public LinkedList()
{
// Create empty head and tail nodes to mark boundaries of list
head = new Node<E>(null);
tail = new Node<E>(null, null, head);
head.setNext(tail);
size = 0;
}

// Return whether there are any items in the list
public boolean isEmpty()
{
return size == 0;
}

// Return the number of items in the list
public int size()
{
return size;
}

// Empties the list
public void clear()
{
head.setNext(tail);
tail.setPrev(head);
size = 0;
}

// Adds a new item to the end of the list
public void add(E item)
{
// Create a new node between tail and the node before tail
Node<E> node = new Node<E>(item, tail, tail.getPrev());
tail.getPrev().setNext(node);
tail.setPrev(node);
// Update the size
size++;
}

// Return the item at the given index
public E get(int index)
{
// Return null if the index is out of bounds
if (index < 0 || index >= size)
{
return null;
}

Node<E> node = head.getNext();

// Traverse the list until we reach the given index
for (int i = 0; i < index; i++)
{
node = node.getNext();
}

// Return the item at the given index
return node.getItem();
}

// Removes an item from the list and returns whether an item was removed
public boolean remove(E item)
{
Node<E> node = head.getNext();

// Traverse the list until we reach the end or find item
for (int i = 0; i < size; i++)
{
// Remove the item when we find it
if (node.getItem().equals(item))
{
// Remove any references to the removed node
node.setNext(null);
node.setPrev(null);

// Update the size
size--;

return true;
}

// Move to the next node
node = node.getNext();
}

return false;
}

// Returns the index of item, or -1 if it's not in the list
public int indexOf(E item)
{
Node<E> node = head.getNext();

// Traverse the list looking for our item
for (int i = 0; i < size; i++)
{
if (node.getItem().equals(item)) return i;
}

return -1;
}

// Add a node to the list at the given index
public boolean insert(int index, E item)
{
// Return false if the index is out of bounds, but allow insertions at the end
// of the list (index == size)
if (index < 0 || index > size)
{
return false;
}

Node<E> newNode = new Node<E>(item);

// Find the node before index i
Node<E> node = head;
for (int i = 0; i < size; i++)
{
node = node.getNext();
}

// Insert after the node we just found
newNode.setNext(node.getNext());
newNode.setPrev(node);
node.getNext().setPrev(newNode);
node.setNext(newNode);

// Update the size
size++;

return true;
}

// Reverse the order of the list
public void reverse()
{
// If there are fewer than 2 items, we don't need to do anything
if (size >= 2)
{
// Two node pointers, will move through the list and swap items
Node<E> front = head.getNext(), back = tail.getPrev();

// Go halfway through the list from each end, swapping items in each pair of nodes
for (int i = 0; i < size / 2; i++)
{
// Swap items in front/back nodes
// ERROR: temp = back.getItem(), will swap incorrectly and overwrite list
E temp = back.getItem();
front.setItem(back.getItem());
back.setItem(temp);
  
// Move front/back pointers
front = front.getNext();
back = back.getPrev();
}
}
}
}

Node.java

/**
* @author Zach
*
* Simple node class with a next and previous pointer
*/

public class Node<T>
{
   // The item stored in the node
   // This cannot be modified once set
   private T item;
   // The nodes linked before and after this node
   private Node<T> next, prev;

   // Create a node connected to no other nodes
   public Node(T item)
   {
       this(item, null, null);
   }

   // Create a node with the give next and prev nodes
   public Node(T item, Node<T> next, Node<T> prev)
   {
       this.item = item;
       this.next = next;
       this.prev = prev;
   }

public T getItem()
{
return item;
}

   public Node<T> getNext()
   {
       return next;
   }

   public Node<T> getPrev()
   {
       return prev;
   }

public void setItem(T item)
{
this.item = item;
}

   public void setNext(Node<T> next)
   {
       this.next = next;
   }

   public void setPrev(Node<T> prev)
   {
       this.prev = prev;
   }
}

TestLinkedList.java (Starter Code which should be the only code being changed)

/**
* @author
*/

public class TestLinkedList
{
public static void main(String[] args)
{
System.out.println("Test IndexOf: " + (testIndexOf() ? "pass" : "fail"));
System.out.println("Test Insert: " + (testInsert() ? "pass" : "fail"));
System.out.println("Test Remove: " + (testRemove() ? "pass" : "fail"));
System.out.println("Test Reverse: " + (testReverse() ? "pass" : "fail"));
}

public static boolean testIndexOf()
{
return false;
}

public static boolean testInsert()
{
return false;
}

public static boolean testRemove()
{
return false;
}

public static boolean testReverse()
{
return false;
}
}

Home- WebAdvisor LINKED Pass-thru for Ent X online.campuscommerce.c X Welcome, Jenna mimir.io/projects/ea5ca60b-5a91-4e8a-8237-a870e2ae2ba3 your partner] and collaborators (other classmates who provided significant assistancel. A collaborator may not write code for you or allow you to copy or use their own code as a reference. Use Mimir's group feature to create a group before submitting (see this page for directions) Description For this assignment, you will be both debugging and testing a linked list. This will be similar to the queue testing assignment, but with the additional step of identifying and fixing the bugs The starter code includes three classes: TestLinked List, Linked List, and Node. Node is a generic data type that stores a single value and can be connected to two other Nodes, it is more or less identical to the Link example in section 10.03. The Linked List is similar to the example from chapter 10, but this implementation is a doubly linked list. It sill uses the header/trailer node model described in 10.01, but has a different set of methods. Below is a brief summary of these methods: isEmpty(): returns whether the list is empty size(): returns the number of items in the list clear ) removes all items from the list add(): adds a new item to the end of the list get(): returns the item at the given index remove (): removes an item from the list indexof () returns the index of an item, or -1if it's not in the list insert): adds an item to the list at the given index reverse(): reverses the order of the entire list LinkedList.java contains four errors, one in each of the following methods: removel), indexOf], insert(], and reversel). Each bug is relatively small, and only involves one or two lines of code ethe test to detect that bug. The TestLinked List class contains a test Once you have fixed a bug, you can write method for each bug, as well as a main method thet runs all four tests. This part will be similar to the previous testing lab. Keep in mind the following when writing your tests Each test should return true if the method passes (the bug has been fixed] and false if the method contains the bug. A test should not crash the program. If your test detects the bug by triggering an exception, the exception should be caught by the test You can assume thet the isEmptyl, size(), clear, addl), and getl] methods don't contain bugs. Use them when writing your tests and avoid the other methods (except for the one you're testing) Grading Mimir will grade your program by running two tests for each of the bugs in the starter code. These tests will check that you have 1 Fixed the bug (5 points] 2 Written a test that detects the bug (5 points Mimir checks your tests by running it with the starter code (which should fail and return falsel, your code (which should pass and return true) and another version of the list with the bugs removed (which should also pass). Mimir will show the results of testing each program
Edipsé IDE P. TestLinkedListjava LinkedListjava Nodejava 1e/** Bauthor 2 3 Implements a double-linked list with four errors 4 6 7 public class LinkedList size) return null; 4 Node node = head.getNext(); 5 // Traverse the list until we reach the given index for (int i e; i
0 0
Add a comment Improve this question Transcribed image text
Answer #1

NOTE
#######

The below program works fine and tested . Please go through the function to understand the change. The test class is changed for better understanding . Please comment in case of any issue in the LinkedList functions

//######################### PGM START #########################################

class Node<T>
{
   // The item stored in the node
   // This cannot be modified once set
   private T item;
   // The nodes linked before and after this node
   private Node<T> next, prev;

   // Create a node connected to no other nodes
   public Node(T item){
       this(item, null, null);
   }

   // Create a node with the give next and prev nodes
   public Node(T item, Node<T> next, Node<T> prev){
       this.item = item;
       this.next = next;
       this.prev = prev;
   }

   public T getItem(){  
       return item;
   }
   public Node<T> getNext(){
       return next;
   }

   public Node<T> getPrev(){
       return prev;
   }
   public void setItem(T item){
       this.item = item;
   }
   public void setNext(Node<T> next) {
       this.next = next;
   }
   public void setPrev(Node<T> prev){
       this.prev = prev;
   }
}

class LinkedList<E>{
  
   // The first and last nodes in the list
   private Node<E> head, tail;
   // Number of items stored in the list
   private int size;

   // Create an empty linked list
   public LinkedList(){
       // Create empty head and tail nodes to mark boundaries of list
       head = new Node<E>(null);
       tail = new Node<E>(null, null, head);
       head.setNext(tail);
       size = 0;
   }

   // Return whether there are any items in the list
   public boolean isEmpty(){
       return size == 0;
   }

   // Return the number of items in the list
   public int size(){  
       return size;
   }

   // Empties the list
   public void clear(){
       head.setNext(tail);
       tail.setPrev(head);
       size = 0;
   }

   // Adds a new item to the end of the list
   public void add(E item){
       // Create a new node between tail and the node before tail
       Node<E> node = new Node<E>(item, tail, tail.getPrev());
       tail.getPrev().setNext(node);
       tail.setPrev(node);

       // Update the size
       size++;
   }

   // Return the item at the given index
   public E get(int index){
       // Return null if the index is out of bounds
       if (index < 0 || index >= size){
           return null;
       }

       Node<E> node = head.getNext();

       // Traverse the list until we reach the given index
       for (int i = 0; i < index; i++){
           node = node.getNext();
       }

       // Return the item at the given index
       return node.getItem();
   }

   // Removes an item from the list and returns whether an item was removed
   public boolean remove(E item){
       Node<E> node = head.getNext();

       // Traverse the list until we reach the end or find item
       for (int i = 0; i < size; i++){
           // Remove the item when we find it
           if (node.getItem().equals(item)){
               // Remove any references to the removed node
               node.getNext().setPrev(node.getPrev());
               node.getPrev().setNext(node.getNext());
              
               node.setNext(null);
               node.setPrev(null);
               // Update the size
               size--;
               return true;
           }

           // Move to the next node
           node = node.getNext();
       }

       return false;
   }

   // Returns the index of item, or -1 if it's not in the list
   public int indexOf(E item){
       Node<E> node = head.getNext();

       // Traverse the list looking for our item
       for (int i = 0; i < size; i++){
           if (node.getItem().equals(item)) return i;
           node=node.getNext();
       }
       return -1;
   }

   // Add a node to the list at the given index
   public boolean insert(int index, E item){
       // Return false if the index is out of bounds, but allow insertions at the end
       // of the list (index == size)
       if (index < 0 || index > size){
           return false;
       }
       Node<E> newNode = new Node<E>(item);

       // Find the node before index i
       Node<E> node = head;
       for (int i = 0; i < index; i++){
           node = node.getNext();
       }

       // Insert after the node we just found
       newNode.setNext(node.getNext());
       newNode.setPrev(node);
       node.getNext().setPrev(newNode);
       node.setNext(newNode);

       // Update the size
       size++;

       return true;
   }

   // Reverse the order of the list
   public void reverse(){
       // If there are fewer than 2 items, we don't need to do anything
       if (size >= 2){
           // Two node pointers, will move through the list and swap items
           Node<E> front = head.getNext(), back = tail.getPrev();

           // Go halfway through the list from each end, swapping items in each pair of nodes
           for (int i = 0; i < size / 2; i++){
               // Swap items in front/back nodes
               // ERROR: temp = back.getItem(), will swap incorrectly and overwrite list
               E temp = back.getItem();
               back.setItem(front.getItem());
               front.setItem(temp);
              

               // Move front/back pointers
               front = front.getNext();
               back = back.getPrev();
           }
       }
   }
   public void display() {
       Node<E> node = head.getNext();
       for(int i=0;i<size;i++) {
          
           System.out.print(node.getItem()+" ");
           node=node.getNext();
       }
       System.out.println();
   }
}

public class TestLinkedList
{
   public static void main(String[] args){
       LinkedList<Integer> l1=new LinkedList<Integer>();
       l1.add(10);
       l1.add(20);
       l1.add(30);
       System.out.println("Initial list.......");
       l1.display();
       System.out.println("\nReverse of the list.....");
       l1.reverse();
       l1.display();
       System.out.println("\nRemoving 20 from list");
       System.out.println(l1.remove(20));
       l1.display();
       l1.insert(1, 70);
       System.out.println("\nAfter insertion of 70 at index 1");
       l1.display();
       /*
       System.out.println("Test IndexOf: " + (testIndexOf() ? "pass" : "fail"));
       System.out.println("Test Insert: " + (testInsert() ? "pass" : "fail"));
       System.out.println("Test Remove: " + (testRemove() ? "pass" : "fail"));
       System.out.println("Test Reverse: " + (testReverse() ? "pass" : "fail"));
       */
   }
   public static boolean testIndexOf(){
       return false;
   }
   public static boolean testInsert(){
       return false;
   }
   public static boolean testRemove(){  
       return false;
   }
   public static boolean testReverse(){
       return false;
   }
}

//############################ PGM END ##############################

OUTPUT
##############
<terminated> TestLinkedList Java Application] CAProgram Files Javaljdk-11 Initial list.... 10 20 30 Reverse of the list.....

Add a comment
Know the answer?
Add Answer to:
starter code To write a program using the starter code which is TestLinkedList to see if...
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
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
ADVERTISEMENT