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.
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));




}
}
1. Create a class MLL, a singly linked, non-circular list where each node only has one...
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: /** * 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 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 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 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 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...
1. Create a node class and linked list class. 2. Add the following function from lecture to your linked list class addFirst(e) addLast (e) add(index, e) removeFirst removeLast remove(index) 3. Add the following methods to the linked list class. a. # Return the size of the list def getSize(self): b. # Clear the list */ def clear(self): c. # Return the element from this list at the specified index def get(self, index):
This class implements a doubly linked list in which the nodes are of the class DLNode. This class must implement the interface DLListADT.java that specifies the public methods in this class. The header for this class will then be public class DLList implements DLListADT This class will have three private instance variables: • private DLNode front. This is a reference to the first node of the doubly linked list. • private DLNode rear. This is a reference to the last...
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 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...