Step 1 Create a new project Name it Assignment_3_1 . Step 2 Build a solution. Wr
ID: 3757169 • 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.
Step 3 Compile and execute your code. Be sure to test the solution using a set of input data that demonstrates that your solution is correct. Take a screen shot showing the output from your Java program.
Using Java and NetBeans
Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. If you are satisfied with the solution, please rate the answer. Thanks
// MyLinkedList.java
public class MyLinkedList<T> {
//defining attributes
private Node<T> head; //points to the head node
private int size; //current number of elements
//default constructor
public MyLinkedList() {
head=null;
size=0;
}
/**
* method to add an item to the head
*/
public void addToHead(T item){
//creating a new node
Node<T> newNode=new Node<T>(item);
if(head==null){
//adding as head node
head=newNode;
}else{
//updating the current head node
newNode.next=head;
head=newNode;
}
size++;
}
/**
* method to add an item to the tail
*/
public void addToTail(T item){
Node<T> newNode=new Node<T>(item);
if(head==null){
//list is empty, so adding as head node
head=newNode;
}else{
//finding the tail node
Node<T> temp=head;
while(temp.next!=null){
temp=temp.next;
}
//adding to the next of current tail node
temp.next=newNode;
}
size++;
}
/**
* method to add an item to the middle index
*/
public void addToMiddle(T item){
Node<T> newNode=new Node<T>(item);
if(head==null){
//adding as head node
head=newNode;
}else{
//finding the middle index
int middle=size/2;
Node<T> temp=head.next;
Node<T> prev=head;
for(int i=0;i<middle-1;i++){
prev=temp;
temp=temp.next;
}
//adding to the middle
prev.next=newNode;
newNode.next=temp;
}
size++;
}
/**
* method to remove an item from the head and return it
*/
public T removeFromHead(){
if(head!=null){
//storing the data in current head
T item=head.data;
//updating the head node
head=head.next;
//decrementing size
size--;
//returning removed itm
return item;
}
return null; //list is empty
}
/**
* method to remove an item from the tail and return it
*/
public T removeFromTail(){
if(head!=null){
if(size==1){
//removing head node
T item=head.data;
head=null;
size--;
return item;
}
Node<T> temp=head;
//finding previous of tail node
for(int i=0;i<size-2;i++){
temp=temp.next;
}
//storing data of tail node
T item=temp.next.data;
//removing tail node
temp.next=null;
size--;
return item;
}
return null;
}
/**
* method to remove an item from the middle and return it
*/
public T removeFromMiddle(){
if(head!=null){
//finding the middle index
int middle=size/2;
if(middle==0){
//removing head node
T item=head.data;
head=null;
size--;
return item;
}
Node<T> temp=head;
//finding the middle Node
for(int i=0;i<middle-1;i++){
temp=temp.next;
}
//storing middle node's data
T item=temp.next.data;
//dropping the middle node
temp.next=temp.next.next;
size--;
return item;
}
return null;
}
/**
* method to find the size of the list
*/
public int size(){
return size;
}
/**
* method to check if an item exists on the list
*/
public boolean contains(T item){
Node<T> temp=head;
for(int i=0;i<size;i++){
if(temp.data.equals(item)){
return true;
}
temp=temp.next;
}
return false;
}
@Override
public String toString() {
//returning string representation of the list
String str="[";
Node<T> temp=head;
for(int i=0;i<size;i++){
str+=temp.data;
if(i!=size-1){
str+=", ";
}
temp=temp.next;
}
str+="]";
return str;
}
/**
* private inner class to represent a single Node
*/
private class Node<T>{
T data;
Node<T> next;
public Node(T data) {
this.data=data;
next=null;
}
}
}
// Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
/**
* creating a MyLinkedList of Strings and testing it thoroughly
*/
MyLinkedList<String> stringList = new MyLinkedList<String>();
System.out.println("Initial list: "+stringList);
System.out.println("Adding a few names to the tail");
stringList.addToTail("James");
stringList.addToTail("John");
stringList.addToTail("Michael");
System.out.println(stringList); // [James, John, Michael]
System.out.println("Adding a name to the head");
stringList.addToHead("Peter");
System.out.println(stringList); // [Peter, James, John, Michael]
System.out.println("Adding a name to the middle");
stringList.addToMiddle("Jason");
System.out.println(stringList); // [Peter, James, Jason, John, Michael]
System.out.println("Adding a few more names to the tail");
stringList.addToTail("Allison");
stringList.addToTail("Daniel");
stringList.addToTail("George");
stringList.addToTail("Simon");
stringList.addToTail("Mark");
// [Peter, James, Jason, John, Michael, Allison, Daniel, George, Simon,
// Mark]
System.out.println(stringList);
System.out.println("Removing from tail.");
//Mark
System.out.println(stringList.removeFromTail()+" is removed");
System.out.println("List: "+stringList);
System.out.println("Removing from middle.");
//Michael
System.out.println(stringList.removeFromMiddle()+" is removed");
System.out.println("List: "+stringList);
System.out.println("Removing from head.");
//Peter
System.out.println(stringList.removeFromHead()+" is removed");
System.out.println("List: "+stringList);
Scanner scanner=new Scanner(System.in);
System.out.println("Enter a name to search: ");
String name=scanner.nextLine();
if(stringList.contains(name)){
System.out.println(name+" exists on the list");
}else{
System.out.println(name+" does not exist on the list");
}
}
}
/*OUTPUT*/
Initial list: []
Adding a few names to the tail
[James, John, Michael]
Adding a name to the head
[Peter, James, John, Michael]
Adding a name to the middle
[Peter, James, Jason, John, Michael]
Adding a few more names to the tail
[Peter, James, Jason, John, Michael, Allison, Daniel, George, Simon, Mark]
Removing from tail.
Mark is removed
List: [Peter, James, Jason, John, Michael, Allison, Daniel, George, Simon]
Removing from middle.
Michael is removed
List: [Peter, James, Jason, John, Allison, Daniel, George, Simon]
Removing from head.
Peter is removed
List: [James, Jason, John, Allison, Daniel, George, Simon]
Enter a name to search:
Allison
Allison exists on the list
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.