Results 1 to 3 of 3

Thread: LinkedList-java

  1. #1
    kamali is offline Member
    Join Date
    Sep 2008
    Posts
    1
    Rep Power
    0

    Default LinkedList-java

    Given the following classes, i need to implement the delete() method, which will delete all nodes equal() to Node n. delete() should return true if at least one node could be removed, and return false otherwise. Use Java syntax.Don’t use java API.

    Public class DS {

    public Node getStart() {
    /* This method is written for you – it will return the first node */
    }

    public void setStart(Node n) {
    /* This method is written for you – it will set the first node */
    }

    public boolean delete(Node n) { /* Provide code for this method */ }

    class Node {

    public boolean equal(Node n) { /* This method is written for you */ }

    public Node getNext() {
    /* This method is written for you – it will return the next node*/
    }

    public Node getPrevious() {
    /* This method is written for you – it will return the previous node*/
    }

    public void setNext(Node n) {
    /* This method is written for you – it will set the next node*/
    }

    public void setPrevious(Node n) {
    /* This method is written for you – it will set the previous node*/
    }

    }
    }
    ================================================== =====

    here i'm submitting the code for delete(Node n).I'm not getting the correct results.Plz help me.
    ================================================== =====

    public boolean delete(Node n) { /* Provide code for this method */ }
    {
    LinkedList myList = new LinkedList();
    int var = 0;

    Node head = new DS().getStart();
    if (head.equal(n) )
    {
    head = head.getNext();
    setStart(head);
    var++;
    }

    Node n1 = null;

    for (Node l = new DS().getStart() ; l != null; l = l.getNext())
    { if n1==null
    if (l.equal(n))
    {
    n1.setNext(l.getNext());
    var++;

    }
    }
    if (var > 0)
    return true;
    else
    return false;

    }

  2. #2
    DevzAbhi's Avatar
    DevzAbhi is offline Member
    Join Date
    Jul 2008
    Posts
    67
    Rep Power
    0

    Default

    Quote Originally Posted by kamali View Post
    Node n1 = null;

    for (Node l = new DS().getStart() ; l != null; l = l.getNext())
    { if n1==null
    if (l.equal(n))
    {
    n1.setNext(l.getNext());
    var++;

    }
    }
    }

    { // if n1==null
    if (l.equal(n))
    { n1=i.getPrevious();
    n1.setNext(l.getNext());
    var++;
    i=n1;
    }

    }
    }

    i think this should help;

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    I'm not getting the correct results
    Can you explain a bit more. Show the output and describe what's wrong with it.
    A problem could be that the following creates a new object that is probably empty. There wouldn't be any starting element to get.
    new DS().getStart()

Similar Threads

  1. Problem with LinkedList Java
    By paul in forum Advanced Java
    Replies: 2
    Last Post: 08-07-2007, 05:57 AM
  2. Please help me with LinkedList in Java
    By lenny in forum New To Java
    Replies: 2
    Last Post: 07-31-2007, 03:24 PM
  3. how to use LinkedList
    By fred in forum Advanced Java
    Replies: 1
    Last Post: 07-24-2007, 02:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •