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

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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!