Hi I dont know were I have gone wrong maybe as usual its something so simple and I cant see the wood for the tree, I am having problems with the remove function , I have tried many variations but with no joy, I get an error when compiling the
Test class file, the problem area is
case 3 : screen.println("Remove element ");
element = targetElement;
list.remove(targetElement);
I hope someone can see my error and correct me pls
here is the full code below............................
import java.lang.Exception;
// Node class
class Node
{
int element;
Node next;
// Constructor
Node(int newElement, Node newNode)
{
element = newElement;
next = newNode;
}
The local variable may not have been initialized. You have to set the value of targetElement, e.g. targetElement =null;
floaty
11-01-2004, 12:50 PM
charliebhoy
Hi floaty
txs 4 your help, got it working now
here is the sorted part
case 3 : screen.println("Enter Value to Remove");
element = new Integer (keyboard.readLine()).intValue();
list.remove(element);
break;
11-09-2004, 10:36 AM
charliebhoy
Hi again,
I have updated the program to a doubly linked list
but were printing backwords it only prints first char (last char as its going backwards)
does anyone have any idea whats up ?
below are the 3 files................................
// Test class
import java.io.*;
public class IntListTest
{
static BufferedReader keyboard = new
BufferedReader(new InputStreamReader(System.in));
static PrintWriter screen = new PrintWriter(System.out,true);
public static void main(String[] args) throws IOException
{
IntList list = new IntList();
int choice,element;
do {
screen.println();
screen.println();
screen.println( "1. Add 2. Display 3.Reverse 4.Remove 5.Quit");
screen.print( "Enter choice ");
choice = new Integer(keyboard.readLine()).intValue();
switch (choice)
{
case 1 : screen.println("Enter new value ");
element = new
Integer(keyboard.readLine()).intValue();
list.add(element);
break;
case 2 : list.display();
break;
case 3 : list.displayR();
break;
case 4: screen.println("Enter Value to Remove");
element = new Integer(keyboard.readLine()).intValue();
list.remove(element);
break;
case 5 : screen.println("Quitting");
break;
default: screen.println("Invalid choice");
}
} while (choice != 4);
}
}
// Test class
import java.io.*;
public class IntListTest
{
static BufferedReader keyboard = new
BufferedReader(new InputStreamReader(System.in));
static PrintWriter screen = new PrintWriter(System.out,true);
public static void main(String[] args) throws IOException
{
IntList list = new IntList();
int choice,element;
do {
screen.println();
screen.println();
screen.println( "1. Add 2. Display 3.Reverse 4.Remove 5.Quit");
screen.print( "Enter choice ");
choice = new Integer(keyboard.readLine()).intValue();
switch (choice)
{
case 1 : screen.println("Enter new value ");
element = new
Integer(keyboard.readLine()).intValue();
list.add(element);
break;
case 2 : list.display();
break;
case 3 : list.displayR();
break;
case 4: screen.println("Enter Value to Remove");
element = new Integer(keyboard.readLine()).intValue();
list.remove(element);
break;
case 5 : screen.println("Quitting");
break;
default: screen.println("Invalid choice");
}
} while (choice != 4);
}
}
// IntList class
public class IntList
{
Node head,tail,current;
// Constructor
IntList()
{
head = new Node(0, null);
tail= new Node(0,null);
}