这while
没有停止,似乎停留在无限循环中:
struct list_elem<int[5]> *p, *pCopy; // list_elem is the node struct
p = l.pfirst; // l is the LL object
pCopy = p->next;
while(p != NULL) {
//bunch of code
p = pCopy;
}
将关闭问题
简单地pCopy
是一个常数,因为它在循环之外。
谢谢你的帮助
答案 0 :(得分:2)
这就是你如何迭代:
while(p != NULL) {
p = p->next;
}
.
运算符用于本地结构,而->
取消引用指针:
#include <stdio.h>
#include <string.h>
struct my_struct {
int id;
struct my_struct *next;
}
int main()
{
struct my_struct *a, *b, *c;
a = malloc(sizeof(struct my_struct));
b = malloc(sizeof(struct my_struct));
c = malloc(sizeof(struct my_struct));
a->next = b;
a->id = 1;
b->next = c;
b->id = 2;
c->next = NULL;
c->id = 3;
struct my_struct *it = a;
printf("Using pointers:\n\n");
while(it != NULL) {
printf("Element %d\n", it->id);
it = it->next;
}
struct my_struct d, e, f;
d.next = &e;
d.id = 1;
e.next = &f;
e.id = 2;
f.next = NULL;
f.id = 3;
it = &d;
printf("Using locals:\n\n");
while(it != NULL) {
printf("Element %d\n", it->id);
it = (it->next);
}
free(a);
free(b);
free(c);
return 0;
}
你应该谷歌上一篇关于C的教程,并且首先尝试理解指针算术。
答案 1 :(得分:0)
while ((p = p->next) != NULL);
你所拥有的并不是真正通过列表,只是一遍又一遍地设置自己