我有一个问题,理解指针的行为设置为零在帕斯卡。我使用涡轮pascal 7.0。 看来,当我设置两个指针头,尾部为零...他们似乎总是指向未来相同的值,即使他们被分配到不同值。零指针帕斯卡尔
在下面的代码中,当我注释掉问题区域并获得预期结果时。
当我从这两行删除注释 head:= nil; tail:= nil;
当取消引用时,'head'指针似乎总是将'tail'指针的值赋予给它。任何见解提供将不胜感激。
program LinkedListTest;
type
ListNodePtr = ^ListNode;
ListNode = record
key,cycleLength: integer;
NodePtr: ListNodePtr;
end;
{
We have just defined the node of a linked list.
Next we declare our head which is the pointer to the first node
and the tail which points to the last node.
The head helps us find our first node in the list
the tail helps us to keep track of the last node in the list.
Both are simple pointers to a node (in our case ListNodePtr).
}
var
head,tail : ListNodePtr;
node1,node2,node3,node4: ListNode;
count: integer;
{Init the linked list}
procedure InitLinkedList;
Begin
new(head);
new(tail);
(* **Remove comments from this code to see problems in final output**
head:=nil;
tail:=nil;
*)
node1.key:=10;
new(node1.NodePtr);
node1.NodePtr:=nil;
head^:=node1;
tail^:=node1;
writeln('head key is now: ',head^.key);
node2.key:=20;
new(node2.NodePtr);
node2.NodePtr:=nil;
head^.NodePtr^:=node2;
tail^:=node2;
writeln('head key is now: ',head^.key);
writeln('tail key is now: ',tail^.key);
writeln('node1 key is now: ',node1.key);
writeln('node2 key is now: ',node2.key);
readln;
end;
begin
InitLinkedList;
end
.
如果您的'更新的解决方案'仍然有错误,然后描述它们。否则将其作为自我回答发布。 –
对不起,我刚刚看到这个回复。我会等待进一步的答复,然后做你的建议。 –