Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A MyList is a data structure consisting of a list of items, on which the followi

ID: 3722896 • Letter: A

Question

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 LinkedList 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) f. . AnyType myPop() f. . .1 void myInject (AnyType x) (. . .)

Explanation / Answer

Solution:

code:

public class MyList<AnyType> {

   public Node head;

   public Node tail;

   public MyList() {

       // TODO Auto-generated constructor stub

   }

   void myPush(AnyType x) {

       Node newNode = new Node(x);

       if (head == null) {

           head = newNode;

           tail = newNode;

       } else {

           newNode.next = head;

           head = newNode;

       }

   }

   AnyType myPop() {

       AnyType t = null;

       if (head == null)

           return null;

       else {

           t = head.data;

           head = head.next;

           if (head == null)

               tail = null;

       }

       return t;

   }

   void myInject(AnyType x) {

       Node newNode = new Node(x);

       if (tail == null) {

           head = newNode;

           tail = newNode;

       } else {

           tail.next = newNode;

           tail = newNode;

       }

   }

   private class Node {

       private AnyType data; // entry in stack

       private Node next; // link to next node

       private Node(AnyType dataPortion) {

           this(dataPortion, null);

       } // end constructor

       private Node(AnyType dataPortion, Node nextNode) {

           data = dataPortion;

           next = nextNode;

       } // end constructor

       private AnyType getData() {

           return data;

       } // end getData

   } // end Node

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote