|
-
linkedlist Node
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;
}
int getElement()
{
return element;
}
Node getNext()
{
return next;
}
void display()
{
System.out.println(element);
}
void remove(int targetElement)
{
System.out.println(targetElement);
}
}
// 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,targetElement;
do {
screen.println("1. Add 2. Display 3.Remove 4. 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 : screen.println("Remove element ");
element = targetElement;
list.remove(targetElement);
break;
case 4 : screen.println("Quitting");
break;
default: screen.println("Invalid choice");
}
} while (choice != 4);
}
}
// IntList class
public class IntList
{
Node head;
// Constructor
IntList()
{
head = new Node(0, null);
}
// Add
void add(int newElement)
{
Node newNode = new Node(newElement, null);
if (head.next == null)
head.next = newNode;
else
if (newNode.element < head.next.element)
{
newNode.next = head.next;
head.next = newNode;
}
else
{
Node probe;
for (probe = head.next;probe.next != null && newNode.element > probe.next.element;
probe = probe.next);
newNode.next = probe.next;
probe.next = newNode;
}
}
// Display
void display()
{
for (Node probe = head.next;probe != null;probe = probe.next)
probe.display();
}
//remove
void remove(int targetElement) {
Node probe, previous;
if (head.next.element == targetElement)
head.next = head.next.next;
else {
previous = head.next;
probe = head.next.next;
while (probe != null && probe.element != targetElement) {
previous = probe;
probe = probe.next;
}
if (probe != null)
previous.next = probe.next;
}
}
}
best wishes charliebhoy
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks