如何指针引用的工作?

编程入门 行业动态 更新时间:2024-10-27 10:19:31
本文介绍了如何指针引用的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

的#define SWAP_PTRS(A,B)做{void *的T =(A); (A)=(B); (B)= T; }而(0)节点* MergeLists(节点* list1的,节点*列表2){  节点列表* = NULL,** pnext =安培;清单;  如果(列表2 == NULL)    返回list1的;  而(list1的!= NULL)  {    如果(list1->数据GT&;&list2- GT;数据)      SWAP_PTRS(list1的,列表2);    * pnext = list1的;    pnext =安培; list1->接下来,    list1的= * pnext;  }  * pnext =列表2;  返回列表;}

这code是从这里开始,这个问题。

我不明白这里3行:

* pnext = list1的;pnext =安培; list1->接下来,list1的= * pnext;

谁能好心帮我吗?解释一下吗?

编辑:我可以更改这些2线:

pnext =安培; list1->接下来,list1的= * pnext;

列表=列表 - >下一个;

解决方案

从一开始:你有两个列表和一个新的表头,你将返回。 pnext指向最初

在code的目的是做尽可能少的指针重新分配成为可能,因此试图保持输入列表的最初的下一个指针不变。

list1的pnext->&列表 - GT;空|V邻>邻>的...列表2|V邻>邻>的...

交换是有保证的较小的元件是第一list1的的。什么这些行的作用是:

去一步一步的:

* pnext = list1的;

获取* pnext(这是在第一次迭代之前清单)为指向包含最小的元素节点

list1的|V邻>邻>的...^|名单< -pnext列表2|V邻>邻>的...

pnext =安培; list1->接下来,

是棘手的部分,因为之前和放大器注意;运营商具有低precedence。它也难以以图形方式显示为它实际上看起来变成节点构建体的一部分。事情是这样的,虽然:

list1的|VØ---->邻>的...^^| |列表中移除x< -pnext列表2|V邻>邻>的...

其中x是LIST1点邻的下一个指针。

list1的= * pnext;

推进list1的头,因为它的第一个元素进行处理。

list1的< -pnext   |   V邻>邻>邻> ...^|名单列表2|V邻>邻>邻> ...

您有什么可从这里做的列表中,您要其返回为合并列表的头。

在不变的有pnext分,其中最后处理元素的下一个点,这是在从list1-2的最小元素应该去。有趣的事情发生与掉期,尽量制定出确切的程序自己(很难得出这样的,和很好的锻炼,了解**做什么)。如果我发现画的好方法我想补充它。

您不能使用列表=列表 - >旁边; 它会做这样的事情:

list1的   |   V邻>邻>邻> ...   ^   |   名单列表2|V邻>邻>邻> ...

这意味着你失去了孤独O(和其他一切最终随着循环的进行)。

编辑: * pnext =列表2; 末做到这一点:

循环终止(之前的状态表示语句):

list1的< -pnext         |         V邻>邻>邻>空^|名单      邻>邻>邻> ...      ^      |      列表2

在下面的语句:

list1的         |         V邻>邻≠0空^ || |清单|      V      邻>邻>邻> ...      ^      |      列表2< -pnext

这语句追加其余列表以列表的末尾。然后返回节点列表*指着合并列表的头。

EDIT2:

和所有的方式,pnext会psented这样更好地重复$ P $:

list1的         |         V邻>邻≠0空^ || |< -pnext清单|      V      邻>邻>邻> ...      ^      |      列表2

这意味着它指向的最后一个处理节点的下一个指针。

#define SWAP_PTRS(a, b) do { void *t = (a); (a) = (b); (b) = t; } while (0) Node* MergeLists(Node* list1, Node* list2) { Node *list = NULL, **pnext = &list; if (list2 == NULL) return list1; while (list1 != NULL) { if (list1->data > list2->data) SWAP_PTRS(list1, list2); *pnext = list1; pnext = &list1->next; list1 = *pnext; } *pnext = list2; return list; }

This code is from here, the chosen answer of this question.

I cannot understand 3 lines here:

*pnext = list1; pnext = &list1->next; list1 = *pnext;

Can anyone kindly help me? Explain it for me?

Edited: Can I change these 2 lines:

pnext = &list1->next; list1 = *pnext;

to

list = list-> next;

解决方案

From the start: You have two lists and a new list head which you will be returning. pnext points to that initially.

The code aims to do as few pointer reassignments as possible, so tries to keep the initial 'next' pointers of the input lists intact.

list1 pnext->list->Null | V o->o->o... list2 | V o->o->o...

Swap is there to ensure that the smaller element is the first of list1. What those lines does is:

Going step by step:

*pnext = list1;

Gets *pnext (which is list before the first iteration) to point to the node containing the smallest element:

list1 | V o->o->o... ^ | list<-pnext list2 | V o->o->o...

.

pnext = &list1->next;

Is the tricky part, as noted before & operator has low precedence. It's also hard to display graphically for it actually looks into part of a Node construct. Something like this though:

list1 | V o---->o->o... ^ ^ | | list x<-pnext list2 | V o->o->o...

where x is the next pointer of the o which list1 points to.

list1 = *pnext;

Advances the list1 head, as its first element is processed.

list1<-pnext | V o->o->o->... ^ | list list2 | V o->o->o->...

You have nothing to do with list from here on, for you want to return it as the head of the merged list.

The invariant there is pnext points to where the last processed element's next points to, which is where the smallest element from list1-2 should go. The interesting stuff happens with swaps, try to work out the exact proceedings yourself (hard to draw like this, and good exercise to understand what ** does). I might add it if I find a good way to draw it.

You cannot use list = list-> next; for it would do something like this:

list1 | V o->o->o->... ^ | list list2 | V o->o->o->...

Which means you lose that lonely o (and everything else eventually as the loop progresses).

edit: The *pnext = list2; at the end does this:

Loop termination (state before the said statement):

list1<-pnext | V o->o->o->null ^ | list o->o->o->... ^ | list2

After the statement:

list1 | V o->o->o Null ^ | | | list | V o->o->o->... ^ | list2<-pnext

That statement appends the remaining list to the end of the list. Then Node* list is returned, pointing to the head of the merged list.

edit2:

And all the way, pnext would be better represented like this:

list1 | V o->o->o Null ^ | | |<-pnext list | V o->o->o->... ^ | list2

Which means it points to the next pointer of the last processed node.

更多推荐

如何指针引用的工作?

本文发布于:2023-07-29 08:36:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239131.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   工作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!