Question

In: Computer Science

Write in Java! (Not Javascript) Consider the LinkedList class we discussed in class (see the slides...

Write in Java! (Not Javascript)

Consider the LinkedList class we discussed in class (see the slides for lecture 8). Add the following methods to the class and submit the completed LinkedList class.

  • int size() which returns the size of the linked list
  • Link getElementByIndex(int index) which returns the Link/node specified by the index. For example, getElementByIndex(0) should return the first Link, getElementByIndex(2) should return the third Link. If index is not in range, your method should return null.
  • boolean hasDuplicate() which returns true if the linked list contains duplicate data, returns false if there is no duplicate data in the list.
  • Link reverse() which reverses the linked list and returns the new head/first of the linked list (which is of type Link)

your file to be submitted to Gradescope should be named LinkedList.java. and here is the skeleton:

class Link

{

    public int data; // assuming integer data

    public Link next; // reference to the next Link

    //--------------------------------------------------------------

    // Constructor

    public Link(int data)

    {

        this.data = data;

        next = null;

    }

}

public class LinkedList

{

// implement this class

}

Solutions

Expert Solution

Sol: Here is the java code

class Link
{
    public int data; // assuming integer data
    public Link next; // reference to the next Link
    //--------------------------------------------------------------
    // Constructor
    public Link(int data)
    {
        this.data = data;
        next = null;
    }
}
public class LinkedList
{
    Link head;
// implement this class
    public int size() 
    { 
        Link temp = head; 
        int count = 0; 
        while (temp != null) 
        { 
            count++; 
            temp = temp.next; 
        } 
        return count; 
    } 
    public int getElementByIndex(int index) 
    { 
        Link current = head; 
        int count = 0; /* index of Node we are 
                          currently looking at */
        while (current != null) 
        { 
            if (count == index) 
                return current.data; 
            count++; 
            current = current.next; 
        } 
  
        /* if we get to this line, the caller was asking 
        for a non-existent element so we assert fail */
        assert(false); 
        return 0; 
    } 
    public boolean hasDuplicate() {
    int currentIndex = 0;
    for (Link current = head; current != null; current = current.next) {
        //with each node in list, compare it to all element of list
        int nodeIndex = 0;
        for (Link node = head; node != null; node = node. next) {
            if (currentIndex != nodeIndex && node.equals(current)) {
                return true;
            }
            nodeIndex++;
        }
        currentIndex++;
    }
    return false;
    }
    Link reverse() 
    { 
        Link prev = null; 
        Link current = head; 
        Link next = null; 
        while (current != null) { 
            next = current.next; 
            current.next = prev; 
            prev = current; 
            current = next; 
        } 
        node = prev; 
        return node; 
    }
}

Related Solutions

. Consider the two-firm game of sequential quantity competition that we studied in class. (See slides...
. Consider the two-firm game of sequential quantity competition that we studied in class. (See slides 33-38 in the “Homogeneous products oligopoly.”) Suppose firm A gets to make one more move after firm B makes its choice of QA, but before price and profits are determined. That is, the order of events will be: C(Qi) = 10Qi --> for firms B and A Demand --> P = 40 – 0.01(QA + QB ) i. Firm A announces QA. ii. Firm...
Use LinkedList build-in class (java.util.LinkedList) to write a Java program that has: A. Method to print...
Use LinkedList build-in class (java.util.LinkedList) to write a Java program that has: A. Method to print all elements of a linked list in order. B. Method to print all elements of a linked list in reverse order. C. Method to print all elements of a linked list in order starting from specific position. D. Method to join two linked lists into the first list in the parameters. E. Method to clone a linked list. The copy list has to be...
Write Java code for extending the LinkedList<E> class of java.util.* to ExtLinkedList<E> that would include the...
Write Java code for extending the LinkedList<E> class of java.util.* to ExtLinkedList<E> that would include the following method: public ExtLinkedList <E> mergeThreeLists (ExtLinkedList<E> list1, ExtLinkedList<E> list2) { } that returns an ExtLinkedList<E> which is the merged version of values from thislist, followed by values from list1, and then followed by values from list2.    For example, if E is Integer type and this listhas (5,3,1), list1has (8, 10,12,14), and list2has (22,23,24,25,26) in that order, then the returned list from a call to...
In java write a method that will take an array and change it into a linkedlist...
In java write a method that will take an array and change it into a linkedlist and then display it in the main method
1.Write Java code for extending the LinkedList<E> class of java.util.* to ExtLinkedList<E> that would include the...
1.Write Java code for extending the LinkedList<E> class of java.util.* to ExtLinkedList<E> that would include the following method: public ExtLinkedList <E> mergeThreeLists (ExtLinkedList<E> list1, ExtLinkedList<E> list2) { } that returns an ExtLinkedList<E> which is the merged version of values from this list, followed by values from list1, and then followed by values from list2. For example, if E is Integer type and this list has (5,3,1), list1 has (8, 10,12,14), and list2 has (22,23,24,25,26) in that order, then the returned...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
Consider the following definition of a doubly linked-list: class LinkedList{ public: LinkedList():head(0), tail(0){} ~LinkedList(); void reverse();...
Consider the following definition of a doubly linked-list: class LinkedList{ public: LinkedList():head(0), tail(0){} ~LinkedList(); void reverse(); //reverses the order of elements in the linked list void insert(int value); private: struct Node{ int data; Node* next; Node* prev; }; Node* head; Node* tail; //Add your helper function here that recursively reverses the order of elements in the linked list }; Write the declaration of a helper function in the class provided above that recursively reverses the order of elements in the...
Consider the following processes we've discussed in class. In which of these processes can we observe...
Consider the following processes we've discussed in class. In which of these processes can we observe evolutionary conflict? Group of answer choices A) multi-level selection B) sexual selection C) Coevolution D) Genetic drift A, B, and C A and B All of the above A, B, and D
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String new_word): Adds a linkedlist item at the end of the linkedlist print(): Prints all the words inside of the linkedlist length(): Returns an int with the length of items in the linkedlist remove(int index): removes item at specified index itemAt(int index): returns LinkedList item at the index in the linkedlist public class MyLinkedList { private String name; private MyLinkedList next; public MyLinkedList(String n) {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT