DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4

Thread: linkedlist Node

  1. #1
    Join Date
    Oct 2004
    Location
    glasgow
    Posts
    8

    Unhappy 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

  2. #2
    Join Date
    Oct 2004
    Posts
    25
    The local variable may not have been initialized. You have to set the value of targetElement, e.g. targetElement =null;

    floaty

  3. #3
    Join Date
    Oct 2004
    Location
    glasgow
    Posts
    8
    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;
    best wishes charliebhoy

  4. #4
    Join Date
    Oct 2004
    Location
    glasgow
    Posts
    8
    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);
    }

    // Add
    void add(int newElement)
    {

    Node newNode = new Node(newElement, null);

    if (head.next == null)
    {
    head.next = newNode;
    tail.previous = newNode;
    } else {

    if (newNode.element < head.next.element)
    {
    newNode.next = head.next;
    head.next = newNode;
    tail.previous=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;
    tail.previous=newNode;
    }
    }
    }

    // Display
    void display()
    {
    for (Node probe = head.next; probe != null; probe = probe.next)

    probe.display();
    }

    // Display reverse
    void displayR()
    {
    for (Node probe = tail.previous; probe != null; probe = probe.previous)
    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


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


Sponsored Links