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
  • Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to...

    Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)...

    Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)    {        this.size = size;    }    /** Reference to list head. */    private Node<E> head = null;    /** The number of items in the list */    private int size = 0;       /** Add an item to the front of the list.    @param item The item to be added    */    public void addFirst(E...

  • Question: SWAPPING NODES IN A SINGULARLY LINKED LIST: I am attempting to create a program that...

    Question: SWAPPING NODES IN A SINGULARLY LINKED LIST: I am attempting to create a program that swaps 2 nodes (no matter where they are in the list) and am having some difficulty. I can't seem to figure out why this swap method is throwing an error. Code: (SWAPPING METHOD AND TEST LINE BOLDED) package linkedlists; public class SinglyLinkedList<E> implements Cloneable { //---------------- nested Node class ---------------- /** * Node of a singly linked list, which stores a reference to its...

  • Improve the speed of public T get(int i) by having it work backward from the end...

    Improve the speed of public T get(int i) by having it work backward from the end of the array if you attempt to get a member which is closer to the end than the start. This improvement will be tested through timing tests on large lists. The method should still return null if i is not a valid index. Code import java.util.Iterator; public class DLList<T> implements Iterable<T> {    private static class Node<T> {        public Node<T> prev, next;...

  • Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(),...

    Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(), peek(), size(), isEmpty(). my linkedlist class is: public class Lab8_problem1 {    private Node head, tail; private int size;    public Lab8_problem1() { this.head = null; this.tail = null; this.size = 0; } //Insert a node at specifc Location public void insertAt(int value, int index) { Node newNode = new Node(); newNode.data = value; if(head == null){ head = newNode; tail = newNode; head.next...

  • C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public:...

    C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public: Node(int val); int value; Node* next; }; Node::Node(int val){ value = val; } class List { public: List(); // Uncomment the line below once you're ready List(List &other); void push_front(int value); bool pop_front(int &value); void push_back(int value); bool pop_back(int &value); int at(int index); void insert_at(int index, int value); void remove_at(int index); int size(); private: // other members you may have used Node* head; Node*...

  • Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface:...

    Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface: /** * An ordered list of items. */ public interface ItemList<E> {      /**       * Append an item to the end of the list       *       * @param item – item to be appended       */ public void append(E item);      /**       * Insert an item at a specified index position       *       * @param item – item to be...

  • C++: I need implement this code using Double Linked List using the cosiderations 1. head point...

    C++: I need implement this code using Double Linked List using the cosiderations 1. head point to null in an empty list 2. There is not need of a tail pointer /*This class implements the singly linked list using templates Each list has two attributes:    -head: first node in the list    -tail: last node in the list #include "circDLLNode.h" template class { public:    //Default constructor: creates an empty list    ();    //Destructor: deallocate memory    ~();  ...

  • Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given reference...

    Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same node, etc. Write the main method to test the swapNodes method. You may need to traverse the list. package linkedlists; public class SinglyLinkedList<E> implements Cloneable {    // ---------------- nested Node class...

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