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.

*************************************************************************************************************************

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();
}
}

Solutions

Expert Solution

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();

        }
}




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...
Write the Java source code necessary to build a solution for the problem below: The Fibonacci...
Write the Java source code necessary to build a solution for the problem below: The Fibonacci numbers form a sequence where each number is the sum of the previous two numbers. Starting from 0 and 1, the first eight Fibonacci numbers are built in the following list using the equation Fn = Fn-1 + Fn-2: 0, 0 0, 1 1, 1 1, 2 2, 3 3, 5 5, 8 8, 13 The sequence would be the numbers in red (0,...
Write in Java * Create a new client class called Plants.java * Write code in the...
Write in Java * Create a new client class called Plants.java * Write code in the Plants class that solves problems 1,2,3 and 4 * Include the solutions of the different problems in different methods and call them from the main method * Use the BagInterface.java and ArrayBag.java, but do not add any code Problem 1: Create a bag plantsCart, which holds the following spring seedlings(represented by String) Rose, Daisy, Cabbage, Cucumber, Carrot, Cucumber, Daffodil, Daisy, Rose, Iris, Rose, Spinach....
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...
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a...
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a method to test if a LinkedList<Long> has Long values that form a Fibonacci sequence from the beginning to the end and return true if it is and false otherwise. A sequence of values is Fibonnaci if every third value is equal to sum of the previous two. Eg., 3,4,7,11,18,29 is a Fibonacci sequence whereas 1,2,3,4 is not, because 2+3 is not equal to 4....
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.
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 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...
Write the code in Java: 1. Create a method that displays your name in the console....
Write the code in Java: 1. Create a method that displays your name in the console. This method is void and takes no parameters. Make an app that runs the method in response to a button press. 2. Create a version of the method in #1 that takes the text (String) to be displayed as a parameter. Allow the user to enter the text in a dialog box or text field and display that text in the console. Be sure...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT