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

Given the code following the instructions, have your program complete the follow

ID: 3718377 • Letter: G

Question

Given the code following the instructions, have your program complete the following:

Add two (2) methods to your program. You need to adjust the program to offer these options within your menu and correctly execute them.Create a method that removes the first instance of a value from within the list. (This means if there are multiple fours (4) in the linked list, the first one encountered would be removed.) Feedback should be provided if the value to be removed is not found.

Method name – remove

#8 on the menu

Create a method that adds a given value within the list after a specified number (ex. add a 7 after the first 3 that is found). Feedback should be provided if the value which will have the new number added after it is not found.

Method name – addAfter

#9 on the menu

NOTES:

The program should be fully functional

The program should be thoroughly commented to explain variables and what functionality various lines/small sections of code are completing as related to the overall execution of the program. copy this code into netbeans or a java program and execute do what is said is above please:

package FinalList;
import java.util.*;
public class FinalList {

public static void main(String[] args)
{
IntList myList = new IntList();
  
Scanner scan = new Scanner(System.in);
  
int choice;
  
int value;
  
//message to user
System.out.println("The following program creates and manages a linked list. ");
  
//print menu
printMenu();
  
//prompt user for choice and read it
System.out.println("Please enter your selections: ");
choice = scan.nextInt();
  
//while loop (until user wants to quit - enters 0)
while(choice != 0)
{
//in while loop we switch to manage method calls
switch(choice)
{
case 0: //quit
break;
case 1: //add to back
System.out.println("Please enter the value to add to the front: ");
value = scan.nextInt();
myList.addToBack(value);
break;
case 2: //add to front
System.out.println("Please enter the value to add to the front: ");
value = scan.nextInt();
myList.addToFront(value);
break;
case 3:
myList.removeFromBack(); //remove from back
break;
case 4:
myList.removeFromFront(); //remove fron front
break;
case 5:
System.out.println(myList);   
break;  
case 6:
System.out.println("There are " +myList.count()+ " nodes in the list. ");   
break;
default:
System.out.println("Invalid selection. Try again. ");   
break;
}
//print menu
printMenu();
  
//prompt for menu selection
System.out.print("PLease enter your selection: ");
choice = scan.nextInt();
}
  
}
  
public static void printMenu()
{

//method prints menu with updated list of funcionality
System.out.println(" Linked List Functions");
System.out.println("-----------------------");
System.out.println("Q - Quit");
System.out.println("1 - Add to back of list");
System.out.println("2 - Add to front of list");
System.out.println("3 - Remove from back of list");
System.out.println("4 - Remove from front of list");
System.out.println("5 - Print the list");
  
}
}

package FinalList;

public class IntList
{
//instance data
private IntNode list; //list pointer
  
//constructor method
public IntList()
{
list = null; //begin with empty list
}
  
//add to back method
public void addToBack(int value)
{
IntNode node = new IntNode(value); //build a new node with data provided
  
IntNode current; //traversing pointer
  
if(list ==null) //the list is empty
{
list = node; //look at the only node, just built
}
else
{
//traverse the list until we find the last node
current = list; //start looking in the front
  
while(current.next != null) //haven't found the last one
{
current = current.next; //move current to the next node
}
  
current.next = node;
}
}
  
//add to front method
public void addToFront(int value)
{
IntNode node = new IntNode(value); //build a new node with data provided

if(list ==null) //the list is empty
{
list = node; //look at the only node, just built
}
else
{
node.next=list;
list=node;
}
}
  
//toString method - also serves as print list method
public void removeFromFront()
{
if(list == null) //empty list
{
System.out.println("The list is empty, no node to remove. ");
}
else
{
if(list.next == null) //list conains only 1 node
{
list = list.next;
}
else //more than 1 node
{
list = list.next;
}
}
}
  
//remove from back method
public void removeFromBack()
{
IntNode current=list;
if(list == null)   
{
System.out.println("The list is empty, no node to remove. ");
}
else
{
if(current.next==null)
{
list = null;
}
else   
{
IntNode previous=current;
while(current.next != null)
{
previous=current;
current=current.next;
}
}
}
}
  
public void addFromFront(int value)
{
IntNode node = new IntNode(value); //build a new node with date provided
  
if(list == null) //empty list
{
list = node; //empty node
}
else
{
node.next = list; //point new node to former first node
list = node; //have list point to new node
}
}
  
public int count()
{
int nodes=0;
  
IntNode current = list;
  
if(list != null)
{
nodes = 0;
}
else
{
while(current.next != null)
{
nodes++;
current = current.next;
}
nodes++;
}
  
return nodes;
}
  
//empty method
public boolean empty()
{
return (list == null);
}
  
  
//toString method
public String toString()
{
String result = "The list - "; //list header
  
IntNode current; //traversing pointer
  
if(list == null) //the list is empty
{
result+= "is empty. ";
}
else
{
result+=" "; //move cursor to next line
current = list; //start at the front   
while(current != null)
{
result+=current.value;
result+=" ";
current = current.next;
}
}
  
return result;
}
  
//************************************************
//IntNode class
//************************************************
private class IntNode
{
public int value;
public IntNode next;
  
//constructor method
public IntNode(int nodeValue)
{
value = nodeValue;
next = null;
}
}
}

Explanation / Answer

Hello, I have completed the two required methods for you. And your removeFromBack() method was not working properly, fixed that also. Drop a comment if you have any doubts. Thanks.

EDIT: I’m getting troubles submitting the answer without losing the format. Showing character limit exceeded error. So I have to paste it as a plain text, which will cause the loss of code formatting and indentations. Sorry for the trouble. If you are using eclipse ,copy the code and press ctrl+shift+F to format the code

// IntList.java

public class IntList {

// instance data

private IntNode list; // list pointer

// constructor method

public IntList() {

list = null; // begin with empty list

}

// add to back method

public void addToBack(int value) {

IntNode node = new IntNode(value); // build a new node with data

// provided

IntNode current; // traversing pointer

if (list == null) // the list is empty

{

list = node; // look at the only node, just built

} else {

// traverse the list until we find the last node

current = list; // start looking in the front

while (current.next != null) // haven't found the last one

{

current = current.next; // move current to the next node

}

current.next = node;

}

}

// add to front method

public void addToFront(int value) {

IntNode node = new IntNode(value); // build a new node with data

// provided

if (list == null) // the list is empty

{

list = node; // look at the only node, just built

} else {

node.next = list;

list = node;

}

}

// toString method - also serves as print list method

public void removeFromFront() {

if (list == null) // empty list

{

System.out.println("The list is empty, no node to remove. ");

} else {

if (list.next == null) // list conains only 1 node

{

list = list.next;

} else // more than 1 node

{

list = list.next;

}

}

}

// remove from back method

public void removeFromBack() {

IntNode current = list;

if (list == null) {

System.out.println("The list is empty, no node to remove. ");

} else {

if (current.next == null) {

list = null;

} else {

IntNode previous = current;

while (current.next != null) {

previous = current;

current = current.next;

}

previous.next = null;

}

}

}

/**

* method to remove a value from the list

* @param value - value to be removed

*/

public void remove(int value) {

boolean found = false;

IntNode current;

IntNode prev = list;

if (prev != null && prev.value == value) {

removeFromFront();

found = true;

} else {

while (prev != null) {

current = prev.next;

if (current != null && current.value == value) {

found = true;

prev.next = current.next;

break;

}

prev = prev.next;

}

}

if (found) {

System.out.println("Value found and removed!");

} else {

System.out.println("Value not found in the list!");

}

}

/**

* method to add a value after a specific value

* @param after- after which the new value should be added

* @param value - value to be added

*/

public void addAfter(int after, int value) {

boolean found = false;

IntNode current = list;

IntNode newNode = new IntNode(value);

while (current != null) {

if (current.value == after) {

found = true;

IntNode temp = current.next;

current.next = newNode;

newNode.next = temp;

break;

}

}

if (!found) {

System.out.println(after + " is not found in the list, " + value

+ " not added");

} else {

System.out.println(value + " is added after " + after);

}

}

public void addFromFront(int value) {

IntNode node = new IntNode(value); // build a new node with date

// provided

if (list == null) // empty list

{

list = node; // empty node

} else {

node.next = list; // point new node to former first node

list = node; // have list point to new node

}

}

public int count() {

int nodes = 0;

IntNode current = list;

if (list == null) {

nodes = 0;

} else {

while (current.next != null) {

nodes++;

current = current.next;

}

nodes++;

}

return nodes;

}

// empty method

public boolean empty() {

return (list == null);

}

// toString method

public String toString() {

String result = "The list - "; // list header

IntNode current; // traversing pointer

if (list == null) // the list is empty

{

result += "is empty. ";

} else {

result += " "; // move cursor to next line

current = list; // start at the front

while (current != null) {

result += current.value;

result += " ";

current = current.next;

}

}

return result;

}

// ************************************************

// IntNode class

// ************************************************

private class IntNode {

public int value;

public IntNode next;

// constructor method

public IntNode(int nodeValue) {

value = nodeValue;

next = null;

}

}

}

// FinalList.java

import java.util.*;

public class FinalList {

public static void main(String[] args) {

IntList myList = new IntList();

Scanner scan = new Scanner(System.in);

int choice;

int value;

// message to user

System.out

.println("The following program creates and manages a linked list. ");

// print menu

printMenu();

// prompt user for choice and read it

System.out.println("Please enter your selections: ");

choice = scan.nextInt();

// while loop (until user wants to quit - enters 0)

while (choice != 0) {

// in while loop we switch to manage method calls

switch (choice) {

case 0: // quit

break;

case 1: // add to back

System.out

.println("Please enter the value to add to the front: ");

value = scan.nextInt();

myList.addToBack(value);

break;

case 2: // add to front

System.out

.println("Please enter the value to add to the front: ");

value = scan.nextInt();

myList.addToFront(value);

break;

case 3:

myList.removeFromBack(); // remove from back

break;

case 4:

myList.removeFromFront(); // remove fron front

break;

case 5:

System.out.println(myList);

break;

case 6:

System.out.println("There are " + myList.count()

+ " nodes in the list. ");

break;

/**

* cases for two newly added methods

*/

case 7:

System.out.print("Enter value to be removed: ");

value = scan.nextInt();

myList.remove(value);

break;

case 8:

System.out.print("Enter value to be added: ");

value = scan.nextInt();

System.out.print("Enter the number after which "

+ "the above value should be added: ");

int after=scan.nextInt();

myList.addAfter(after, value);

break;

default:

System.out.println("Invalid selection. Try again. ");

break;

}

// print menu

printMenu();

// prompt for menu selection

System.out.print("PLease enter your selection: ");

choice = scan.nextInt();

}

}

public static void printMenu() {

// method prints menu with updated list of funcionality

System.out.println(" Linked List Functions");

System.out.println("-----------------------");

System.out.println("0 - Quit");

System.out.println("1 - Add to back of list");

System.out.println("2 - Add to front of list");

System.out.println("3 - Remove from back of list");

System.out.println("4 - Remove from front of list");

System.out.println("5 - Print the list");

System.out.println("6 - Current size");

//new options

System.out.println("7 - Remove an element");

System.out.println("8 - Add a value after");

}

}

/*OUTPUT*/

The following program creates and manages a linked list.

Linked List Functions

-----------------------

0 - Quit

1 - Add to back of list

2 - Add to front of list

3 - Remove from back of list

4 - Remove from front of list

5 - Print the list

6 - Current size

7 - Remove an element

8 - Add a value after

Please enter your selections:

1

Please enter the value to add to the front:

44

Linked List Functions

-----------------------

0 - Quit

1 - Add to back of list

2 - Add to front of list

3 - Remove from back of list

4 - Remove from front of list

5 - Print the list

6 - Current size

7 - Remove an element

8 - Add a value after

PLease enter your selection: 2

Please enter the value to add to the front:

77

Linked List Functions

-----------------------

0 - Quit

1 - Add to back of list

2 - Add to front of list

3 - Remove from back of list

4 - Remove from front of list

5 - Print the list

6 - Current size

7 - Remove an element

8 - Add a value after

PLease enter your selection: 5

The list -

77

44

Linked List Functions

-----------------------

0 - Quit

1 - Add to back of list

2 - Add to front of list

3 - Remove from back of list

4 - Remove from front of list

5 - Print the list

6 - Current size

7 - Remove an element

8 - Add a value after

PLease enter your selection: 8

Enter value to be added: 88

Enter the number after which the above value should be added: 77

88 is added after 77

Linked List Functions

-----------------------

0 - Quit

1 - Add to back of list

2 - Add to front of list

3 - Remove from back of list

4 - Remove from front of list

5 - Print the list

6 - Current size

7 - Remove an element

8 - Add a value after

PLease enter your selection: 5

The list -

77

88

44

Linked List Functions

-----------------------

0 - Quit

1 - Add to back of list

2 - Add to front of list

3 - Remove from back of list

4 - Remove from front of list

5 - Print the list

6 - Current size

7 - Remove an element

8 - Add a value after

PLease enter your selection: 7

Enter value to be removed: 89

Value not found in the list!

Linked List Functions

-----------------------

0 - Quit

1 - Add to back of list

2 - Add to front of list

3 - Remove from back of list

4 - Remove from front of list

5 - Print the list

6 - Current size

7 - Remove an element

8 - Add a value after

PLease enter your selection: 7

Enter value to be removed: 88

Value found and removed!

Linked List Functions

-----------------------

0 - Quit

1 - Add to back of list

2 - Add to front of list

3 - Remove from back of list

4 - Remove from front of list

5 - Print the list

6 - Current size

7 - Remove an element

8 - Add a value after

PLease enter your selection: 5

The list -

77

44

Linked List Functions

-----------------------

0 - Quit

1 - Add to back of list

2 - Add to front of list

3 - Remove from back of list

4 - Remove from front of list

5 - Print the list

6 - Current size

7 - Remove an element

8 - Add a value after

PLease enter your selection: 7

Enter value to be removed: 77

Value found and removed!

Linked List Functions

-----------------------

0 - Quit

1 - Add to back of list

2 - Add to front of list

3 - Remove from back of list

4 - Remove from front of list

5 - Print the list

6 - Current size

7 - Remove an element

8 - Add a value after

PLease enter your selection: 5

The list -

44

Linked List Functions

-----------------------

0 - Quit

1 - Add to back of list

2 - Add to front of list

3 - Remove from back of list

4 - Remove from front of list

5 - Print the list

6 - Current size

7 - Remove an element

8 - Add a value after

PLease enter your selection: 0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote