s=r->next和r->next=s的区别
时间: 2024-05-28 18:13:34 浏览: 12
`s=r->next` 表示将指针 `r` 所指向的链表节点的下一个节点的地址赋值给指针 `s`,此时 `s` 指向的是 `r` 所指向节点的下一个节点。
`r->next=s` 表示将指针 `s` 所指向的节点插入到指针 `r` 所指向节点的后面,即将指针 `s` 所指向节点的地址赋值给指针 `r` 所指向节点的 `next` 指针。
可以看出,两者的作用是不同的,前者是获取链表节点的下一个节点,后者是将一个新的节点插入到链表中。
相关问题
void f (GoodsList &L, GoodsList &LC, int c) { Goods *p=L, q, r, s; LC=(Goods *) malloc(sizeof(Goods)); LC->next=LC; r=LC; while(p->next!=L) { if(p->next->code>=10) p=p->next; else { s=(Goods *) malloc(sizeof(Goods)); s->code=p->next->code; s->price=p->next->price; s->total=p->next->total; s->next=LC; r->next=s; r=r->next; p=p->next; } //else } //while } //end f
这段代码实现了单链表的遍历和插入操作,其中:
- 参数L是一个指向单链表头节点的指针;
- 参数LC是一个指向新链表头节点的指针,函数执行后会将符合条件的节点插入到LC所指向的链表中;
- 参数c是一个整型变量,用于判断符合条件的节点。
具体实现过程如下:
1. 创建一个新的链表LC,并将其next指针指向自身,表示链表为空。
```
LC = (Goods *)malloc(sizeof(Goods));
LC->next = LC;
```
2. 遍历单链表L,当节点的code值小于10时,将其插入到新链表LC的末尾。
```
while (p->next != L) {
if (p->next->code >= 10)
p = p->next;
else {
s = (Goods *)malloc(sizeof(Goods));
s->code = p->next->code;
s->price = p->next->price;
s->total = p->next->total;
s->next = LC;
r->next = s;
r = r->next;
p = p->next;
}
}
```
3. 将新链表LC的头节点指针返回给调用函数。
```
LC = LC->next;
```
需要注意的是,该函数会修改原链表L的节点指针,因此在调用该函数前需要备份原链表的头节点指针,以免出现指针丢失的情况。
s->next=NULL; r->next=s; r=s;
These three lines of code are used to add a new node 's' at the end of a linked list.
Explanation:
- The first line 's->next=NULL;' sets the 'next' pointer of the new node 's' to NULL, indicating that it is the last node in the list.
- The second line 'r->next=s;' sets the 'next' pointer of the current last node 'r' to 's', linking the new node 's' to the list.
- The third line 'r=s;' updates the variable 'r' to point to the new last node 's'. This ensures that future nodes added to the list will be added after 's'.
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)