Question

if we have following List classes: public class LinkedList<T> { class ListNode { protected T value;...

if we have following List classes:

public class LinkedList<T> {

class ListNode {

protected T value;

protected ListNode next;

public ListNode(T val, ListNode nxt) {

value = val;

next = nxt;

}

public ListNode(T val) {

this(val, null);

}

public ListNode() {

this(null, null);

}

}

can you write the folowing methods in java:

1.Write a method for the LinkedList class called int indexOf(T val) which returns the integer index indicating the location of val in the list, or -1 if val is not in the list.

2. Write a method for the LinkedList class called boolean equals(LinkedList rhs) which checks for deep equality, i.e. checks if rhs and the receiving object store the same values in the same order.

3. Assume that the method described in Problem 3 does not exist. Write a method boolean equals(LinkedList lhs, LinkedList rhs) that uses two ListIterators to check for deep equality of lists lhs and rhs. This code should NOT be part of the ListIterator class.

4. Write a recursive method void printReverse(Node node) which prints the values in the list referenced by node in reverse. You may assume that node references a valid node in the list, i.e. not the dummy header node. HINT: this method should only be 3 or 4 lines long.

5. Write a method for the LinkedList class called boolean removeLast(T val) which removes the last occurrence of val from the list. The method should return true if it is successful.

6. Repeat the previous problem using a doubly linked list.

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

public class LinkedList<T> {

  

   ListNode head;

   class ListNode {

       protected T value;

       protected ListNode next;

       public ListNode(T val, ListNode nxt) {

           value = val;

           next = nxt;

       }

       public ListNode(T val) {

           this(val, null);

       }

       public ListNode() {

           this(null, null);

       }

   }

   int indexOf(T val) {

       ListNode curr = head;

       int index = -1;

       while(curr!=null){

           if(curr.value.equals(val)){

               ++index;

               return index;

           }

       }

       return index;

   }

   boolean equals(LinkedList rhs){

       ListNode curr1 = this.head;

       ListNode curr2 = rhs.head;

       if(curr1 ==null && curr2==null)

           return true;

       if(curr1 ==null || curr2==null)

           return false;

      

       while(curr1!=null && curr2!=null){

           if(!curr1.value.equals(curr2.value))

               return false;

           curr1 = curr1.next;

           curr2 = curr2.next;

       }

       if(curr1 !=null || curr2!=null)

           return false;

      

       return true;

      

   }

}

Add a comment
Know the answer?
Add Answer to:
if we have following List classes: public class LinkedList<T> { class ListNode { protected T value;...
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; /**...

  • I have the following c++ data structures assignment: Copy Constructors, Destructors, and Assignment Operators An understanding...

    I have the following c++ data structures assignment: Copy Constructors, Destructors, and Assignment Operators An understanding of how to implement copy constructors, destructors, and assignment operators is essential when working with data structures using dynamic memory allocation. Failure to implement these methods correctly can and probably will result in memory leaks. In this project, you are provided with a working implementation of a doubly-linked list in which the copy constructor, destructor, and assignment operator methods are not complete. To complete...

  • Due in25 minutes,Java8 programming linkedlist node Given the following definition for a Node class: public class...

    Due in25 minutes,Java8 programming linkedlist node Given the following definition for a Node class: public class Node<T extends Comparable<T>>{ public T val; public Node<T> prev; public Node<T> next; } Create an add method for a Sorted Linked Set. The collection is stored as a linked list, however has a few extra properties. First, as it is a Set, values stored in the list are distinct. Second, the list is sorted (so if 4, 2, 3, 1 were added, they would...

  • JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList...

    JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList Class (some methods included). Remember, you will use the LinkedList Class that we developed in class not Java’s LinkedList Class. You will add the following method to the LinkedList Class: printEvenNodes – this is a void method that prints Nodes that have even indices (e.g., 0, 2, 4, etc). Create a LinkedListDemo class. Use a Scanner Class to read in city names and store...

  • C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr...

    C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr in addition to a headPtr along with methods to get, set, insert and remove values at either end of the list. Call these getFirst, getLast, setFirst, setLast, insertFirst, insertLast, removeFirst, removeLast. Don't forget, you also need a copy constructor and destructor plus getLength, isEmpty and clear methods. Overload the stream insertion operator as a friend function which outputs the list in format { 1,...

  • Create a nested inner class inside a file called LinkedList.java called the ListIterator. ListIterator: This public...

    Create a nested inner class inside a file called LinkedList.java called the ListIterator. ListIterator: This public class is nested within an LinkedList data structure and allows us to traverse the elements in any collection, access any data element and remove any data elements of the collection. It also adds the functionality to move forward and backward, which is new functionality. We are going to build this data structure from scratch. Instance variables (fields) are: mPrevious (Node) – the previous node...

  • Suppose we implement a doubly linked list class template LinkedList with template type T. LinkedList has...

    Suppose we implement a doubly linked list class template LinkedList with template type T. LinkedList has fields Node *headPtr, Node *tailPtr and int length, where the struct type Node has fields prev and next of type Node* along with data of type T. The prev and next pointers of each Node points to the previous and next Nodes in the list (or are respectively null in the case of the list’s head or tail node). We wish to detect "invalid"...

  • When compiling the LinkedList and Iterator class, the following error is being produced: Note: LinkedList.java uses...

    When compiling the LinkedList and Iterator class, the following error is being produced: Note: LinkedList.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. Any suggestions? public class LinkedList<T> {    Node<T> itsFirstNode;    Node<T> itsLastNode;    private int size;    public LinkedList() {        itsFirstNode = null;        itsLastNode = null;          size = 0;    }    public Iterator<T> getIterator() {        return new Iterator(this);    }    // THIS WILL NEED...

  • In Java You may add any classes or methods to the following as you see fit in order to complete t...

    In Java You may add any classes or methods to the following as you see fit in order to complete the given tasks. Modify the LinkedList (or DoubleLinkedList) class and add a method append. append should take another LinkedList (DoubleLinkedList) as input and append that list to the end of this list. The append method should work by doing a few "arrow" adjustments on the boxes and it should not loop through the input list to add elements one at...

  • //LinkedList import java.util.Scanner; public class PoD {    public static void main( String [] args )...

    //LinkedList import java.util.Scanner; public class PoD {    public static void main( String [] args ) { Scanner in = new Scanner( System.in ); LinkedList teamList = new LinkedList(); final int TEAM_SIZE = Integer.valueOf(in.nextLine()); for (int i=0; i<TEAM_SIZE; i++) { String newTeamMember = in.nextLine(); teamList.append(newTeamMember); } while (in.hasNext()) { String removeMember = in.nextLine(); teamList.remove(removeMember); }    System.out.println("FINAL TEAM:"); System.out.println(teamList); in.close(); System.out.print("END OF OUTPUT"); } } =========================================================================================== //PoD import java.util.NoSuchElementException; /** * A listnked list is a sequence of nodes with...

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