2013-05-25 129 views
2

我有一个问题,理解指针的行为设置为零在帕斯卡。我使用涡轮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 
. 
+0

如果您的'更新的解决方案'仍然有错误,然后描述它们。否则将其作为自我回答发布。 –

+0

对不起,我刚刚看到这个回复。我会等待进一步的答复,然后做你的建议。 –

回答

1

有几件奇怪的事情。

您将数据加载到堆栈(node1)上分配一个记录,该记录将在过程退出时执行,然后将其内容(不是引用/指针)深入复制到分配给头尾的记录中(使用新的) 。

 head^:=node1; 
     tail^:=node1; 

在这一点上,你有节点1的内容,节点1的三个副本,头2和尾^

随着节点2你犯同样的错误。 (head^.NodePtr ^:= node2)

您可以通过简单地分配点来指定点,例如,

 head:=tail; 

和接入领域的直接太

 head^.field:=something 

如果头点的东西理智。

该构建:

new(node1.NodePtr); 
    node1.NodePtr:=nil; 

本质上是一个内存泄漏。您将记录空间分配给nodeptr,但是立即将NIL分配给它,而不会引用刚刚分配的记录。

提示:首先用纸盒(表示记录)和箭头(表示指针)在纸上制定算法。

+0

谢谢你的深入回复。有很多事情我不知道。我会弄清楚我正在尝试做什么的确切细节。 –

+0

所做的更改:不再需要过程调用 –

+0

您仍然将堆栈上的记录分配给指针。在动态数据结构的情况下是错误的,因为它们在函数退出时无效。 –

1

修订1-移除局部变量节点1和节点

集尾巴“下一个节点”指针为nil

检查头指向尾部为列表中的2个节点

基于解答的更新解决方案

program LinkedListTest; 
type 
    ListNodePtr = ^ListNode; 
    ListNode = record 
     key,cycleLength: integer; 
     NodePtr: ListNodePtr; 
    end; 


var 
head,tail,tempPtr : ListNodePtr; 
count: integer; 
pointerIsNil: boolean; 
{Init the linked list} 


begin 

new(head); 
new(tail); 
new(tempPtr); 
tempPtr^.key:=10; 
head:=tempPtr; 
tail:=tempPtr; 
tail^.NodePtr:=nil; 
writeln('head key is now: ',head^.key); 
writeln('tail key is now: ',tail^.key); 
pointerIsNil:=head^.NodePtr = nil; 
writeln('Is heads node ptr nil? Answer is: ',pointerIsNil); 
new(tempPtr); 
tempPtr^.key:=20; 
head^.Nodeptr:=tempPtr; 
tail:=tempPtr; 
writeln('head key is now: ',head^.key); 
writeln('tail key is now: ',tail^.key); 

pointerIsNil:=head^.NodePtr = nil; 
writeln('Is heads node ptr nil? Answer is: ',pointerIsNil); 
writeln('Making sure head is linked to the tail ',head^.NodePtr^.key); 


readln; 


end 
.