Monday, February 22, 2010

Chapter 2: Basic Abstract Data Types. Problem 2.9

The following procedure was intended to remove all occurances of element x from list L. Explain why it doesn't always work and suggest a way to repair the procedure so it performs its intended task. 

procedure delete (x : elementtype; var L: LIST);
var
p: position
begin
p := FIRST(L);
while p <> END(L) do begin
if RETRIEVE(p,L) = x then
DELETE(p,L);
p := NEXT(p,L);
end
end; {delete}


In the above problem, you can't just delete the NODE without adjusting the previous pointer to point to the node after the one to be deleted.

No comments:

Post a Comment