Question

1. Create a class MLL, a singly linked, non-circular list where each node only has one...

1. Create a class MLL, a singly linked, non-circular list where each node only has one link next and the list has a head and a tail link (think about implementation of node). The MLL class also implements the Iterable interface. The following should be implemented as well:

2. Add the methods to the MLL class:

a. iterator() to implement the Iterable interface. This method returns an instance of MLLI initialized appropriately.

b. addFirst( value) that adds a value to the front of the list

c. addEnd( value) that adds a value to the end of the list - don't forget the boundary cases

d. A get method which given an index returns the element.

3. Create a class MLLI that implements the Iterator interface.

4. Create a class called MLIC which uses your new data structure. It only needs to implement the following methods:

a. addToBack

b. addToFront

c. Search for a value (this should search for an Integer in the list and return the first index of the value if it is present, if not return -1. The array is not sorted). Two versions of this method are needed. One which uses an iterator and one which does not.

The programming language is java.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Iterator;

//  A simple class which acts as Node of our linked list
class Node{
    int data;
    Node next;

    Node(int data){
        this.data = data;
    }
}


class MLL implements Iterable<Node>{
    /* head and tail taken static so that each object
    doesn't  have their different copies 
     */
    static Node head = null;
    static Node tail = null;

    void addFirst(int value){
        Node node = new Node(value);
        if(head == null){
           head = node;
           tail = node;
        }else{
            // Push the previous element back
            node.next = head;
            head = node;
        }
    }

    void addLast(int value){
        Node node = new Node(value);
        if(head == null){
            head = node;
            tail = node;
        }else{
            // Set the new element as last element
            tail.next = node;
            tail = node;
        }
    }

    int get(int index){
        Node pointer = head;
        if(index <= 0){
            return -1;
        }
        // Assuming index starts from 1
        int count = 1;
        // Check for index, as well as for the end of the list 
        while(count != index && pointer != null){
            pointer = pointer.next;
        }
        // Check if element really exists
        if(pointer != null){
            return pointer.data;
        }
        return -1;
    }

    // Returns the iterator
    @Override
    public Iterator<Node> iterator() {
        return new MLLI();
    }
}

class MLLI implements Iterator<Node>{

    Node start = MLL.head;

    @Override
    public boolean hasNext() {
        return start.next != null;
    }

    @Override
    public Node next() {
        Node obj = start;
        start = start.next;
        return obj;
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException();
    }
}

class MLIC{
    static MLL object = new MLL();

     void addToFront(int value){
         object.addFirst(value);
     }

     void addToBack(int value){
         object.addLast(value);
     }

     int search(int value){
         Node pointer = MLL.head;
         int count = 1;
         while(pointer != null && pointer.data != value){
             pointer = pointer.next;
             count++;
         }
         if(pointer != null){
             return count;
         }
         return -1;
     }

     int searchWithIterator(int value){
         MLLI iterator = (MLLI) object.iterator();
         int count = 1;
         boolean flag = false;
         while (iterator.hasNext()){
             Node object = iterator.next();
             // Set flag to determine whether element was in linked list or not.
             if(object.data == value) {
                 flag = true;
                 break;
             }
             count++;
         }
         if(flag)
             return count;
         return -1;
     }
}
public class Test {
    public static void main(String[] args) {
        MLIC object = new MLIC();

        object.addToBack(5);
        object.addToBack(1);

        object.addToFront(34);
        object.addToFront(21);
        object.addToFront(10);

        System.out.println("Index " + object.search(35));
        System.out.println("Index " + object.searchWithIterator(5));
        System.out.println("Index " + object.search(34));
        System.out.println("Index " + object.searchWithIterator(34));
    }
}
Add a comment
Know the answer?
Add Answer to:
1. Create a class MLL, a singly linked, non-circular list where each node only has one...
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
  • 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...

  • 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...

  • how to implement a linked list object using only singly linked list toolkit. Then implement a...

    how to implement a linked list object using only singly linked list toolkit. Then implement a FREQUENCY function to count the ovcurrence of each element in the list. task#1: Add = operator to node: implement the assignment operator for the node such that setting a node = overwrites the value in the node with the value. task#2:Linked List class implement the methods of linked list. insert search and locate remove node* operator [] task#3: Implement the Frequency

  • use python In class, we've studied Singly-Linked Lists which are made of nodes that point at...

    use python In class, we've studied Singly-Linked Lists which are made of nodes that point at subsequent nodes. One of the biggest annoyances with Linked Lists is the difficulty of going backwards through a list (such as getting the previous node or traversing the list backwards). An intuitive solution to this inefficiency is the doubly-linked list, which adds pointers to previ- ous nodes. Doubly-Linked Lists are not very different from Singly-Linked Lists, but are far more common because they are...

  • use python In class, we've studied Singly-Linked Lists which are made of nodes that point at...

    use python In class, we've studied Singly-Linked Lists which are made of nodes that point at subsequent nodes. One of the biggest annoyances with Linked Lists is the difficulty of going backwards through a list (such as getting the previous node or traversing the list backwards). An intuitive solution to this inefficiency is the doubly-linked list, which adds pointers to previ- ous nodes. Doubly-Linked Lists are not very different from Singly-Linked Lists, but are far more common because they are...

  • use python In class, we've studied Singly-Linked Lists which are made of nodes that point at...

    use python In class, we've studied Singly-Linked Lists which are made of nodes that point at subsequent nodes. One of the biggest annoyances with Linked Lists is the difficulty of going backwards through a list (such as getting the previous node or traversing the list backwards). An intuitive solution to this inefficiency is the doubly-linked list, which adds pointers to previ- ous nodes. Doubly-Linked Lists are not very different from Singly-Linked Lists, but are far more common because they are...

  • Consider a singly linked list. What is true about the last node in the data structure?

    Question 14 Consider a singly linked list. What is true about the last node in the data structure? The node cannot store data values. The node points to the head node. The node points to null. The node points to head. Question 15 When defining an interface, you cannot implement another interface. you may not include more than one method signature. you must provide the body of at least one method. you must provide signatures for each method Question 16 Generics are allowed in the class definition header but not in a method definition...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • Create a linked list with given code Given code: import java.util.Iterator; public interface Set { /**...

    Create a linked list with given code Given code: import java.util.Iterator; public interface Set { /** Removes all of the elements from this set */ void clear(); /** Returns true if this set contains no elements @return true if this set contains no elements */ boolean isEmpty(); /** Returns the number of elements in this set (its cardinality) @return the number of elements in this set */ int size(); /** Returns an iterator over the elements in this set @return...

  • In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class...

    In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class (50 pts): Create node with public properties: Block type block and block ptr next. Create a linked list class that uses the node you generated without an add or delete method with a head and optional tail and counter. Make a driver that generates a node to test your implementation. Part 2 Add Method (30 pts): Create an add method in your linked list...

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