ja, so würde ich auch löschen.
Nur noch überprüfen, ob es denn überhaupt einen zu löschenden Node gibt
Delphi-Quellcode:
program SingleLinkedList;
{$APPTYPE CONSOLE}
type
PNode = ^TNode;
TNode = record
data: Integer;
next: PNode;
end;
var
FirstNode: PNode; // Pseudoknoten
LastNode: PNode; // Pseudoknoten
CurrentNode: PNode;
procedure Init;
begin
// new(FirstNode); --> überflüssig, Memoryleak
FirstNode := nil;
//new(LastNode); --> überflüssig, Memoryleak
LastNode := nil;
end;
procedure Add(data: Integer);
begin
new(CurrentNode);
CurrentNode.data := data;
// new(CurrentNode.next); --> überflüssig, Memoryleak
CurrentNode.next := nil;
if LastNode = nil then
begin
FirstNode := CurrentNode;
LastNode := CurrentNode;
end
else
begin
LastNode.next := CurrentNode;
LastNode := CurrentNode;
end;
end;
procedure InsertNodeAfter(Node: PNode; data: Integer);
var
NewNode: PNode;
begin
new(NewNode);
NewNode.data := data;
NewNode.next := Node.next;
Node.next := NewNode;
end;
procedure DeleteNextNode(Node: PNode);
var
TempNode: PNode;
begin
if (Node.next <> nil) then
begin
TempNode := Node.next;
Node.next := TempNode.next;
Dispose (TempNode);
end;
end;
procedure ClearList;
var
TempNode: PNode;
begin
CurrentNode := FirstNode;
while (CurrentNode <> nil) do
begin
TempNode := CurrentNode.Next;
Dispose (CurrentNode);
CurrentNode := TempNode;
end;
Init;
end;
procedure WalkTheList;
begin
Writeln;
CurrentNode := FirstNode;
while CurrentNode <> nil do
begin
Writeln(CurrentNode.data);
CurrentNode := CurrentNode.next;
end;
end;
var
i: Integer;
TempNode: PNode = nil;
begin
Init;
for i := 0 to 5 do
begin
Add(i);
if i mod 3 = 0 then
TempNode := CurrentNode;
end;
WalkTheList;
InsertNodeAfter(TempNode, 9);
WalkTheList;
Init;
for i := 0 to 5 do
begin
Add(i);
if i mod 3 = 0 then
TempNode := CurrentNode;
end;
WalkTheList;
DeleteNextNode(TempNode);
WalkTheList;
Readln;
end.