add method of LinkedSortList

Hi,
   I'm trying to write an add method for a LinkedSortList class. Can someone explain the algorithm (don't write the code) step-by-step for the add method? The spec is to add an element without changing the order of the list (keeps it in order). The structure to use is linear nodes. I would really appreciate it. Thanks!
azcalv408Asked:
Who is Participating?
 
borislavmarkovCommented:
You have the list as follows:

A->B->D->Z
You have to add new element "W".
Begin to iterate elements and find position where new element is in relation like:

PREVIOUS < ADDED < NEXT

then you set pointers like:

PREVIOUS .next = ADDED
ADDED.previous = PREVIOUS
ADDED.next = NEXT
NEXT.previous = ADDED

so you begin to iterate elements of you current array and you find out that:

A,B - no action
B,D - no action
D,Z - here you take the action:

D.next = W
W.previous = D
W.next = Z
Z.previous = W

then you have structure:


A->B->D->W->Z

end.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.