How to switch two nodes in a singly linked list in pascal?
procedure List.switch(var n1,n2 : pNode);
var aux : pNode;
begin
aux := n1;
p1:= n2;
n2 := aux;
end;
(Here pNode is a pointer on a node in the list.)
Nodes are defined like this:
pNode = ^Node;
Node = record
data : data;
next : pNode;
end;
This code doesn't work. It either doesn't compile, saying "Can't take the address of constant expressions", or just doesn't do anything. I guess it has to do with how pointers work...
I found relevant information here, but I don't read C.
Thanks for any advice!
Something like this should work for you. It uses a local variable of type Node (which is presumably what PNode is a pointer to) as a placeholder.
Here's a version of it that isn't an object method, with a console app that tests it:
I think something like this would work: