Results 1 to 7 of 7
  1. #1
    jputneyCS is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Exclamation help with determing if a node is duplicate and if so prevent from adding to list

    //my code works perfectly, i just need to stop it from adding duplicate nodes. any help, ill be extremely thankful.



    public class OrderedList<T extends Comparable<? super T>> extends List<T>
    {
    public void add(T o)
    {
    if(head == null)
    {
    head = new ListNode<T>(o, head);
    }
    else
    {
    if(head.data.compareTo(o)>0)
    {
    head = new ListNode<T>(o, head);
    }
    else
    {
    ListNode<T> current = new ListNode<T>(head.data, head);
    ListNode<T> previous = new ListNode<T>(null, head);
    boolean add = true;

    while(current!= null && current.data.compareTo(o)<0)
    {
    previous = current;
    current = current.next;
    }
    // i think this is where i need to check for duplicity

    previous.next = new ListNode<T>(o,current);

    }
    }
    }
    }
    Last edited by jputneyCS; 10-08-2010 at 08:26 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    I think you're right: once you've moved the current pointer so that previous and all others (towards the head) are strictly greater than current then check previous.next. If it is equal to current then the method can end (throw an exception or whatever).

    (I'm assuming that by duplicate you mean "compares equal" - this is the semantics that Set uses. And your OrderedList is more or less a SortedSet.)

    Also, unless I'm missing something your line

    Java Code:
    previous.next = new ListNode<T>(o,current);

    adds current in the correct position in the list. But what happens to the rest of the list - the ones that used to follow previous? Won't they be "forgotten"?
    Last edited by pbrockway2; 10-08-2010 at 08:47 AM.

  3. #3
    jputneyCS is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    im using the code to find the position that i should add the element thats being passed into the list. when i run it all of my elements print out correctly. i just have to exclude elements thats already in the list. sorry if i dont understand what you mean by losing previous data.....

    while(current!= null && current.data.compareTo(o)<0)
    {
    previous = current;
    current = current.next;
    }

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Sorry I've woken up! It's the ListNode that has a reference to the next node, right?

    In that case it all looks good. Just check, at the place you marked, the value in the current node and o. If they are equal just return or throw an exception.

  5. #5
    jputneyCS is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    ok, i added the conditon. i think what im trying to do is if the node after the positon i want to add is not equal to the node im adding then add.....but its throwing a nullPointerException on the condition


    while(current!= null && current.data.compareTo(o)<0)
    {
    previous = current;
    current = current.next;
    }
    // i think this is where i need to check for duplicity
    if(!current.equals(o))
    previous.next = new ListNode<T>(o,current);

  6. #6
    jputneyCS is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    ok, i figured it out thanks very much. i ended up using the condition
    while(current!= null && current.data.compareTo(o)<0)
    {
    previous = current;
    current = current.next;
    }
    // i think this is where i need to check for duplicity
    if(current == null || !current.data.equals(o))
    previous.next = new ListNode<T>(o,current);

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Great - I'm glad you've got it figured out. current==null is the case where you have reached the end of the list.

Similar Threads

  1. Linked List node class help!!
    By SteroidalPsycho in forum New To Java
    Replies: 0
    Last Post: 05-03-2010, 02:21 AM
  2. Replies: 0
    Last Post: 03-15-2010, 10:14 PM
  3. Adding a node with a TreeViewer; what's wrong?
    By Rodrigo Braz in forum SWT / JFace
    Replies: 1
    Last Post: 04-24-2009, 08:43 AM
  4. Replies: 6
    Last Post: 04-13-2009, 05:19 AM
  5. How to prevent duplicate username entry in database?
    By anki1234 in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 01-09-2008, 09:02 AM

Tags for this Thread

Posting Permissions

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