Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-20-2009, 06:38 AM
Member
 
Join Date: Mar 2009
Posts: 1
Rep Power: 0
ineedhelpwithjava is on a distinguished road
Unhappy doubly linked list insert
public class DoubleList
{
private DoubleNode head;
private DoubleNode tail;

public DoubleList()
{
head = null;
tail = null;
}

public boolean isEmpty()
{
return head == null;
}

public DoubleNode getHead()
{
return head;
}

public void insertHead(DoubleNode node)
{
if (isEmpty())
{
node.setPrev(null);
node.setNext(null);
head = node;
tail = node;
}
else
{
head.setPrev(node);
node.setPrev(null);
node.setNext(head);
head = node;
}
}

public void insertTail(DoubleNode node)
{
if (isEmpty())
{
node.setNext(null);
node.setPrev(null);
head = node;
tail = node;
}
else
{
tail.setNext(node);
node.setNext(null);
node.setPrev(tail);
tail = node;
}
}

public void deleteHead()
{
if (head.getNext() == null)
{
head = null;
tail = null;
}
else
{
head.getNext().setPrev(null);
head = head.getNext();
}
}

public void deleteTail()
{
if (head.getNext() == null)
{
head = null;
tail = null;
}
else
{
tail.getPrev().setNext(null);
tail = tail.getPrev();
}
}

public boolean insert(Event event)
{
DoubleNode newNode = new DoubleNode(event);
String timestamp = event.getTimestamp();
DoubleNode current = head;
boolean result = false;

while (current != null)
{
if (timestamp.compareTo(current.getEvent().getTimesta mp()) < 0)
{
if (current == head)
{
insertHead(newNode);
}
else if (current == tail)
{
insertTail(newNode);
}
else
{
// insert the new node before the current node
newNode.setNext(current);
newNode.setPrev(current.getPrev());
current.getPrev().setNext(newNode);
current.setPrev(newNode);
}
result = true;
}
else
{
current = current.getNext();
}
}

if (current == null)
{
insertTail(newNode);
result = true;
}

return result;
}

public boolean delete(Event event)
{
DoubleNode current = head;
boolean result = false;
String timestamp = event.getTimestamp();

while (current != null)
{
if (timestamp.compareTo(current.getEvent().getTimesta mp()) == 0)
{
// remove the node
result = true;
}
else
{
current = current.getNext();
}
}
return result;
}
}

please i need help with the insert method, i can't get it working
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 03-20-2009, 03:05 PM
Steve11235's Avatar
Senior Member
 
Join Date: Dec 2008
Posts: 972
Rep Power: 2
Steve11235 is on a distinguished road
Default
Taking a brief look, I don't see how you exit the while loop once you insert the node. Given that all your if's are flattened (use [ code ]...[ /code ]), I really can't see the logic there, either.

While a lot of people hate this, I suggest using while(true) and break. It eliminates a lot of nesting, and you know that you are leaving the loop.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked list rosh72851 New To Java 1 02-05-2009 08:21 AM
Linked List integer list igniteflow Advanced Java 1 12-10-2008 09:53 PM
Help with Doubly linked list Dr Gonzo New To Java 5 12-06-2008 08:45 AM
Doubly-linked list with data structure Java Tip java.lang 0 04-16-2008 11:30 PM
Linked List help neobie New To Java 8 12-22-2007 04:15 AM


All times are GMT +2. The time now is 05:09 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org