Step 1 Create a new project Name it Assignment_3_1 . Step 2 Build a solution . W
ID: 3793345 • Letter: S
Question
Step 1 Create a new project
Name it Assignment_3_1 .
Step 2 Build a solution.
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. CANNOT USE JAVA LIBRARIES
Explanation / Answer
Hi,
Please see below the java classes and sample output. Please comment for any queries/feedbacks.
Thanks,
Anita Joseph
MyLinkedList,java
import java.util.Iterator;
import java.util.LinkedList;
public class MyLinkedList {
LinkedList<String> linkedList;
public MyLinkedList(){
linkedList=new LinkedList<String>();
}
//to add items to tail
public void addItemtoTail(String name){
linkedList.add(name);
}
//to add items to head
public void addItemtoHead(String name){
linkedList.addFirst(name);
}
//to add items to middle
public void addItemtoMiddle(String name){
int middleIndex = linkedList.size()/2;
linkedList.add(middleIndex,name);
}
//to remove items from tail
public void removeItemFromTail(){
linkedList.removeLast();
}
//to remove items from head
public void removeItemFromHead(){
linkedList.removeFirst();
}
//to remove items from middle
public void removeItemFromMiddle(){
linkedList.remove(linkedList.size()/2);
}
//checking the current size
public void checkSize(){
System.out.println("Size of the list is : "+linkedList.size());
}
//Searching an element in the list
public void searchElement(String name){
Iterator<String> itr=this.linkedList.iterator();
boolean isPresent = false;
while(itr.hasNext()){
String currName = itr.next();
if(currName.equalsIgnoreCase(name)){
isPresent = true; //setting the isPresent flag as true if a match found and exiting from the loop
break;
}
}
if(isPresent){
System.out.println(name +" present in the list.");
}
else{
System.out.println(name +" is not present in the list.");
}
}
//To display the items present in the list
public void displayItems(){
System.out.println("Displaying current list: ");
Iterator<String> itr=this.linkedList.iterator();
while(itr.hasNext()){
System.out.print(itr.next()+", ");
}
System.out.println();
}
}
MyLinkedListTester.java
import java.util.Scanner;
//Tester class for MyLinkedList
public class MyLinkedListTester {
public static void main(String [] args){
Scanner scan = new Scanner(System.in);
String nameToSearch = "";
//Setting the initial values to the linked list
MyLinkedList myList = new MyLinkedList();
myList.addItemtoTail("James");
myList.addItemtoTail("John");
myList.addItemtoTail("Michael");
myList.addItemtoTail("Peter");
myList.addItemtoTail("Allison");
myList.addItemtoTail("Daniel");
myList.addItemtoTail("George");
myList.addItemtoTail("Simon");
myList.addItemtoTail("Jason");
myList.addItemtoTail("Mark");
//reading the name to search from the user
System.out.println("Enter a name to search: ");
nameToSearch = scan.nextLine();
//Searching the name in current list
myList.searchElement(nameToSearch);
//Displaying items in the list
myList.displayItems();
//Checking the size
System.out.println("checking the size: ");
myList.checkSize();
//Adding items to tail
System.out.println();
System.out.println("Adding items to tail - name : Catherine");
myList.addItemtoTail("Catherine");
myList.displayItems();
//Adding items to tail
System.out.println();
System.out.println("Adding items to head - name : Joseph");
myList.addItemtoHead("Joseph");
myList.displayItems();
//Adding items to tail
System.out.println();
System.out.println("Adding items to middle - name : Robert");
myList.addItemtoMiddle("Robert");
myList.displayItems();
//Checking the size
System.out.println();
System.out.println("checking the size: ");
myList.checkSize();
//Removing items from tail
System.out.println();
System.out.println("Removing items from tail ");
myList.removeItemFromTail();
myList.displayItems();
//Removing items from head
System.out.println();
System.out.println("Removing items from head ");
myList.removeItemFromHead();
myList.displayItems();
//Removing items from middle
System.out.println();
System.out.println("Removing items from middle ");
myList.removeItemFromMiddle();
myList.displayItems();
//Checking the size
System.out.println("checking the size: ");
myList.checkSize();
}
}
Sample output:
Enter a name to search:
Jack
Jack is not present in the list.
Displaying current list:
James, John, Michael, Peter, Allison, Daniel, George, Simon, Jason, Mark,
checking the size:
Size of the list is : 10
Adding items to tail - name : Catherine
Displaying current list:
James, John, Michael, Peter, Allison, Daniel, George, Simon, Jason, Mark, Catherine,
Adding items to head - name : Joseph
Displaying current list:
Joseph, James, John, Michael, Peter, Allison, Daniel, George, Simon, Jason, Mark, Catherine,
Adding items to middle - name : Robert
Displaying current list:
Joseph, James, John, Michael, Peter, Allison, Robert, Daniel, George, Simon, Jason, Mark, Catherine,
checking the size:
Size of the list is : 13
Removing items from tail
Displaying current list:
Joseph, James, John, Michael, Peter, Allison, Robert, Daniel, George, Simon, Jason, Mark,
Removing items from head
Displaying current list:
James, John, Michael, Peter, Allison, Robert, Daniel, George, Simon, Jason, Mark,
Removing items from middle
Displaying current list:
James, John, Michael, Peter, Allison, Daniel, George, Simon, Jason, Mark,
checking the size:
Size of the list is : 10
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.