Please write this in Java and include a main method. A MyList is a data structur
ID: 3722451 • Letter: P
Question
Please write this in Java and include a main method.
A MyList is a data structure consisting of a list of items, on which the following operations are possible myPush(x): Insert item x on the front end of the MyList. myPop(): Remove the front item from the MyList and return it. myInject(x): Insert item x on the rear end of the MyList. Using the Linked List class, write a class in Java to implement the MyList data structure and that take O(1) time per operation. Note: The MyList class signature is: public class MyList MyList . -> void myPush(AnyType x) {-- .} AnyType myPop) t. . -1 void mylnject(AnyType x) {.)Explanation / Answer
//Java Code
/*In java LinkedList class work as a doubly linklist it means all the three operation would take O(1) time */
import java.io.*;
import java.util.*;
public class MyList<E> //generic class
{
private LinkedList<E>list;
MyList()
{
list=new LinkedList<E>();
}
public void myPush(E x)
{
list.addFirst(x);
}
public E myPop()
{
return list.getFirst();
}
public void myInject(E x)
{
list.addLast(x);
}
public static void main(String args[])
{
/* Here i have taken an example to test the code.let's create an list of any type .Let's integer type */
MyList<Integer>mlist=new MyList<Integer>();
mlist.myPush(10);
Integer temp=mlist.myPop();
System.out.println(temp);
mlist.myInject(20);
}
}