Hello guys Im having some trouble with a java task im doing in my textbook. I ge
ID: 3642252 • Letter: H
Question
Hello guys
Im having some trouble with a java task im doing in my textbook. I get some parts but it allways contains in just error to the end. Its not much to show, its a bit mess...
Task:
// ***************************************************************
// 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(" ----------------------- ");
}
//*************************************************************
// 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(" 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(" Enter your choice: ");
}
}
Explanation / Answer
please rate.
I have also written functions for length, replace, toString, removelast
class IntNode{
private IntNode next;
private int data;
/** Constructor to create a new node. Node must contain value for data*/
public IntNode(int data){
this.data = data;
next = null;
}
/** Used to change data stored in node */
public void changeData(int data){
this.data = data;
}
/**
* Returns the data stored in node
* @return data
*/
public int getData(){
return data;
}
/**
* Returns the next node in linked list
* @return next IntNode
*/
public IntNode getNextNode(){
return next;
}
/**
* Use to change the next node of the current node
* @param next node
*/
public void setNextNode(IntNode next){
this.next = next;
}
}
/**
* The Class IntList.
*/
public class IntList {
/** The dummy head node. Not used to store any user data */
private IntNode head;
/**
* Instantiates a new int list.
*/
IntList(){
head = new IntNode(0);
}
/**
* Adds the to front. Creates a new node with given value.
* Sets the next value to value pointed by head.
* Changes head to point to new node.
*
* @param val the val
*/
public void addToFront(int val){
IntNode temp = new IntNode(val);
temp.setNextNode(head.getNextNode());
head.setNextNode(temp);
}
/**
* Adds the to end.
* Traverse the list and add to end.
* @param val the val
*/
public void addToEnd(int val){
IntNode temp = head;
while(temp.getNextNode()!=null){
temp = temp.getNextNode();
}
IntNode newNode = new IntNode(val);
temp.setNextNode(newNode);
}
/**
* Removes the first.
* Make head point to next element.
*/
public void removeFirst(){
IntNode temp = head.getNextNode();
head.setNextNode(temp.getNextNode());
System.out.println("Removed "+temp.getData()+" from IntList");
}
/**
* Prints the values in linked list.
*/
public void print(){
IntNode temp = head;
System.out.println("Data in IntList");
while(temp.getNextNode()!=null){
temp = temp.getNextNode();
System.out.println(temp.getData());
}
}
public int getLength(){
int length = 0;
IntNode temp = head;
while(temp.getNextNode()!=null){
temp = temp.getNextNode();
length ++;
}
return length;
}
public void removeLast(){
IntNode temp = head;
IntNode prev = null;
System.out.println("Data in IntList");
while(temp.getNextNode()!=null){
prev = temp;
temp = temp.getNextNode();
}
if(prev!=head){
prev.setNextNode(null);
}
}
public String toString(){
String values="";
IntNode temp = head;
while(temp.getNextNode()!=null){
temp = temp.getNextNode();
values += temp.getData()+ " ";
}
return values;
}
public void replace(int oldData,int newData){
IntNode temp = head;
while(temp.getNextNode()!=null){
temp = temp.getNextNode();
if(temp.getData()==oldData){
temp.changeData(newData);
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.