연결된 목록 추상화를 구현하려고하지만 문제가 발생합니다. 일단 내가 링크 된 목록을 만들고 요소를 추가하십시오. 목록을 인쇄 할 때 첫 번째 요소가 무한 루프 방식으로 인쇄됩니다. 즉, 첫 번째 요소가 자체에 연결되었거나 인쇄 기능이 올바르지 않습니다. 그러나, 나는 문제를 찾을 수 없다, 누군가 도울 수 있 었는가? 그것은 더 큰 무언가의 일부이기 때문에이 프로그램은,이 추상화를 따라야합니다C의 링크 된 목록은 올바르게 구성되는 목록입니까?
typedef struct _friend {
char *firstname;
char *lastname;
char birthdate[9];
} friend;
typedef struct _node {
friend *value;
struct _node *next;
} node;
typedef struct _linkedlist {
node *head;
} linkedlist;
:
다음은 목록 추상화입니다.
void printList(linkedlist *llist,FILE *fp)
{
node *n;
friend *f;
// for each node, print out the friend attached to it
for(n = llist->head; n != NULL ; n = n->next)
{
// assign f to the friend of the right node
f = n->value;
// print the friend out
fprintf(fp,"%s %s: %s\n",
f->firstname, f->lastname, f->birthdate);
}
}
llist가 NULL인지 여부를 확인하고 있지만 즉시 그 값을 쓰고 있습니다. llist-> head가 null인지 여부를 도대체 알겠습니까? – templatetypedef
'if (llist == NULL)'과'else' 블록에서 * 똑같은 * 연산을 수행한다는 것을 알고 있습니까? – wildplasser