Question

In: Computer Science

A Linked List of Integers File IntList.java contains definitions for a linked list of integers. The...

  1. A Linked List of Integers

File IntList.java contains definitions for a linked list of integers. The class contains an inner class IntNode that holds information for a single node in the list (a node has a value and a reference to the next node) and the following IntList methods:

  • public IntList()—constructor; creates an empty list of integers
  • public void addToFront(int val)—takes an integer and puts it on the front of the list
  • public void addToEnd(int val)—takes an integer and puts it on the end of the list
  • public void removeFirst()—removes the first value from the list
  • public void print()—prints the elements in the list from first to last

File IntListTest.java contains a driver that allows you to experiment with these methods. Save both of these files to your directory, compile and run IntListTest, and play around with it to see how it works. Then add the following methods to the IntList class. For each, add an option to the driver to test it.

  1. public int length()—returns the number of elements in the list
  2. public String toString()—returns a String containing the print value of the list.
  3. public void removeLast()—removes the last element of the list. If the list is empty, does nothing.
  4. public void replace(int oldVal, int newVal)—replaces all occurrences of oldVal in the list with newVal.

Note that you can still use the old nodes; just replace the values stored in those nodes.

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

// FILE: IntList.java

//

// Purpose: Defines a class that represents a list of integers

//

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

public class IntList

{

private IntNode front; //first node in list

//-----------------------------------------

// Constructor. Initially list is empty.

//-----------------------------------------

public IntList()

{

front = null;

}

//-----------------------------------------

// Adds given integer to front of list.

//-----------------------------------------

public void addToFront(int val)

{

front = new IntNode(val,front);

}

//-----------------------------------------

// Adds given integer to end of list.

//-----------------------------------------

public void addToEnd(int val)

{

IntNode newnode = new IntNode(val,null);

//if list is empty, this will be the only node in it

if (front == null)

front = newnode;

else

{

//make temp point to last thing in list

IntNode temp = front;

while (temp.next != null)

temp = temp.next;

//link new node into list

temp.next = newnode;

}

}

//-----------------------------------------

// Removes the first node from the list.

// If the list is empty, does nothing.

//-----------------------------------------

public void removeFirst()

{

if (front != null)

front = front.next;

}

//------------------------------------------------

// Prints the list elements from first to last.

//------------------------------------------------

public void print()

{

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

System.out.print("List elements: ");

IntNode temp = front;

while (temp != null)

{

System.out.print(temp.val + " ");

temp = temp.next;

}

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

}

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

// An inner class that represents a node in the integer list.

// The public variables are accessed by the IntList class.

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

private class IntNode

{

public int val; //value stored in node

public IntNode next; //link to next node in list

//------------------------------------------------------------------

// Constructor; sets up the node given a value and IntNode reference

//------------------------------------------------------------------

public IntNode(int val, IntNode next)

{

this.val = val;

this.next = next;

}

}

}

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

// IntListTest.java

//

// Driver to test IntList methods.

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

import java.util.Scanner;

public class IntListTest

{

private static Scanner scan;

private static IntList list = new IntList();

//----------------------------------------------------------------

// Creates a list, then repeatedly prints the menu and does what

// the user asks until they quit.

//----------------------------------------------------------------

public static void main(String[] args)

{

scan = new Scanner(System.in);

printMenu();

int choice = scan.nextInt();

while (choice != 0)

{

dispatch(choice);

printMenu();

choice = scan.nextInt();

}

}

//----------------------------------------

// Does what the menu item calls for.

//----------------------------------------

public static void dispatch(int choice)

{

int newVal;

switch(choice)

{

case 0:

System.out.println("Bye!");

break;

case 1: //add to front

System.out.println("Enter integer to add to front");

newVal = scan.nextInt();

list.addToFront(newVal);

break;

case 2: //add to end

System.out.println("Enter integer to add to end");

newVal = scan.nextInt();

list.addToEnd(newVal);

break;

case 3: //remove first element

list.removeFirst();

break;

case 4: //print

list.print();

break;

default:

System.out.println("Sorry, invalid choice")

}

}

//-----------------------------------------

// Prints the user's choices

//-----------------------------------------

public static void printMenu()

{

System.out.println("\n Menu ");

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

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

System.out.println("1: Add an integer to the front of the list");

System.out.println("2: Add an integer to the end of the list");

System.out.println("3: Remove an integer from the front of the list");

System.out.println("4: Print the list");

System.out.print("\nEnter your choice: ");

}

}

Solutions

Expert Solution

Given below is the modified code for the question. Please do rate the answer if it helped . Thank you.
// ***************************************************************
// FILE: IntList.java
//
// Purpose: Defines a class that represents a list of integers
//
// ***************************************************************

public class IntList
{

   private IntNode front; //first node in list
   //-----------------------------------------
   // Constructor. Initially list is empty.
   //-----------------------------------------

   public IntList()

   {

       front = null;

   }

   //-----------------------------------------

   // Adds given integer to front of list.

   //-----------------------------------------

   public void addToFront(int val)

   {

       front = new IntNode(val,front);

   }

   //-----------------------------------------

   // Adds given integer to end of list.

   //-----------------------------------------

   public void addToEnd(int val)

   {

       IntNode newnode = new IntNode(val,null);

       //if list is empty, this will be the only node in it

       if (front == null)

           front = newnode;

       else

       {

           //make temp point to last thing in list

           IntNode temp = front;

           while (temp.next != null)

               temp = temp.next;

           //link new node into list

           temp.next = newnode;

       }

   }

   //-----------------------------------------

   // Removes the first node from the list.

   // If the list is empty, does nothing.

   //-----------------------------------------

   public void removeFirst()

   {

       if (front != null)

           front = front.next;

   }

   //------------------------------------------------

   // Prints the list elements from first to last.

   //------------------------------------------------

   public void print()

   {

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

       System.out.print("List elements: ");

       IntNode temp = front;

       while (temp != null)

       {

           System.out.print(temp.val + " ");

           temp = temp.next;

       }

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

   }

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

   // An inner class that represents a node in the integer list.

   // The public variables are accessed by the IntList class.

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

   private class IntNode

   {

       public int val; //value stored in node

       public IntNode next; //link to next node in list

       //------------------------------------------------------------------

       // Constructor; sets up the node given a value and IntNode reference

       //------------------------------------------------------------------

       public IntNode(int val, IntNode next)

       {

           this.val = val;

           this.next = next;

       }

   }
  
   public int length() {
       int len = 0;
       for(IntNode n = front; n != null; n = n.next)
           len++;
       return len;
   }
  
   public String toString() {
       String s = "";
      
       if(front != null) {
           s += front.val;
           for(IntNode n = front.next; n != null; n = n.next)
               s += "," + n.val;
       }
       return s;
   }

  
   public void removeLast() {
       if(front == null) //empty list
           return;
       else if(front.next == null) //only one node
           front = null;
       else {
           IntNode n = front;
           while(n.next.next != null) //till last but one
               n = n.next;
           n.next = null;
       }
   }
      
   public void replace(int oldVal, int newVal) {
       for(IntNode n = front; n != null; n = n.next) {
           if(n.val == oldVal)
               n.val = newVal;
       }
   }
}


output
-----

Menu
====
0: Quit
1: Add an integer to the front of the list
2: Add an integer to the end of the list
3: Remove an integer from the front of the list
4: Remove an integer from the end of the list
5: Replace a value with new value
6: Print list length
7: Print the list

Enter your choice: 1
Enter integer to add to front
1

Menu
====
0: Quit
1: Add an integer to the front of the list
2: Add an integer to the end of the list
3: Remove an integer from the front of the list
4: Remove an integer from the end of the list
5: Replace a value with new value
6: Print list length
7: Print the list

Enter your choice: 2
Enter integer to add to end
2

Menu
====
0: Quit
1: Add an integer to the front of the list
2: Add an integer to the end of the list
3: Remove an integer from the front of the list
4: Remove an integer from the end of the list
5: Replace a value with new value
6: Print list length
7: Print the list

Enter your choice: 2
Enter integer to add to end
3

Menu
====
0: Quit
1: Add an integer to the front of the list
2: Add an integer to the end of the list
3: Remove an integer from the front of the list
4: Remove an integer from the end of the list
5: Replace a value with new value
6: Print list length
7: Print the list

Enter your choice: 2
Enter integer to add to end
4

Menu
====
0: Quit
1: Add an integer to the front of the list
2: Add an integer to the end of the list
3: Remove an integer from the front of the list
4: Remove an integer from the end of the list
5: Replace a value with new value
6: Print list length
7: Print the list

Enter your choice: 6
List length = 4

Menu
====
0: Quit
1: Add an integer to the front of the list
2: Add an integer to the end of the list
3: Remove an integer from the front of the list
4: Remove an integer from the end of the list
5: Replace a value with new value
6: Print list length
7: Print the list

Enter your choice: 7
List elements: 1,2,3,4

Menu
====
0: Quit
1: Add an integer to the front of the list
2: Add an integer to the end of the list
3: Remove an integer from the front of the list
4: Remove an integer from the end of the list
5: Replace a value with new value
6: Print list length
7: Print the list

Enter your choice: 5
Enter old value to be replaced: 4
Enter new value: 8

Menu
====
0: Quit
1: Add an integer to the front of the list
2: Add an integer to the end of the list
3: Remove an integer from the front of the list
4: Remove an integer from the end of the list
5: Replace a value with new value
6: Print list length
7: Print the list

Enter your choice: 7
List elements: 1,2,3,8

Menu
====
0: Quit
1: Add an integer to the front of the list
2: Add an integer to the end of the list
3: Remove an integer from the front of the list
4: Remove an integer from the end of the list
5: Replace a value with new value
6: Print list length
7: Print the list

Enter your choice: 4

Menu
====
0: Quit
1: Add an integer to the front of the list
2: Add an integer to the end of the list
3: Remove an integer from the front of the list
4: Remove an integer from the end of the list
5: Replace a value with new value
6: Print list length
7: Print the list

Enter your choice: 7
List elements: 1,2,3

Menu
====
0: Quit
1: Add an integer to the front of the list
2: Add an integer to the end of the list
3: Remove an integer from the front of the list
4: Remove an integer from the end of the list
5: Replace a value with new value
6: Print list length
7: Print the list

Enter your choice: 6
List length = 3

Menu
====
0: Quit
1: Add an integer to the front of the list
2: Add an integer to the end of the list
3: Remove an integer from the front of the list
4: Remove an integer from the end of the list
5: Replace a value with new value
6: Print list length
7: Print the list

Enter your choice: 3

Menu
====
0: Quit
1: Add an integer to the front of the list
2: Add an integer to the end of the list
3: Remove an integer from the front of the list
4: Remove an integer from the end of the list
5: Replace a value with new value
6: Print list length
7: Print the list

Enter your choice: 7
List elements: 2,3

Menu
====
0: Quit
1: Add an integer to the front of the list
2: Add an integer to the end of the list
3: Remove an integer from the front of the list
4: Remove an integer from the end of the list
5: Replace a value with new value
6: Print list length
7: Print the list

Enter your choice: 0


Related Solutions

C++ language or Python. Linked Lists You are given a linked list that contains N integers....
C++ language or Python. Linked Lists You are given a linked list that contains N integers. You are to perform the following reverse operation on the list: Select all the subparts of the list that contain only even integers. For example, if the list is {1,2,8,9,12,16}, then the selected subparts will be {2,8}, {12,16}. Reverse the selected subpart such as {8,2} and {16,12}. The list should now be {1,8,2,9,16,12}. Your node definition should consist of 2 elements: the integer value...
Given a singly linked list that contains a sequence of integers, write a method that loop...
Given a singly linked list that contains a sequence of integers, write a method that loop through each elements in this singly linked list with O(n) time complexity, and let each elements multiply 6, return the result. code needed in java! thanks in advance!
The file supplied.o contains code that can build, display, and destroy a linear linked list (singly-linked)....
The file supplied.o contains code that can build, display, and destroy a linear linked list (singly-linked). For this lab, you will need to write the following two functions in list.cpp, and add function prototypes for them to list.h. The provided main.cpp has calls to each of these functions commented out. As you write the functions, uncomment them from main.cpp. void reverse(node * head, node *& newHead) Recursively make a revserse copy of the source list with head where newhead is...
Given a linked list of integers, remove any nodes from the linked list that have values...
Given a linked list of integers, remove any nodes from the linked list that have values that have previously occurred in the linked list. Your function should return a reference to the head of the updated linked list. (In Python)
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT