3.3.4 Deletion

To delete the record following predecessor (Figure 3.7(a)), only the link field value of its successor must be copied into the predecessor's link field:

setlink(predecessor,next(next(predecessor)));

Thus next (next(predecessor)) is the value that must be copied into the predecessor's link field. It is clearer to write the code as

successor = next(predecessor);

setlink(predecessor,next(successor));

even if it is slower, requiring an additional memory access. Again, either code is independent of any implementation for the list. After checking for special cases (Figure 3.7(b)), the correct function is found to be

>Comment

delete(phead,predecessor)

/* Deletes, in list head, the successor of

the record pointed to by predecessor.

*/

listpointer *phead,predecessor;

{

listpointer successor,null,setnull(),next();

null = setnull();

>Comment

if(*phead != null)

if(predecessor == null)

>Comment

*phead = next(*phead);

else

{

>Comment

successor = next(predecessor);

if(successor != null)

setlink(predecessor,next(successor));

}

{

Sometimes, instead of having a pointer to the predecessor of a record to be deleted, only a pointer to the record itself is available. Deletion can still be accomplished; all that is required is to copy the entire successor record (the successor to the record to be deleted, that is) into the record to be deleted. It is necessary to assume, however, that the last list record is never deleted. Why? Whether or not this is better than keeping track of the predecessor depends on how much time is required to effect this copying operation. The larger the record, the greater the time. However, if the information field of a record is kept elsewhere, and a pointer to it stored in the record itself, then the operation reduces to copying just this pointer and the value of the link field.

Are you listing from all this list information?