Question
I am having trouble with the following code. Everything is working except for the addAfter method in the IntList class. it asks the user to
I am having trouble with the following code. Everything is working except for the addAfter method in the IntList class. it asks the user to add a number after the first instance of a number of their choice (ex. add a 7 after the first 3 that is found). It should give a message when the number being added after isnt in the list, but it doesnt. the whole method doesnt really work. can you copy and paste the FinalList and IntList into netbeans / java program to redo the addAfter method and make it work? thanks!
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 back: ");
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;
//cases for two newly added methods
case 6:
System.out.print("Enter value to be removed: ");
value = scan.nextInt();
myList.remove(value);
break;
case 7:
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");
//new options
System.out.println("6 - Remove first instance");
System.out.println("7 - Add a value after");
}
}
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
}
}
// 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started