Pointers on C——12 Using Structures and Pointers.3

Debugging the Insert Function

调试插入函数


Unfortunately, the insert function is incorrect. Try inserting the value 20 into the list and you will see one problem: the while loop runs off the end of the list and then applies indirection to a NULL pointer. To solve this problem, we must test current to make sure that it is not NULL before evaluating current->value:

不幸的是,这个插入函数是不正确的。试试把2 0这个值插入到链表中,你就会发现一个问题:while 循环越过链表的尾部,并对一个NULL 指针执行间接访问操作为了解决这个问题,我们必须对current的值进行测试,在执行表达式current->value 之前确保它不是一个NULL 指针:


while( current != NULL && current->value < value ){

The next problem is tougher. Trace the function to insert the value 3 into the list. What happens?

个问题更加棘手,试试把3 这个值插入到链表中,看看会发生什么?


In order to add a node to the beginning of the list, the function must change the root pointer. The function, however, cannot access the variable root. The easiest way to fix this problem is to just make root a global variable so that the insertion function can modify it. Unfortunately, this approach is also the worst  way to fix the problem,because then the function works only for that one list.

为了在链表的起始位置插入个节点,函数必须修改根指针。但是,函数不能访问变root 修正这个问题最容易的方法是把root 声明为全局变量,这样插入函数就能修改它不幸的是,这是最坏的种问题解决方法。因为这样一来,函数只对这个链表起作


The better solution is to pass a pointer to root as an argument. Then the function can use indirection both to obtain the value of root (the pointer to the first node of the list), and to store a new pointer into it. What is the type of this parameter?root is a pointer to a Node, so the parameter is of type Node **: a pointer to a pointer to a Node. The function in Program 12.2 contains these modifications. We must now call the function like this:

稍好的解决方法是把个指向root 的指针作为参数传递给函数。然后,使用间接访问,函数不仅可以获得root (指向链表第1 个节点的指针,也就是根指针〉的值,也可以向它存储个新的指针值这个参数的类型是什么呢? root 是个指向Node 的指针,所以参数的类型应该是Node **,也就是一个指向Node 的指针的指针程序12.2 的函数包含了这些修改。现在,我们必须以下面这种方式调用这个函数:

result = sll_insert( &root, 12 );

This second version contains some additional statements.

这第2 个版本包含了另外些语句

previous = NULL;


is needed so that we can check later whether the new value will be the first node in the list.

我们需要这条语旬,这样我们在以就可以检查新值是否应为链表的第1 个节点。


current = *rootp;


uses indirection on the root pointer argument to get the value of root, a pointer to the first node in die list. Finally

这条句对根指针数执行间接访问操作,得到的结果是root 的值,也就是指向链表第1 个节点的指针


if( previous == NULL )

*rootp = new;

else

previous->link = new;


was added to the end of the function. It checks whether the new value should be added to the beginning of the list. If so, we use indirection on the root pointer to make root point to the new node.

这条语句被添加到函数的最后它用于检查新值是否应该被添加到链表的起始位置如果是,我们使用间接访问修改根指针,使它指向新节点。


This function works, and in many languages it is as good as you can get.However, we can do better because C allows you to get the address of (a pointer to) existing objects.

这个函数可以正确完成任务,而且在许多语中,这是你能够获得的最佳方案。但是,我们还可以做得更好一些,因为C 允许我们获得现存对象的地址〈即指向该对象的指针〉。

上一章 Pointers on C——12 Using Structures and Pointers.2


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值