Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.util.Scanner; /** * Question : * * Finish the implementation of the

ID: 3691382 • Letter: I

Question

import java.util.Scanner;

/**
* Question :
*
* Finish the implementation of the delete() method.
* This method will remove the first occurrence of the given value
* from the list. The method will return true if the element is
* in the list and false otherwise.
*
* For instance, given the list
* 1 2 3
* delete(5) would return false and the list would be unchanged
* given the same list, delete(2) would return true and the list
* would look like:
* 1 3
*
* You will also need to add the "addToEnd()" method that you
* wrote in question 5. You can simply copy and paste that
* into this class.
*
* Only add code in between the comments that say
* "Begin your code here"
* and
* "End your code here"
*
* or immediately after
* "Paste addToEnd() method here"
*/

public class LinkedList {
   // Private inner class Node
  
   private class Node{
       int data;
   Node link;
  
   @SuppressWarnings("unused")
       public Node(){
       data = Integer.MIN_VALUE;
       link = null;
   }
  
   public Node(int x, Node p){
       data = x;
       link = p;
   }
   }
   // End of Node class
  
   public Node head;
  
   public LinkedList(){
       head = null;
   }
  
   public boolean delete(int data){
       /** Begin your code here **/
      
       /** end your code here **/
   }
  
   /** Paste addToEnd() method here **/
   public void addToEnd(int data) {
       /** Begin your code here **/
       if(head == null)
       {
       head = new Node(data, head);
       }
       else
       {
       Node position = head;
       while(position.link != null)
       {
       position = position.link;
       }
       position.link = new Node(data, null);
       }
       /** end your code here **/
   }

   public void printList(){
       Node position = head;
       if (head == null)
           System.out.println("The list is empty.");
       while(position != null){
           System.out.print(position.data + " ");
           position = position.link;
       }
       System.out.println();
   }
  
   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       LinkedList list = new LinkedList();
       System.out.println("Attempting to delete from an empty list");
       if (list.delete(5)) {
           System.out.println("Delete worked!");
       } else {
           System.out.println("Couldn't delete");
       }
      
       System.out.println("How many values would you like to add to the list?");
       int toAdd = input.nextInt();
      
       for(int i = 0; i < toAdd; i++) {
           System.out.println("Enter value " + (i + 1));
           list.addToEnd(input.nextInt());
       }
      
       System.out.println("The list is:");
       list.printList();
      
       System.out.println("Enter a value to remove from the list");
       int toRemove = input.nextInt();
       System.out.println("You entered " + toRemove);
       if(list.delete(toRemove)) {
           System.out.println(toRemove + " was removed from the list");
       } else {
           System.out.println(toRemove + " was not in the list");
       }
       System.out.println("The list is: ");
       list.printList();
      
       System.out.println("Enter another value to remove from the list");
       toRemove = input.nextInt();
       System.out.println("You entered " + toRemove);
       if(list.delete(toRemove)) {
           System.out.println(toRemove + " was removed from the list");
       } else {
           System.out.println(toRemove + " was not in the list");
       }
       System.out.println("The list is: ");
       list.printList();
      
       System.out.println("Enter another value to remove from the list");
       toRemove = input.nextInt();
       System.out.println("You entered " + toRemove);
       if(list.delete(toRemove)) {
           System.out.println(toRemove + " was removed from the list");
       } else {
           System.out.println(toRemove + " was not in the list");
       }
       System.out.println("The list is: ");
       list.printList();
      
       input.close();
   }
}

Explanation / Answer

LinkedList.java

import java.util.Scanner;
/**
* Question :
*
* Finish the implementation of the delete() method.
* This method will remove the first occurrence of the given value
* from the list. The method will return true if the element is
* in the list and false otherwise.
*
* For instance, given the list
* 1 2 3
* delete(5) would return false and the list would be unchanged
* given the same list, delete(2) would return true and the list
* would look like:
* 1 3
*
* You will also need to add the "addToEnd()" method that you
* wrote in question 5. You can simply copy and paste that
* into this class.
*
* Only add code in between the comments that say
* "Begin your code here"
* and
* "End your code here"
*
* or immediately after
* "Paste addToEnd() method here"
*/
public class LinkedList {
// Private inner class Node
  
private class Node{
int data;
Node link;
  
@SuppressWarnings("unused")
public Node(){
data = Integer.MIN_VALUE;
link = null;
}
  
public Node(int x, Node p){
data = x;
link = p;
}
}
// End of Node class
  
public Node head;
  
public LinkedList(){
head = null;
}
  
public boolean delete(int data){
Node position=head;
if(head==null)
{
return false;
}
if(position.data==data)
{
head=null;
return true;
}
while(position.link!=null)
{
System.out.println(position.data);
Node next=position.link;
if(next.data==data)
{
if(next.link!=null)
{
position.link=next.link;
}
else
{
position.link=null;
}
  
return true;
}
position=position.link;
}
return false;
}
  
/** Paste addToEnd() method here **/
public void addToEnd(int data) {
/** Begin your code here **/
if(head == null)
{
head = new Node(data, head);
}
else
{
Node position = head;
while(position.link != null)
{
position = position.link;
}
position.link = new Node(data, null);
}
/** end your code here **/
}
public void printList(){
Node position = head;
if (head == null)
System.out.println("The list is empty.");
while(position != null){
System.out.print(position.data + " ");
position = position.link;
}
System.out.println();
}
  
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
LinkedList list = new LinkedList();
System.out.println("Attempting to delete from an empty list");
if (list.delete(5)) {
System.out.println("Delete worked!");
} else {
System.out.println("Couldn't delete");
}
  
System.out.println("How many values would you like to add to the list?");
int toAdd = input.nextInt();
  
for(int i = 0; i < toAdd; i++) {
System.out.println("Enter value " + (i + 1));
list.addToEnd(input.nextInt());
}
  
System.out.println("The list is:");
list.printList();
  
System.out.println("Enter a value to remove from the list");
int toRemove = input.nextInt();
System.out.println("You entered " + toRemove);
if(list.delete(toRemove)) {
System.out.println(toRemove + " was removed from the list");
} else {
System.out.println(toRemove + " was not in the list");
}
System.out.println("The list is: ");
list.printList();
  
System.out.println("Enter another value to remove from the list");
toRemove = input.nextInt();
System.out.println("You entered " + toRemove);
if(list.delete(toRemove)) {
System.out.println(toRemove + " was removed from the list");
} else {
System.out.println(toRemove + " was not in the list");
}
System.out.println("The list is: ");
list.printList();
  
System.out.println("Enter another value to remove from the list");
toRemove = input.nextInt();
System.out.println("You entered " + toRemove);
if(list.delete(toRemove)) {
System.out.println(toRemove + " was removed from the list");
} else {
System.out.println(toRemove + " was not in the list");
}
System.out.println("The list is: ");
list.printList();
  
input.close();
}
}

Output:

Attempting to delete from an empty list

Couldn't delete

How many values would you like to add to the list?

3

Enter value 1

1

Enter value 2

2

Enter value 3

3

The list is:

1 2 3

Enter a value to remove from the list

2

You entered 2

1

2 was removed from the list

The list is:

1 3

Enter another value to remove from the list

5

You entered 5

1

5 was not in the list

The list is:

1 3

Enter another value to remove from the list

6

You entered 6

1

6 was not in the list

The list is:

1 3