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.
*************************************************************************************************************************
Just need help creating methods to ":remove an item from the head, tail, or middle of a linked list;"
My existing code is below:
************MyLinked List class**************
public class MyLinkedList<E> {
private Node<E> head;
private Node<E> tail;
public void addAtStart(E data) {
Node<E> newNode = new Node<>(data);
if (head == null) {
head = newNode;
tail = newNode;
} else {
Node<E> temp = head;
head = newNode;
head.next = temp;
}
}
public void addAtMid(E data) {
if (head == null)
head = new Node<>(data);
else {
Node<E> newNode = new Node<>(data);
Node<E> ptr = head;
int length = 0;
while (ptr != null) {
length++;
ptr = ptr.next;
}
int count = ((length % 2) == 0) ? (length / 2) : (length + 1) /
2;
ptr = head;
while (count-- > 1)
ptr = ptr.next;
newNode.next = ptr.next;
ptr.next = newNode;
}
}
public void addAtEnd(E data) {
Node<E> newNode = new Node<>(data);
if (head == null) {
head = newNode;
tail = newNode;
} else {
tail.next = newNode;
tail = newNode;
}
}
public void display() {
Node<E> current = head;
if (head == null) {
System.out.println("List is empty");
return;
}
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
public boolean search(E data) {
Node<E> current = head;
int i = 1;
boolean flag = false;
if (head == null) {
System.out.println("List is empty");
} else {
while (current != null) {
if (current.data.equals(data)) {
flag = true;
break;
}
i++;
current = current.next;
}
}
if (flag)
return true;
else
return false;
}
private class Node<E> {
public E data;
public Node<E> next;
public Node(E data) {
this.data = data;
this.next = null;
}
}
}
************Testclass class****************
import java.util.Scanner;
public class Testclass {
public static void main(String[] args) {
MyLinkedList<String> names = new
MyLinkedList<>();
names.addAtStart("James");
names.addAtStart("John");
names.addAtStart("Michael");
names.addAtStart("Peter");
names.addAtMid("Allison");
names.addAtMid("Daniel");
names.addAtMid("George");
names.addAtEnd("Simon");
names.addAtEnd("Jason");
names.addAtEnd("Mark");
System.out.print("Enter the name you want to search: ");
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
if (names.search(name)) {
System.out.println("Name is in list");
}
else {
System.out.println("That name is not in list");
}
scan.close();
names.display();
}
}
Answer.
package singlyLinkedList;
public class MyLinkedList<E> {
private Node<E> head;
private Node<E> tail;
public void addAtStart(E data) {
Node<E> newNode = new Node<>(data);
if (head == null) {
head = newNode;
tail = newNode;
} else {
Node<E> temp = head;
head = newNode;
head.next = temp;
}
}
public void addAtMid(E data) {
if (head == null)
head = new Node<>(data);
else {
Node<E> newNode = new Node<>(data);
Node<E> ptr = head;
int length = 0;
while (ptr != null) {
length++;
ptr = ptr.next;
}
int count = ((length % 2) == 0) ? (length / 2) : (length + 1) / 2;
ptr = head;
while (count-- > 1)
ptr = ptr.next;
newNode.next = ptr.next;
ptr.next = newNode;
}
}
public void addAtEnd(E data) {
Node<E> newNode = new Node<>(data);
if (head == null) {
head = newNode;
tail = newNode;
} else {
tail.next = newNode;
tail = newNode;
}
}
public void deleteFromStart() {
// Checks if the list is empty
if (head == null) {
System.out.println("List is empty");
return;
} else {
// Checks whether the list contains only one node
// If not, the head will point to next node in the list and tail will point to
// the new head.
if (head != tail) {
head = head.next;
} else
head = tail;
}
}
public void deleteFromEnd() {
if (head != tail) { // Checks whether the list contains only one element
Node current = head;
// Loop through the list till the second last element such that current.next is
// pointing to tail
while (current.next != tail) {
current = current.next;
}
// Second last element will become new tail of the list
tail = current;
tail.next = null;
}
// If the list contains only one element
// Then it will remove it and both head and tail will point to null
}
public void display() {
Node<E> current = head;
if (head == null) {
System.out.println("List is empty");
return;
}
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
public boolean search(E data) {
Node<E> current = head;
int i = 1;
boolean flag = false;
if (head == null) {
System.out.println("List is empty");
} else {
while (current != null) {
if (current.data.equals(data)) {
flag = true;
break;
}
i++;
current = current.next;
}
}
if (flag)
return true;
else
return false;
}
private class Node<E> {
public E data;
public Node<E> next;
public Node(E data) {
this.data = data;
this.next = null;
}
}
}
----------------------------------------------------
package singlyLinkedList;
import java.util.Scanner;
public class Testclass {
public static void main(String[] args) {
MyLinkedList<String> names = new MyLinkedList<String>();
names.addAtStart("James");
names.addAtStart("John");
names.addAtStart("Michael");
names.addAtStart("Peter");
names.addAtMid("Allison");
names.addAtMid("Daniel");
names.addAtMid("George");
names.addAtEnd("Simon");
names.addAtEnd("Jason");
names.addAtEnd("Mark");
System.out.print("Enter the name you want to search: ");
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
if (names.search(name)) {
System.out.println("Name is in list \n \n");
} else {
System.out.println("That name is not in list");
}
scan.close();
names.display();
// Deleting from the Starting of the LinkedLIst
names.deleteFromStart();
System.out.print("\n\n Elements After Deleting the Starting Node Element : -- ");
names.display();
// Deleting from the End of the LinkedLIst
names.deleteFromEnd();
System.out.print("\n\n Elements after Deleting the Ending Node Element : --");
names.display();
}
}