| | |
hey guys!! can you help me with this??
![]() |
•
•
Join Date: Dec 2007
Posts: 5
Reputation:
Solved Threads: 0
hey guys.. can you help me explain this code?
[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!!
[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!!
•
•
Join Date: Jan 2008
Posts: 19
Reputation:
Solved Threads: 0
/*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!
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!
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
![]() |
Similar Threads
- You Guys Play Videogames? (Geeks' Lounge)
- hey guys plz help me!!!!!! (IT Professionals' Lounge)
- Hey Guys, CMS needed ..... (Existing Scripts)
- hard drive Issue....Help :( (Storage)
- Hey guys can anybody help me (Geeks' Lounge)
- hey guys; having some trouble with this program i'm writing (Java)
Other Threads in the Java Forum
- Previous Thread: Writing methods inside of a class using actionlisteners?
- Next Thread: assist on this exception
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game gameprogramming givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working






