Results 1 to 7 of 7
- 10-08-2010, 07:10 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 4
- Rep Power
- 0
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 07:26 AM.
- 10-08-2010, 07:42 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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 07:47 AM.
- 10-08-2010, 07:55 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 4
- Rep Power
- 0
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;
}
- 10-08-2010, 08:07 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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.
- 10-08-2010, 08:25 AM #5
Member
- Join Date
- Oct 2010
- Posts
- 4
- Rep Power
- 0
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);
- 10-08-2010, 09:10 AM #6
Member
- Join Date
- Oct 2010
- Posts
- 4
- Rep Power
- 0
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);
- 10-09-2010, 12:30 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Similar Threads
-
Linked List node class help!!
By SteroidalPsycho in forum New To JavaReplies: 0Last Post: 05-03-2010, 01:21 AM -
prevent schemagen from adding the super-class to the schema?
By shay1680 in forum XMLReplies: 0Last Post: 03-15-2010, 09:14 PM -
Adding a node with a TreeViewer; what's wrong?
By Rodrigo Braz in forum SWT / JFaceReplies: 1Last Post: 04-24-2009, 07:43 AM -
i need help with my code.how can i eliminate duplicate number from my sorted list
By yinghang in forum New To JavaReplies: 6Last Post: 04-13-2009, 04:19 AM -
How to prevent duplicate username entry in database?
By anki1234 in forum JavaServer Pages (JSP) and JSTLReplies: 4Last Post: 01-09-2008, 08:02 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks