Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

File IntList.java contains definitions for a linked list ofintegers. The class contains an inner class IntNode that holdsinformation for a single node in the list

File IntList.java contains definitions for a linked list ofintegers. The class contains an inner class IntNode that holdsinformation for a single node in the list (a node has a value and areference to the next node) and the following IntList methods:

? public IntList()—constructor; creates an empty list ofintegers

? public void addToFront(int val)—takes an integer and puts iton the front of the list

? public void addToEnd(int val)—takes an integer and puts it onthe end of the list

? public void removeFirst()—removes the first value from thelist

? public void print()—prints the elements in the list from firstto last

File IntListTest.java contains a driver that allows you toexperiment with these methods. Save both of these files to yourdirectory, compile and run IntListTest, and play around with it tosee how it works. Then add the following methods to the IntListclass. For each, add an option to the driver to test it.

1. public int length()—returns the number of elements in thelist

2. public String toString()—returns a String containing theprint value of the list.

3. public void removeLast()—removes the last element of thelist. If the list is empty, does nothing.

4. public void replace(int oldVal, int newVal)—replaces alloccurrences of oldVal in the list with newVal.

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

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

// FILE: IntList.java

//

// Purpose: Defines a class that represents a list ofintegers

//

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

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("-----------------------");

}

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

// An inner class that represents a node in the integerlist.

// 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 IntNodereference

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

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 doeswhat

// 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(" Menu ");

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

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

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

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

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

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

System.out.print("Enter your choice: ");

}

}

Step by Step Solution

3.40 Rating (144 Votes )

There are 3 Steps involved in it

Step: 1

To add the requested methods to the IntList class you can modify the code as follows java Copy code ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Structures and Algorithm Analysis in Java

Authors: Mark A. Weiss

3rd edition

132576279, 978-0132576277

More Books

Students also viewed these Programming questions

Question

=+ Identify the ethical dilemma in this scenario.

Answered: 1 week ago

Question

What are the 5 Cs of marketing channel structure?

Answered: 1 week ago