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

LAB4 – Singly linked list Objective: The objective of this lab is to help the st

ID: 3919217 • Letter: L

Question

LAB4 – Singly linked list
Objective:
The objective of this lab is to help the students have a better understanding of the linked list based implementations.
1. Download LAB4.rar to get the Contact.java, Node.java and mainClass.java.
2. Create a class named myLinkedList based on the UML class diagram shown below.
3. Comments:
? head and tail references always refer to the first and last node on the list, respectively. size is a field record the number of nodes on the list, should be initialized as 0.
? size() method returns the number of nodes.
? isEmpty( ) checks if the list is empty. Return true if the list is empty, false, otherwise.
? first(): return the element stored in the first node
? last():return the element stored in the last node
myLinkedList
-head : Node
-tail : Node
-size:int
+ myLinkedList( )
+size( ):int
+isEmpty( ):boolean
+first( ):Node
+last( ):Node
+addFirst(Contact newContact):void
+addLast(Contact newContact):void
+removeFirst( ):Contact
+removeLast( ):Contact
+concatenateList (myLinkedList anotherList): void
+printList():void
? addFirst(): add a node at the beginning of the list
? addLast(): add a node to the end of the list
? removeFirst(): remove the first node on the list, return the removed
element
? removeLast(): remove the last node on the list, return the removed
element
? concatenateList () : this method will concatenate another list referred as
anotherList at the end of this list without creating any new node. For
example, if you have this list is head->1->2->3->4<-tail, M:
head->5->6<-tail then after call this method, you will have
head->1->2->3->4->5->6<-tail.
? toString( ): override the toString method to support print the contact
stored at each node by calling the System.out.println( ) method.
4. You can simply use mainClass.java or derive your own test method to test
myLinkedList. Only myLinkedlist is required for submission.
public static void main(String[] args)
{
myLinkedList listOne = new myLinkedList();
listOne.addFirst(new Contact("friend_1"));
listOne.addLast(new Contact("friend_2"));
listOne.addLast(new Contact("friend_3"));
listOne.addFirst(new Contact("friend_4"));
System.out.println("listOne: "+listOne);
myLinkedList listTwo = new myLinkedList();
listTwo.addFirst(new Contact("friend_5"));
listTwo.addLast(new Contact("friend_6"));
listTwo.addLast(new Contact("friend_7"));
listTwo.addFirst(new Contact("friend_8"));
System.out.println("listTwo: "+listTwo);
listOne.removeLast();
System.out.println("listOne after removing last: "+listOne);
listOne.concatenateList(listTwo);
System.out.println("listOne after concatenation: "+listOne);
listOne.removeFirst();
System.out.println("listOne after removing first: "+listOne);
}

Explanation / Answer

MainClass

If you have any questions let me know.feel free to ask