当循环正在执行我不明白的代码时
While loop is executing code I don't understand
谁能解释一下这个程序是如何工作的?我不知道"while"循环中发生了什么
struct L {
int d;
L *p;}
L* l = new L;
l->d = 3;
l->p = new L;
l->p->d = 5; //Is this a substructure?
l->p->p = l;
while(l->d < 7) {
cout << l->d++ << ' ';
l = l->p; // ??
}
// L is a data structure also known as a linked list. It has:
struct L
{
int d; // a value
L *p; // a pointer to the next element
}
// it creates the first element of the linked list, and assigns it a value of 3
L* l = new L;
l->d = 3;
// then makes it point to a new element, with value 5
l->p = new L;
l->p->d = 5;
// the new element's pointer to the next element is then set to be
// the first item in the list, hence creating a circular list of 2 elements
// .->| 3 |-->| 5 | -.
// '-----------------'
l->p->p = l;
// then it loops through the list, printing the value of the current element
// and increasing it to 1 every time, and stopping when the value of the current
// element is 7 or more
while(l->d < 7) {
cout << l->d++ << ' '; // prints the value of the current element and increases it
l = l->p; // moves to the next node
}
这里有一篇非常好的文章,可以帮助你理解什么是链表以及如何正确使用它:https://www.learn-c.org/en/Linked_lists
我希望这已经足够清楚了!
相关文章:
- 我不明白这段代码是如何对这个pythonlist()进行排序的,也不明白如何用C++中的向量来重现它
- 我不明白为什么我的代码不起作用并且需要更长的时间来运行
- 我遇到了这个代码片段,不明白. 它递归检查 C++ 字符串中是否存在大写字符
- 下面的代码是如何工作的?它输出分解的数字并且功能齐全,我只是不明白它是如何做到的
- 当循环正在执行我不明白的代码时
- c ++ 2d 数组和指向指针的指针 - 我不明白这段代码
- static_cast 没有尖括号的代码片段 我不明白
- 我不明白代码的某些部分
- 我不明白我在这个编程代码中得到的错误
- 不明白这段代码?这是关于在 c++ 中读取文件
- 不明白这个代码原理
- 我不明白在下面的代码中将 char buffer[] 与 X 类型的对象对齐的原因
- 我不明白这段代码的最后一个输出
- 我不明白为什么我的代码不起作用
- 很简单的代码,并得到错误C2712,不明白为什么
- 我不明白为什么这个代码不能打开文件
- 我不明白为什么编译代码片段的第二个版本。AFAIK 它不应该,因为 §10.3/2
- 我不明白为什么我的c ++代码运行得这么慢
- 当我运行此代码时,开发人员C++不断崩溃.我不明白为什么
- 我不明白这段代码是如何在MS Visual C++编译器中执行的