Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

2. MyLinkedList is a linked list with two reference variables, where head always refers to the head of myLinkedList and tail always refers to end

2. MyLinkedList is a linked list with two reference variables, where head always refers to the head of myLinkedList and tail always refers to end (last node if existed) of myLinkedList.

3. Based on the provided skeleton implement the following methods

a. public int size(): this method will return the number of nodes on the linkedList

b. public boolean isEmpty(): check if the list is empty or not

c. public void addFirst(E e): //add a node to the front of the list

d. public void addLast(E e)://add a node to the end of the list

e. public E removeFirst()://remove the first node of the list and return the removed item

f. public void removeLast( ): this method will remove the last node from the list if exist.

g. public void concatenateList (myLinkedList M) : this method will concatenate another myLinkedList referred as M at the end of this myLinkedList without creating any new node. For example, if you have this list is head->1->2->3->4<-tail, M: head->5->61->2->3->4->5->6<-tail.

h. public int searchElement (E targetElement): this method will search if a targetElement is on a list or not, and return the occurrence of the target element.

i. public void removeElement(E targetElement): this method will remove all occurrence of a targetElement from a list.

j. Public E middleElement(): This method will return the element store in the middle of the list. For example, if you have this list is head->1->2->3->4<-tail, by calling this method, it will return 2 or 3. If you have a list contains head->1->2->3->4->5<-tail, by calling this method, it will return 3.

Remarks:Please do not add other instance fields or methods. Do not change the provided method signature including return type. Only based on the given information to implement each method

Main Class***********************************************************************************************

public class mainClass { public static void main(String[] args) { myLinkedList  listOne = new myLinkedList (); listOne.addFirst(2); listOne.addLast(3); listOne.addLast(4); listOne.addFirst(1); System.out.println("listOne: "+listOne); myLinkedList  listTwo = new myLinkedList (); listTwo.addFirst(2); listTwo.addLast(3); listTwo.addLast(4); listTwo.addFirst(1); System.out.println("listTwo: "+listTwo); listOne.removeLast(); System.out.println("listOne after removing last: "+listOne); listOne.concatenateList(listTwo); System.out.println("listOne after concatenate with listTwo: "+listOne); if(listOne.searchElement(1)>0) System.out.println("Found 1 in the list. "); else System.out.println("No 1 in the list. "); listOne.removeElement(1); System.out.println("listOne after remove all 1: "+listOne); if(listOne.searchElement(1)<1) System.out.println("Failed to find 1 in the list. "); else System.out.println("No 1 in the list. "); System.out.println("Middle element of "+ listOne +" is "+listOne.middleElement()); listOne.addFirst(5); System.out.println("New List after inserting one item is"+ listOne); System.out.println("Middle element of "+ listOne +" is "+listOne.middleElement()); } } 

Linked Class****************************************************************************************************************

public class myLinkedList>{ // instance variables of the SinglyLinkedList private Node head = null; // head node of the list (or null if empty) private Node tail = null; // last node of the list (or null if empty) public myLinkedList() { // constructs an initially empty list } public int size() {//Returns the number of elements in the linked list. } public boolean isEmpty() { // Tests whether the linked list is empty. } public E first() { // returns (but does not remove) the first element } public E last() { // returns (but does not remove) the last element } public void addFirst(E e) { // adds element e to the front of the list } public void addLast(E e) { // adds element e to the end of the list } public E removeFirst() { E firstElement =null;// removes and returns the first element } public E removeLast() { // removes and returns the first element } public void concatenateList (myLinkedList M) {//attach another linkedList refered by M to the end of this linkedList } public int searchElement (E targetElement){//test if a target element can be found on the list, return the occurrences of the target element } public void removeElement(E targetElement){//remove all target element that can be found on the list } public E middleElement(){//find and return the element that stored at the middle node of a linkedList } /** * Produces a string representation of the contents of the list. * This exists for debugging purposes only. */ public String toString() { StringBuilder sb = new StringBuilder("("); Node walk = head; while (walk != null) { sb.append(walk.getItem()); if (walk != tail) sb.append(", "); walk = walk.getNext(); } sb.append(")"); return sb.toString(); } } 

Node class***********************************************************************************************************

public class Node> { private E item; // reference to the element stored at this node private Node next; // reference to the subsequent node in the list /** * Creates a node with the given element and next node. * * @param e the element to be stored * @param n reference to a node that should follow the new node */ public Node(E e, Node n) { item = e; next = n; } public Node(E e) { item = e; next = null; } // Accessor methods public E getItem() { return item; } public Node getNext() { return next; } // Modifier methods public void setNext(Node n) { next = n; } } //----------- end of nested Node class -----------

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Consistently develop management talent.

Answered: 1 week ago

Question

Create a refreshed and common vision and values across Europe.

Answered: 1 week ago

Question

Provide the best employee relations environment.

Answered: 1 week ago