Question


Complete with javadoc comments for constructor and setter/getter methods

Overview The purpose of this lab is to implement a generic Singly Linked Lists using a generic Node UML for Node<E> and Singl

Grading Rubric Node<E> (44 Points) Comment with name and each method/constructor documented in javadoc format (7 points) 2 Fi

ample Run of TestSinglyLinkedList.java: run: CarList upon startup: [-Empty Appending: Appending: Size: 0 [2017 Honda Accord]

(2016 Camry - 0, (2017 Explorer -0), (2016 Zoom Zoom 0), (2017 Acura -0), (2019 Kia Soul -0) ] Size: 9 Search for cars - shou

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Node.java : Java program to define a class to represent a generic Node of SinglyLinkedList

public class Node<E> {

      

       private E data;

       private Node<E> next;

      

       /**

       * Constructor to define a Node

       * @param initialData the data part of the node

       * @param initialNext the link to the next node

       */

       public Node(E initialData, Node<E> initialNext)

       {

             data = initialData;

             next = initialNext;

       }

      

       /**

       * method to return the data of the node

       * @return the generic data type

       */

       public E getData()

       {

             return data;

       }

      

       /**

       * method to return the link to the next node

       * @return reference to Node

       */

       public Node<E> getNext()

       {

             return next;

       }

      

       /**

       * method to set the data of this node

       * @param newData is the data of the node to be set

       */

       public void setData(E newData)

       {

             data = newData;

       }

       /**

       * method to set the next reference of Node

       * @param newNext is the next part of the node to be set

       */

       public void setNext(Node<E> newNext)

       {

             next = newNext;

       }

}

//end of Node.java

//SinglyLinkedList.java : Java class to represent SinglyLinkedList

import java.util.ArrayList;

import java.util.List;

public class SinglyLinkedList<E> {

      

       private Node<E> head;

       private Node<E> tail;

       private int numElements;

      

       /**

       * Constructor to define an empty linked list

       */

       public SinglyLinkedList()

       {

             head = null;

             tail = null;

             numElements = 0;

       }

      

       /**

       * method to return the the size of the linked list

       * @return number of elements in the linked list

       */

       public int getSize()

       {

             return numElements;

       }

      

       /**

       * method to insert the element at the end of list

       * @param newElement is the element to be inserted

       */

       public void appendList(E newElement)

       {

             Node<E> node = new Node<E>(newElement,null);

             if(head == null)

             {

                    head = node;

                    tail = node;

             }else

             {

                    tail.setNext(node);

                    tail = node;

             }

            

             numElements++;

       }

      

       /**

       * method to insert the element at the start of the list

       * @param newElement is the element to be inserted

       */

       public void prependList(E newElement)

       {

             Node<E> node = new Node<E>(newElement,head);

             if(head == null)

             {

                    head = node;

                    tail = node;

             }else

             {

                    head = node;

             }

            

             numElements++;

       }

      

       /**

       * method to return if target exists in the list or not

       * @param target is the element to search

       * @return true if the element exists else false

       */

       public boolean exists(E target)

       {

             Node<E> curr = head; // set curr to head

             // loop till the end of the list

             while(curr != null)

             {

                    if(curr.getData().equals(target)) // check if data in curr is equal to the element to be searched

                           return true;

                    curr = curr.getNext();

             }

            

             return false; // element not found

       }

      

       /**

       * method to return the occurrences if target in list

       * @param target the element to be searched

       * @return the number of times target appears in list

       */

       public int countOccurences(E target)

       {

             Node<E> curr = head; // set curr to head

             int count = 0;

             // loop till the end of the list

             while(curr != null)

             {

                    if(curr.getData().equals(target))

                           count++; // increment the count if data in curr is equal to target

                    curr = curr.getNext();

             }

            

             return count; // return the count

       }

      

       /**

       * method to delete the first occurrence of target from list

       * @param target the element to be deleted

       * @return true if the element is deleted successfully else false

       */

       public boolean remove(E target)

       {

             if(head.getData().equals(target)) //check if target is present in head

             {

                    head = head.getNext();

                    numElements--;

                    return true;

             }else

             {

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

                    Node<E> prev = head;

                    // loop till the end of list

                    while(curr != null)

                    {

                           if(curr.getData().equals(target)) // if data in curr is equal to target, delete the node and return false

                           {

                                 prev.setNext(curr.getNext()); // set the next of prev to next of curr

                                 if(curr.getNext() == null)

                                        tail = prev;

                                 numElements--; // decrement the number of elements

                                 return true;

                           }

                          

                           prev = curr;

                           curr = curr.getNext();

                    }

             }

            

             return false; // target not present

       }

      

       /**

       * method to return the list of data in the linked list

       * @return null if linked list is empty else list of data in linked list

       */

       public List<E> iterator()

       {

             if(head == null) // list is empty

             {

                    return null;

             }else

             {

                    List<E> list = new ArrayList<E>();

                    Node<E> curr = head; // set curr to head

                    // loop till the end of list

                    while(curr != null)

                    {

                           list.add(curr.getData()); // add the data to list

                           curr = curr.getNext();

                    }

                   

                    return list; // return the list of data

             }

       }

             

}

//end of SinglyLinkedList.java

Extra Credit :

//Node.java : Java class to update Node to represent doubly linked list node

public class Node<E> {

      

       private E data;

       private Node<E> next;

       private Node<E> prev;

      

       /**

       * Constructor to define a Node

       * @param initialData the data part of the node

       * @param initialNext the link to the next node

       */

       public Node(E initialData, Node<E> initialNext)

       {

             data = initialData;

             next = initialNext;

       }

      

       /**

       * method to return the data of the node

       * @return the generic data type

       */

       public E getData()

       {

             return data;

       }

      

       /**

       * method to return the link to the next node

       * @return reference to Node

       */

       public Node<E> getNext()

       {

             return next;

       }

      

       /**

       * method to set the data of this node

       * @param newData is the data of the node to be set

       */

       public void setData(E newData)

       {

             data = newData;

       }

       /**

       * method to set the next reference of Node

       * @param newNext is the next part of the node to be set

       */

       public void setNext(Node<E> newNext)

       {

             next = newNext;

       }

      

       /**

       * method to set the prev reference of Node

       * @param newPrev is the prev part of the node to be set

       */

       public void setPrev(Node<E> newPrev)

       {

             prev = newPrev;

       }

      

       /**

       * method to return the link to the previous node

       * @return reference to Node

       */

       public Node<E> getPrev()

       {

             return prev;

       }

}

// end of Node.java

//SinglyLinkedList.java : Update it to represent doubly linked list

import java.util.ArrayList;

import java.util.List;

public class SinglyLinkedList<E> {

      

       private Node<E> head;

       private Node<E> tail;

       private int numElements;

      

       /**

       * Constructor to define an empty linked list

       */

       public SinglyLinkedList()

       {

             head = null;

             tail = null;

             numElements = 0;

       }

      

       /**

       * method to return the the size of the linked list

       * @return number of elements in the linked list

       */

       public int getSize()

       {

             return numElements;

       }

      

       /**

       * method to insert the element at the end of list

       * @param newElement is the element to be inserted

       */

       public void appendList(E newElement)

       {

             Node<E> node = new Node<E>(newElement,null);

             node.setPrev(tail);

             if(head == null)

             {

                    head = node;

                    tail = node;

             }else

             {

                    tail.setNext(node);

                    tail = node;

             }

            

             numElements++;

       }

      

       /**

       * method to insert the element at the start of the list

       * @param newElement is the element to be inserted

       */

       public void prependList(E newElement)

       {

             Node<E> node = new Node<E>(newElement,head);

             node.setPrev(null);

             if(head == null)

             {

                    head = node;

                    tail = node;

             }else

             {

                    head.setPrev(node);

                    head = node;

             }

            

             numElements++;

       }

      

       /**

       * method to return if target exists in the list or not

       * @param target is the element to search

       * @return true if the element exists else false

       */

       public boolean exists(E target)

       {

             Node<E> curr = head; // set curr to head

             // loop till the end of the list

             while(curr != null)

             {

                    if(curr.getData().equals(target)) // check if data in curr is equal to the element to be searched

                           return true;

                    curr = curr.getNext();

             }

            

             return false; // element not found

       }

      

       /**

       * method to return the occurrences if target in list

       * @param target the element to be searched

       * @return the number of times target appears in list

       */

       public int countOccurences(E target)

       {

             Node<E> curr = head; // set curr to head

             int count = 0;

             // loop till the end of the list

             while(curr != null)

             {

                    if(curr.getData().equals(target))

                           count++; // increment the count if data in curr is equal to target

                    curr = curr.getNext();

             }

            

             return count; // return the count

       }

      

       /**

       * method to delete the first occurrence of target from list

       * @param target the element to be deleted

       * @return true if the element is deleted successfully else false

       */

       public boolean remove(E target)

       {

             if(head.getData().equals(target)) //check if target is present in head

             {

                    head = head.getNext();

                    if(head != null)

                           head.setPrev(null);

                    numElements--;

                    return true;

             }else

             {

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

                    // loop till the end of list

                    while(curr != null)

                    {

                           if(curr.getData().equals(target)) // if data in curr is equal to target, delete the node and return false

                           {

                   

                                 curr.getPrev().setNext(curr.getNext());

                                 if(curr.getNext() == null)

                                        tail = curr.getPrev();

                                 else

                                        curr.getNext().setPrev(curr.getPrev());

                                 numElements--; // decrement the number of elements

                                 return true;

                           }

                          

                           curr = curr.getNext();

                    }

             }

            

             return false; // target not present

       }

      

       /**

       * method to return the list of data in the linked list

       * @return null if linked list is empty else list of data in linked list

       */

       public List<E> iterator()

       {

             if(head == null) // list is empty

             {

                    return null;

             }else

             {

                    List<E> list = new ArrayList<E>();

                    Node<E> curr = head; // set curr to head

                    // loop till the end of list

                    while(curr != null)

                    {

                           list.add(curr.getData()); // add the data to list

                           curr = curr.getNext();

                    }

                   

                    return list; // return the list of data

             }

       }

      

       /**

       * method to return a list that contains data in reverse order i.e from tail to head

       * @return null if linked list is empty else list of data in linked list

       */

       public List<E> iteratorReverse()

       {

             if(head == null) // list is empty

             {

                    return null;

             }else

             {

                    List<E> list = new ArrayList<E>();

                    Node<E> curr = tail; // set curr to tail

                    // loop till the start of list

                    while(curr != null)

                    {

                           list.add(curr.getData()); // add the data to list

                           curr = curr.getPrev();

                    }

                   

                    return list; // return the list of data

             }

       }

}

//end of SinglyLinkedList.java

Add a comment
Know the answer?
Add Answer to:
Complete with javadoc comments for constructor and setter/getter methods Overview The purpose of this lab is...
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