Friday, October 16, 2009

Operation 6 IS – Empty List, Operation 7 IS – Full List :

Processing logic often depends on whether there are data in a list. Thus, we provide empty list, a simple module that returns a Boolean indicating that there are data in the list or that it is empty (Algorithm 6).

Empty linked list

algorithm emptyList (val list )

Returns Boolean indicating whether the list is empty.

Pre list is metadata structure to a valid list

Return true if list empty, false if list contains data

1 return (list.count equal to zero)

end emptyList


Full List :

At first glance, full list appears to be as simple as empty list. It turns out to be a relatively complex algorithm to implement, however. Very few languages provide the programmer with the capability to test how much memory is left in dynamic memory. Given this limitation, the only way we can test for a full list is to try to allocate memory. If we are successful, we simply recycle the memory and return false – the list is not full. If we are unsuccessful, then we return true – dynamic memory is full. The pseudocode is shown in Algorithm 7.

Algorithm – 7 Full linked list

algorithm fullList (val list )

Returns Boolean indicating whether or not the list is full.

Pre list is metadata structure to a valid list

Return false if room for new node; true if memory full

1 allocate (pNew)

2 if (allocation successful)

1 recycle (pNew)

2 return false

3 end if

4 return true

end fullList

No comments:

Post a Comment