In: Computer Science
Write the Java source code necessary to build a solution for the
problem below:
Create a MyLinkedList class. Create methods in the class to add an
item to the head, tail, or middle of a linked list; remove an item
from the head, tail, or middle of a linked list; check the size of
the list; and search for an element in the list.
Create a test class to use the newly created MyLinkedList class. Add the following names in to the list: James, John, Michael, Peter, Allison, Daniel, George, Simon, Jason, and Mark. Your program should allow the user to enter a name from the console, and then search to see if the name exists in the list.
Hi, Please find my implementation.
Please let me know in case of any issue.
public class MyLinkedList {
static class Node{
String data;
Node next;
Node(String d){
data = d;
next =null;
}
}
private Node head;
public MyLinkedList() {
head = null;
}
public void append(String data){
if(head == null)
head = new Node(data);
else{
Node temp = head;
while(temp.next != null)
temp = temp.next;
temp.next = new Node(data);
}
}
public void addFront(String data){
Node newNode = new Node(data);
newNode.next = head;
head = newNode;
}
public void removeFromLast(){
if(head == null || head.next == null)
head = null;
else{
Node temp = head;
while(temp.next.next != null)
temp = temp.next;
temp.next = null; // removing last element
}
}
public void removeFirst(){
if(head == null)
return;
else
head = head.next;
}
public boolean search(String data){
Node temp = head;
while(temp != null){
if(temp.data.equals(data))
return true;
temp = temp.next;
}
return false;
}
public void addAtMiddle(String data){
Node slow = head;
Node fast = head;
Node prev = null;
Node newNode = new Node(data);
if(head == null || head.next == null){
newNode.next = head;
head = newNode;
return;
}
while(fast != null && fast.next != null){
fast = fast.next.next;
prev = slow;
slow = slow.next;
}
if(prev != null)
prev.next =newNode;
newNode.next = slow;
}
public void removeAtMiddle(){
Node slow = head;
Node fast = head;
Node prev = null;
if(head == null || head.next == null){
head = null;
return;
}
while(fast != null && fast.next != null){
fast = fast.next.next;
prev = slow;
slow = slow.next;
}
prev.next = slow.next;
}
public void display(){
Node temp = head;
while(temp != null){
System.out.print(temp.data+" ");
temp =temp.next;
}
System.out.println();
}
}
public class MyLinkedListTest {
public static void main(String[] args) {
MyLinkedList list = new MyLinkedList();
list.addAtMiddle("James");
list.display();
list.addAtMiddle("John");
list.display();
list.addAtMiddle("Michael");
list.display();
list.addAtMiddle("Peter");
list.display();
list.addAtMiddle("Allison");
list.display();
list.addFront("Daniel");
list.display();
list.append("George");
list.display();
list.addFront("Simon");
list.display();
list.append("Jason");
list.display();
list.addAtMiddle("Mark");
list.display();
list.removeAtMiddle();
list.display();
list.removeFirst();
list.display();
list.removeFromLast();
list.display();
System.out.println("George ? "+list.search("George"));
}
}
/*
Sample run:
James
John James
John Michael James
John Peter Michael James
John Peter Allison Michael James
Daniel John Peter Allison Michael James
Daniel John Peter Allison Michael James George
Simon Daniel John Peter Allison Michael James George
Simon Daniel John Peter Allison Michael James George Jason
Simon Daniel John Peter Mark Allison Michael James George Jason
Simon Daniel John Peter Mark Michael James George Jason
Daniel John Peter Mark Michael James George Jason
Daniel John Peter Mark Michael James George
George ? true
*/