gr3*_*bo1 4 c++ linked-list while-loop
通过定义存储单元
struct node {
int item;
node *next;
};
Run Code Online (Sandbox Code Playgroud)
假设ptr指向一个链表,那么put while(ptr!=NULL) 和 while(ptr->next!=NULL)循环通过列表之间是否有区别,直到到达空指针?
Nat*_*pel 14
while(ptr->next!=NULL) 不会遍历你的最后一个节点.
到达最后一个节点时,ptr->next它将为null,并且它将退出while循环
while(ptr != NULL)将while(ptr->next != NULL)跳过最后一个元素时将迭代所有链表.
当您想要访问最后一个节点以在列表末尾添加新元素时,第二个解决方案很有用.
| 归档时间: |
|
| 查看次数: |
13158 次 |
| 最近记录: |