Friday, October 16, 2009

Operation 12 – Destroy List:

When a list is no longer needed but the application is not done, the list should be destroyed. Destroy list deletes any nodes still in the list and recycles their memory. It then sets the metadata to a null list condition. The code for destroy list is shown in Algorithm 10.

Algorithm – 10 Destroy linked list

algorithm destroyList (ref list )

Deletes all data in list

Pre list is metadata structure to a valid list

Post All data deleted

1 loop (list.count not zero)

1 dltPtr = list.head

2 list.head = dltPtr->link

3 list.count= list.count – 1

4 recycle (dltPtr)

2 end loop

No data left in list. Reset metadata.

3 list.head = null

4 return

end destroyList

No comments:

Post a Comment