Question

In: Computer Science

Write the Java source code necessary to build a solution for the problem below: Create a...

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.

Solutions

Expert Solution

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

*/


Related Solutions

Write the Java source code necessary to build a solution for the problem below: Create a...
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...
only JAVA code /** Create a method as instructed below and then call it appropriately. */...
only JAVA code /** Create a method as instructed below and then call it appropriately. */ import java.util.Scanner; public class MoreBankCharges { //Constant declarations for base fee and per check fees //Class scope so constants are accessible by all methods static final double BASE_FEE = 10.0; static final double LESS_THAN_20_FEE = 0.10; static final double TWENTY_TO_THIRTYNINE_FEE = 0.08; static final double FORTY_TO_FIFTYNINE_FEE = 0.06; static final double SIXTY_OR_MORE_FEE = 0.04; public static void main(String[] args) { //Variable declarations int numChecks;...
Write a java code that first discards as many whitespace characters as necessary until the first...
Write a java code that first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace...
Create a new Java program named AllAboutMe (For JAVA we use blue J) Write code to...
Create a new Java program named AllAboutMe (For JAVA we use blue J) Write code to have the program print your name, favorite color, and three hobbies to a new text file called “AllAboutMe” using PrintStream. Submit code.
WRITE CODE IN JAVA it is now your turn to create a program of your choosing....
WRITE CODE IN JAVA it is now your turn to create a program of your choosing. If you are not sure where to begin, think of a task that you repeat often in your major that would benefit from a program. For example, chemistry unit conversions, finding the area for geometric shapes, etc. You can also create an interactive story that changes based on the user input/decisions. The possibilities are endless. The program must include instructions for the user. Be...
Java please! Write the code in Exercise.java to meet the following problem statement: Write a program...
Java please! Write the code in Exercise.java to meet the following problem statement: Write a program that will print the number of words, characters, and letters read as input. It is guaranteed that each input will consist of at least one line, and the program should stop reading input when either the end of the file is reached or a blank line of input is provided. Words are assumed to be any non-empty blocks of text separated by spaces, and...
Write a program that solves the Knapsack problem. Code to the following standards. Your source of...
Write a program that solves the Knapsack problem. Code to the following standards. Your source of items to put into the knapsack should consist of five randomly generated integers in the range of 1 to 20. Your knapsack can hold 20 lbs.
This is a programming assignment!!! Start with the Assignment3.java source code posted to Canvas. This code...
This is a programming assignment!!! Start with the Assignment3.java source code posted to Canvas. This code outputs the programmer’s name , prompts the user to enter two numbers (integers) and outputs their sum. Note: When you run the program, it expects you to enter two integer values (a counting or whole number like 1, 216, -35, or 0) Make the following changes/additions to the code: On line 11, change the String myName from “your full name goes here!!!” to your...
Write Java code for each of the following problem a. Two players, a and b, try...
Write Java code for each of the following problem a. Two players, a and b, try to guess the cost of an item. Whoever gets closest to the price without going over is the winner. Return the value of the best bid. If both players guessed too high, return -1. example: closestGuess(97, 91, 100) → 97 closestGuess(3, 51, 50) → 3 closestGuess(12, 11, 10) → -1 b. Given a non-empty string, return true if at least half of the characters...
Requirements: Code in C++. With given information, write the solution to this problem so that it...
Requirements: Code in C++. With given information, write the solution to this problem so that it is understandable to someone with basic knowledge of C++ (ex: only keep basic libraries, keep coding shortcuts to a minimum). Also leave comments in the code (plz), the logic behind solving this problem if possible, and explanation of what the keys to solving this problem is and how to run test cases to ensure correctness of code. Problem: For this problem you will compute...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT