hey guys.. can you help me explain this code?

public Boolean insertAfter (double key, double dd)
{
Link current = first1
while (current.dData != key)
{
current = current.next;
if (current == null)
Return false;
}
Link newLink = new Link(dd);
if (current == last)
{
newLink.next = null;
last = newLink;
}
else
{
newLink.next = current.next;
current.next.previous = newLink;
}
newLink.previous = current;
current.next = newLink;
return true;
}

thanks!!

Edited 3 Years Ago by happygeek: fixed formatting

/*This is a method that inserts a new node in a Doubly linked-list. It inserts a new node after a node whose data value is passed as key into the method. the second argument, dd takes the data value of the node to be inserted. It returns true if the insertion is successful. It returns false if there's no node in the list whose data value is equal to key value.*/
public Boolean insertAfter (double key, double dd)
{
Link current = first1
/*first1 is the global reference that refers the first node of the linked list. We create a reference (of class Link) that helps us traverse the linked list upto the node in which our key value is stored.*/
while (current.dData != key)
{
current = current.next;
if (current == null)
Return false;
}

/*The above while loop searches for a node whose data field holds the same value as key. If the data value of current node is not equal to key then current will move to the next node (current = current.next;). If there is no next node (if current==null) then it means that we have reached the end of the linked list and no node having data value as the key value provided was found. So we return false.*/

Link newLink = new Link(dd);
/* If node with data value equal to key value is found then we have to insert a new node after this node. so creating a new node with data dd. */
if (current == last)
{
newLink.next = null;
last = newLink;
/* If current node is the last node then the next of the new node should be null. and last should now refer to the newly inserted node. */
}
else
{
newLink.next = current.next;
current.next.previous = newLink;
/* If current node is not the last node of the list then current's next node becomes new node's next (newLink.next = current.next;) and next node's previous refers to the newly created node (current.next.previous = newLink;). */
}
newLink.previous = current;
/* now, new node's previous should be current (because we insert the new node after the node refered by current. */
current.next = newLink;
/* Finally, current node's next should refer to the newly created node. */
return true;
/* Return true to indicate successful insertion. */
}

I would suggest you try to understand linked-lists diagrammatically. Draw block diagrams in which links are drawn as arrows. Then, try to understand how the arrows will change after executing each line of code. It will be much easier to understand it that way. Ok then, Good bye. Take care!

i have no idea at all...my prof. just give us the program. she didn't explain what we'll do it..

This article has been dead for over six months. Start a new discussion instead.