Board index » delphi » Turbo Pascal - need help in a link list problem

Turbo Pascal - need help in a link list problem

 In a problem, I need to merge two link list together as a new one, by
referring the last link of the first list to the first item of the second
list. I am not allowed to use dummy record.
I will appreciate any comment.

 

Re:Turbo Pascal - need help in a link list problem


In article <01bb9a43.bb488900$7c024999@user>

Quote
Mohsen Younesi wrote:
> In a problem, I need to merge two link list together as a new one, by
>referring the last link of the first list to the first item of thesecond
>list. I am not allowed to use dummy record.
>I will appreciate any comm

You've just about stated the whole process.  It's a shame that you
couldn't fill in the missing pieces. Maybe next time...

Establish a pointer (variable) of the same type as a list node.

Assign the head of the first list to this pointer.

If this pointer is NIL, then there is no first list so you need only
assign the head of the second list to the head of the first list.  

Otherwise, while the "next" node field is not NIL, assign it to the
pointer.

Having reached the last node of the first list ("next" node field is
nil) assign the head of the second list to the "next" node field.

When done, you should probably assign a NIL value to the head of the
second list as it no longer exists as a separate list.

The above outlines a solution to  your problem.  You should be able
to "code" it without too much trouble.  You're goal, however, should
be to produce a reasonable design (outline) from which anyone could
"code" the solution.

    ...red

Re:Turbo Pascal - need help in a link list problem


Quote
Mohsen Younesi (Mohsen.Youn...@Dundee.NCR.COM) wrote:
>  In a problem, I need to merge two link list together as a new one, by
> referring the last link of the first list to the first item of the second
> list. I am not allowed to use dummy record.
> I will appreciate any comment.

hmm.. this should come to you quite.. logically.

have another pointer, point to the previous record.

like
head->next  ( pointer to the next record )
head<-next  ( pointer to the previous record )

by doing this. you can make the beginning of the second list point to the
end of the first list and the end of the second list point to the first

like

first->second
first<-second

----------------------------------------------------------------------
Support Chips.. nothing runs without us

Email address: sof...@chat.carleton.ca
----------------------------------------------------------------------

Other Threads