本帖最后由 阿松松 于 2015-1-9 15:29 编辑
在list.c中,把一个结点插入到链表尾部
void vListInsertEnd( xList *pxList, xListItem *pxNewListItem )
- void vListInsertEnd( xList *pxList, xListItem *pxNewListItem )
- {
- volatile xListItem * pxIndex;
- /* 链表中,总是访问由PxIndex指向的链表结点。插入新结点到链表中时,
- 使得在调用pvListGetOwnerOfNextEntry函数时,新插入的结点是最后一个被移除的,
- 而不是对链表排序(说实话官方的这句
- Insert a new list item into pxList, but rather than sort the list,
- makes the new list item the last item to be removed by a call to
- pvListGetOwnerOfNextEntry. 我一直搞不懂)。
- 这意味着pxIndex必须指向这个新插入的结点。 */
- pxIndex = pxList->pxIndex;
- /*以下两句为新插入结点指定前后结点,可以比喻为孩子找到了爹妈。*/
- /*使得新插入的结点的下一个结点指向当前链表pxList中的pxIndex的下一个结点。*/
- pxNewListItem->pxNext = pxIndex->pxNext;
- /*使得新插入的结点的上一个结点指向当前链表pxList中的pxIndex。*/
- pxNewListItem->pxPrevious = pxList->pxIndex;
- /*以下两句使得当前链表认得新插入的结点,即爹妈要认识这是自己的孩子。
- 即当前链表的pxNext以及pxPrevious都指向新插入结点(因为当前链表只有这
- 一个结点,除了xListEnd)。*/
- /*使得当前链表pxList中的pxIndex的上一个的下一个(醉了,不知道为什么要
- 这样指来指去,最后还是指到了pxIndex,不还是自己嘛,汗。)指向新插入的结点。*/
- pxIndex->pxNext->pxPrevious = ( volatile xListItem * ) pxNewListItem;
- /*使得当前链表pxList中的pxIndex的下一个(pxNext)指向新插入的结点。*/
- pxIndex->pxNext = ( volatile xListItem * ) pxNewListItem;
- /*使得当前链表pxList中的pxIndex指向新插入的结点。*/
- pxList->pxIndex = ( volatile xListItem * ) pxNewListItem;
- /* 让pxList知道这是他的结点。 */
- pxNewListItem->pvContainer = ( void * ) pxList;
- /*链表结点的数量自加1*/
- ( pxList->uxNumberOfItems )++;
- }
复制代码 指针指来指去的,想想也头大,等下画个图。
|
代码中28行的分析错误二得很,今天看这些代码有新的理解,这样才是正确的。
这句代码讲的是:
使得当前链表pxList中的pxIndex指针中的pxNext指针中的pxIndex指针指向新插入的结点pxNexListIetm,这样解释有点儿罗嗦,简而言之,就是将pxList中pxIndex指向的下一个结点的指向规则不再指向以前的旧结点,而是指向新插入的结点pxNexListIetm
下面讲的也是扯淡了,勿喷。。。
引用一个大神讲的,看完这个思路才清晰
也没想到,插图倒是正确的了,尴尬