Question

In: Computer Science

myLinkedList.java>>>>>>>> public class MyLinkedList implements MiniList<Integer>{ /* Private member variables that you need to declare: **...

myLinkedList.java>>>>>>>>

public class MyLinkedList implements MiniList<Integer>{
/* Private member variables that you need to declare:
** The head pointer
** The tail pointer
*/

public class Node {
// declare member variables (data and next)

// finish these constructors
public Node(int data, Node next) {}
public Node(int data) {} // HINT: use this() with next = null
}

// Initialize the linked list (set head and tail pointers)
public MyLinkedList() {}

@Override
public boolean add(Integer item) {
return false;
}

@Override
public void add(int index, Integer element) {

}

@Override
public Integer remove() {
return null;
}

@Override
public Integer remove(int index) {
return null;
}

@Override
public boolean remove(Integer item) {
return false;
}

@Override
public void clear() {

}

@Override
public boolean contains(Integer item) {
return false;
}

@Override
public Integer get(int index) {
return null;
}

@Override
public int indexOf(Integer item) {
return 0;
}

@Override
public boolean isEmpty() {
return false;
}

@Override
public int size() {
return 0;
}

// Uncomment when ready to test
// @Override
// public String toString() {
// String ret = "";
// Node curr = head;
// while (curr != null) {
// ret += curr.data + " ";
// curr = curr.next;
// }
// return ret;
// }

}
------------------------------------------------------------------------------------

MiniList.java>>>>>>>>

public interface MiniList<E> {
public boolean add(E item);
public void add(int index, E element);

public E remove();
public E remove(int index);
public boolean remove(E item);

public void clear();

public boolean contains(E item);

public boolean equals(Object o);

public E get(int index);
public int indexOf(E item);

public boolean isEmpty();

public int size();
}

--------------------------------------------------------------------------------------------

LLTest.java>>>>>>>>>>>>>>>>>>>>>>>>

public class LLTest {
public static void main(String args[]) {
MyLinkedList ll = new MyLinkedList();

// Testing add methods
System.out.println("Testing Add");
ll.add(40);
ll.add(10);
ll.add(20);
ll.add(1, 30);
ll.add(3, 100);
ll.add(65);
ll.add(2);
System.out.println("Expected: 40 30 10 100 20 65 2");
System.out.println("Actual: " + ll);
System.out.println();

// Testing remove methods
System.out.println("Testing Remove");
ll.remove();
ll.remove(2);
ll.remove(3);
ll.remove((Integer)2);
System.out.println("Expected: 30 10 20");
System.out.println("Actual: " + ll);
System.out.println("Size should be 3 -> " + ll.size());
System.out.println();

// Testing Contains
System.out.println("Testing Contains");
ll.add(2); // to make it a little bigger
System.out.println("Should be true -> " + ll.contains(2));
System.out.println("Should be false -> " + ll.contains(65));
System.out.println("Should be true -> " + ll.contains(30));
System.out.println();

// Testing Get
System.out.println("Testing Get");
System.out.println("Actual list: " + ll);
System.out.print("List using get: ");
for (int i = 0; i < ll.size(); i++) {
System.out.print(ll.get(i) + " ");
}
System.out.println("\n");

// Testing indexOf
System.out.println("Testing indexOf");
System.out.println("Should be 2 -> " + ll.indexOf(20));
System.out.println("Should be 3 -> " + ll.indexOf(2));
System.out.println("Should be -1 -> " + ll.indexOf(65));

// You can write more tests (these are not all encompassing)
// When should these methods fail/throw exceptions?
// Have all of the edge cases been tested?
// Not quite all of the methods have been tested here (e.g. clear())

}
}

----------------------------------------------------------------------------------

directions-

complete all of the methods in the the given MiniList interface. The documentation for these methods match the ones in Oracle's documentation for a linked list

write the node class which should contain two member variables: data and next.

Solutions

Expert Solution

OUTPUT - I have added an own display function for testing. Feel free to remove that.




CODE:

- Tried my best to cover all the functions and appropriate return types. Kindly upvote if this helped you.
- Define you own package at the top if needed.

public class MyLinkedList implements MiniList < Integer > {
    /* Private member variables that you need to declare:
     ** The head pointer
     ** The tail pointer
     */

    class Node {
        Integer data;
        Node next;

        public Node(int data) {
            this.data = data;
            this.next = null;
        }
    }

    public Node h;
    public Node t;

    // Initialize the linked list (set head and tail pointers)
    public MyLinkedList() {
        h = null;
        t = null;
    }

    @Override
    public boolean add(Integer item) {
        Node newNode = new Node(item);
        if (h == null) {
            h = newNode;
            t = newNode;
        } else {
            t.next = newNode;
            t = newNode;
        }
        return true;
    }

    @Override
    public void add(int index, Integer element) {
        Node current = h;
        if (h == null) {
            return;
        }
        int count = 0;
        while (current != null) {
            if (index - 1 == count) {
                Node newNode = new Node(element);
                newNode.next = current.next;
                current.next = newNode;
                break;
            }
            current = current.next;
            count++;
        }
    }

    @Override
    public Integer remove() {
        //Node current = h;
        Integer ele = null;
        ele = h.data;
        h = h.next;
        //        if (h == null) {
        //            return null;
        //        }
        //        while (current != null) {
        //            if (current.next.next == null) {
        //                ele = current.next.data;
        //                current.next = current.next.next;
        //                break;
        //            }
        //            current = current.next;
        //        }
        return ele;
    }

    @Override
    public Integer remove(int index) {
        Node current = h;
        Integer ele = null;
        int count = 0;
        if (h == null) {
            return null;
        }
        while (current != null) {
            if (index - 1 == count && current.next != null) {

                ele = current.next.data;
                if (current.next.next == null) {
                    // t.next = current.next;
                    t = current;
                }
                current.next = current.next.next;
                //current.next.next = null;
                break;
            }
            current = current.next;
            count++;
        }
        return ele;
    }

    @Override
    public boolean remove(Integer item) {
        Node current = h;

        if (h == null) {
            return false;
        }
        if (current.data == item) {
            h = current.next;
            return true;
        }
        while (current != null) {
            if (current.next != null && current.next.data == item) {
                if (current.next.next == null) {
                    // t.next = current.next;
                    t = current;
                }
                current.next = current.next.next;
                //current.next.next = null;
                break;
            }
            current = current.next;
        }
        return true;
    }

    @Override
    public void clear() {
        h = null;
    }

    @Override
    public boolean contains(Integer item) {

        Node current = h;

        if (h == null) {
            return false;
        }
        while (current != null) {
            if (current.data == item) {
                return true;
            }
            current = current.next;
        }
        return false;
    }

    @Override
    public Integer get(int index) {
        Node current = h;
        int count = 0;
        if (h == null) {
            return null;
        }
        while (current != null) {
            if (index == count) {
                return current.data;
            }
            current = current.next;
            count++;

        }
        return null;
    }

    @Override
    public int indexOf(Integer item) {
        Node current = h;
        int count = 0;
        if (h == null) {
            return -1;
        }
        while (current != null) {
            if (current.data == item) {
                return count;
            }
            current = current.next;
            count++;
        }
        return -1;
    }

    @Override
    public boolean isEmpty() {
        if (h == null) {
            return true;
        }
        return false;
    }

    @Override
    public int size() {
        Node current = h;
        int count = 0;
        if (h == null) {
            return 0;
        }
        while (current != null) {
            current = current.next;
            count++;
        }
        return count;
    }

    public void display() {
        Node current = h;
        if (h == null) {
            System.out.println("No data in list");
            return;
        }
        System.out.println("Nodes info are ");
        while (current != null) {
            System.out.print(current.data + " ");
            current = current.next;
        }
    }

    // Uncomment when ready to test
    // @Override
    // public String toString() {
    // String ret = "";
    // Node curr = head;
    // while (curr != null) {
    // ret += curr.data + " ";
    // curr = curr.next;
    // }
    // return ret;
    // }

}


interface MiniList < E > {
    public boolean add(E item);
    public void add(int index, E element);

    public E remove();
    public E remove(int index);
    public boolean remove(E item);

    public void clear();

    public boolean contains(E item);

    public boolean equals(Object o);

    public E get(int index);
    public int indexOf(E item);

    public boolean isEmpty();

    public int size();
}





class LLTest {
    public static void main(String args[]) {
        MyLinkedList ll = new MyLinkedList();

        // Testing add methods
        System.out.println("Testing Add");
        ll.add(40);
        ll.add(10);
        ll.add(20);
        ll.add(1, 30);
        ll.add(3, 100);
        ll.add(65);
        ll.add(2);
        System.out.println("Expected: 40 30 10 100 20 65 2");
        System.out.print("Actual: ");
        ll.display();
        System.out.println();

        // Testing remove methods
        System.out.println("Testing Remove");
        ll.remove();
        ll.remove(2);
        ll.remove(3);
        ll.remove((Integer) 2);
        System.out.println("Expected: 30 10 20");
        System.out.print("Actual: ");
        ll.display();
        System.out.println("Size should be 3 -> " + ll.size());
        System.out.println();

        // Testing Contains
        System.out.println("Testing Contains");
        ll.add(2); // to make it a little bigger
        System.out.print("TESTING: ");
        ll.display();
        System.out.println("Should be true -> " + ll.contains(2));
        System.out.println("Should be false -> " + ll.contains(65));
        System.out.println("Should be true -> " + ll.contains(30));
        System.out.println();

        // Testing Get
        System.out.println("Testing Get");
        System.out.print("Actual: ");
        ll.display();
        System.out.print("List using get: ");
        for (int i = 0; i < ll.size(); i++) {
            System.out.print(ll.get(i) + " ");
        }
        System.out.println("\n");

        // Testing indexOf
        System.out.println("Testing indexOf");
        System.out.println("Should be 2 -> " + ll.indexOf(20));
        System.out.println("Should be 3 -> " + ll.indexOf(2));
        System.out.println("Should be -1 -> " + ll.indexOf(65));

        // You can write more tests (these are not all encompassing)
        // When should these methods fail/throw exceptions?
        // Have all of the edge cases been tested?
        // Not quite all of the methods have been tested here (e.g. clear())

    }
}

Related Solutions

Please solve this problem in java. (simple linked list) public class MyLinkedList implements MiniList{ /* Private...
Please solve this problem in java. (simple linked list) public class MyLinkedList implements MiniList{ /* Private member variables that you need to declare: ** The head pointer ** The tail pointer */    private Node head;    private Node tail;       public class Node { // declare member variables (data and next)    Integer data;    Node next; // finish these constructors    public Node(int data, Node next) {               this.data=data;        this.next=next;    }...
Author code /** * LinkedList class implements a doubly-linked list. */ public class MyLinkedList<AnyType> implements Iterable<AnyType>...
Author code /** * LinkedList class implements a doubly-linked list. */ public class MyLinkedList<AnyType> implements Iterable<AnyType> { /** * Construct an empty LinkedList. */ public MyLinkedList( ) { doClear( ); } private void clear( ) { doClear( ); } /** * Change the size of this collection to zero. */ public void doClear( ) { beginMarker = new Node<>( null, null, null ); endMarker = new Node<>( null, beginMarker, null ); beginMarker.next = endMarker; theSize = 0; modCount++; } /**...
The language is java package hw; public class MyLinkedList<E> { SLLNode<E> head = null; public MyLinkedList()...
The language is java package hw; public class MyLinkedList<E> { SLLNode<E> head = null; public MyLinkedList() {} // O(1) public MyLinkedList(E[] elements) { // O(elements.length) for(int i=elements.length-1;i>=0;i--) add(elements[i]); } public void printLinkedList() { // T(N) = O(N) System.out.print("printLinkedList(): "); SLLNode<E> node = head; while(node != null) { System.out.print(node.info + " "); node = node.next; // move to the next node } System.out.println(); } public void add(E e) { // T(N) = O(1) SLLNode<E> newNode = new SLLNode<E>(e); newNode.next = head;...
public class Runner{ public static void main(String[] args){           MylinkedList ll = new MylinkedList(10.1);...
public class Runner{ public static void main(String[] args){           MylinkedList ll = new MylinkedList(10.1);        ll.append(15.6);        ll.append(10.5);        ll.append(8.11);        ll.print();               ll.initiateIterator();        Object o = null;        while ( (o=ll.nextObject())!=null){            System.out.println((Double)(o)+100.1);        } Your solution here    // Iterate, find, and report the largest number               MylinkedList lb = new MylinkedList();        lb.append( new Rectangle(10.1, 20.2) );   ...
How do you write the constructor for the three private fields? public class Student implements Named...
How do you write the constructor for the three private fields? public class Student implements Named { private Person asPerson; private String major; private String universityName; @Override public String name() { return asPerson.name(); }
/*Design and code a class Calculator that has the following * tow integer member variables, num1...
/*Design and code a class Calculator that has the following * tow integer member variables, num1 and num2. * - a method to display the sum of the two integers * - a method to display the product * - a method to display the difference * - a method to display the quotient * - a method to display the modulo (num1%num2) * - a method toString() to return num1 and num2 in a string * - Test your...
public class SumMinMaxArgs { private int[] array; // You will need to write the following: //...
public class SumMinMaxArgs { private int[] array; // You will need to write the following: // // 1. A constructor that takes a reference to an array, // and initializes an instance variable with this // array reference // // 2. An instance method named sum, which will calculate // the sum of the elements in the array. If the array // is empty (contains no elements), then this will // will return 0. You will need a loop for...
//package Lab7_2_LinkedList_Sol; public class Runner { public static void main(String[] args){ MyLinkedList ll = new MyLinkedList(10.1);...
//package Lab7_2_LinkedList_Sol; public class Runner { public static void main(String[] args){ MyLinkedList ll = new MyLinkedList(10.1); ll.append(10.5); ll.append(8.11); ll.append(15.6); System.out.println("--------Printing via print method------------"); ll.print(); System.out.println("--------Printing via initiator------------"); ll.initiateIterator(); Object o = null; while ( (o=ll.nextObject())!=null){ System.out.println((Double)(o)); } ll.initiateIterator(); Object largest=ll.nextObject(); while (((o=ll.nextObject())!=null)){ if ((Double)o>(Double)largest) largest=o; } System.out.println("The largest number in the LL is: "+largest); System.out.println("--------Number insertion------------"); ll.insert(100.2145, 4); ll.insert(110.2145, 0); ll.insert(120.2145, 2); ll.insert(180.2145, 8); ll.print(); System.out.println("--------Removal------------"); int ind=3; System.out.println("Removal of pos "+ind+" is: "+ll.remove(ind)); ll.print(); ind=5; System.out.println("Removal of pos "+ind+"...
In C++ Programming Language: 1a. Declare a class, namely ReverseUniverse, that contains three public member functions....
In C++ Programming Language: 1a. Declare a class, namely ReverseUniverse, that contains three public member functions. Please see Q2 and Q3 for the name of the second and third function. (5 points) Write a function string reverseString that reverses a string (first member function). It takes a string parameter and returns its reversed version. (10 points) 1b. In the class ReverseUniverse in Q1. Write a function vector reverseVector that reverses a vector (second member function). It takes a double vector...
1. Consider the following code: public class Widget implements Serializable { private int x; public void...
1. Consider the following code: public class Widget implements Serializable { private int x; public void setX( int d ) { x = d; } public int getX() { return x; } writeObject( Object o ) { o.writeInt(x); } } Which of the following statements is true? I. The Widget class is not serializable because no constructor is defined. II. The Widget class is not serializable because the implementation of writeObject() is not needed. III. The code will not compile...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT