2012-01-25 1 views
2

연결된 목록 추상화를 구현하려고하지만 문제가 발생합니다. 일단 내가 링크 된 목록을 만들고 요소를 추가하십시오. 목록을 인쇄 할 때 첫 번째 요소가 무한 루프 방식으로 인쇄됩니다. 즉, 첫 번째 요소가 자체에 연결되었거나 인쇄 기능이 올바르지 않습니다. 그러나, 나는 문제를 찾을 수 없다, 누군가 도울 수 있 었는가? 그것은 더 큰 무언가의 일부이기 때문에이 프로그램은,이 추상화를 따라야합니다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); 
    } 

} 
+0

llist가 NULL인지 여부를 확인하고 있지만 즉시 그 값을 쓰고 있습니다. llist-> head가 null인지 여부를 도대체 알겠습니까? – templatetypedef

+0

'if (llist == NULL)'과'else' 블록에서 * 똑같은 * 연산을 수행한다는 것을 알고 있습니까? – wildplasser

답변

3

for 루프 printList ISN에 :

/* addHead 
    * 
    * This function takes two parameters - a linked list and a friend. 
    * This creates a node for the linked list and connects the friend to the 
    * node. Then it adds the node to the head of the linked list. 
    */ 

void addHead(linkedlist *llist, friend *f) 
{ 

    // create a node and put the friend in it 
    node *n = (node *)malloc(sizeof(node)); 
    n->value = f; 
    n->next = NULL; 

    // if the list is empty 
    if (llist == NULL) 
    { 
     // this link is the entire list 
     llist->head = n; 
     printf("adding friend to null list\n"); 

    } 
    // if the list is not empty 
    else 
    { 
     // make the new link's next pointer point to 
     // the first link in the list 
     n->next = llist->head; 
     printf("adding %s to head\n", n->value->firstname); 

     // make the head pointer point to the new link 
     llist->head = n; 


} 

} 

/* 
* printList 
* 
* This steps down through each of the nodes in a linked list and 
* prints out the information stored in the friend to which the node points. 
* Instead of automatically printing to the screen, it prints to the 
* file pointer passed in. If the programmer wants to print to the screen, 
* he/she will pass in stdout. 
*/ 

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 = llist->head->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); 
    } 

} 

당신에게

0

시도 감사 : 다음은 목록을 인쇄하고 목록의 시작 부분에 노드를 추가해야 할 기능은 다음과 같습니다 꽤 맞았습니다 :

for(n = llist->head; n != NULL ; n = llist->head->next) 

T 그의 읽어야합니다

for(n = llist->head; n != NULL ; n = n->next) 

을 그렇지 않으면 이후, n가 같은 값 매 시간으로 설정됩니다 두 번째 반복에서.

다음은 현재 가지고있는 문제와 관련이 없지만 어쨌든 언급하겠습니다. 다음 코드에서 :

if (llist == NULL) 
{ 
    // this link is the entire list 
    llist->head = n; 
    printf("adding friend to null list\n"); 

} 

llist == NULL 경우, llist->head = n는 세그 폴트 것입니다.

addHead()의 현재 서명을 사용하면 llistNULL 인 경우 많은 작업을 수행 할 수 있습니다 (오류 메시지 인쇄 및 구제 조치 제외).

대신 llist->head이 NULL인지 여부를 확인하려는 경우 else 블록이 이미 올바르게 처리 했으므로이 작업을 수행 할 필요가 없습니다.

0

다음은 n = n -> next 여야합니다. 그렇지 않으면 매회 머리가 다음 번에 오게됩니다.

0

가 나는 프로그램에 다음과 같은 짓을 :

  • 약간 friend 구조를 수정했습니다. 편의상 이름과 성을 배열로 선언했습니다.
  • addHead()
  • 에서 확인
  • 오류 다른 함수를 호출하는 main()이 구조체
  • friend
  • 이 인쇄에 오류가 반복 수정 ' malloc()이었다 에드 메모리를 해제하는 freeList()를 추가 생성 create_friend() 기능을 추가 쓴 함수

그래서 여기에 ..

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

typedef struct _friend { 
    char firstname[10]; 
    char lastname[10]; 
    char birthdate[9]; 
} friend; 


typedef struct _node { 
    friend *value; 
    struct _node *next; 
} node; 

typedef struct _linkedlist { 
    node *head; 
} linkedlist; 


void addHead(linkedlist *llist, friend *f) 
{ 
    node *n = NULL; 

    if ((n = (node *)malloc(sizeof(node))) == NULL) { 
     printf("unable to allocate memory \n"); 
     exit(1); 
    } 

    n->value = f; 
    n->next = NULL; 

    if (llist == NULL) { 
     llist->head = n; 
     printf("adding friend to null list\n"); 
    } else { 
     n->next = llist->head; 
     printf("adding %s to head\n", n->value->firstname); 
     llist->head = n; 
    } 

    return; 
} 

void printList(linkedlist *llist) 
{ 
    node *n; 
    friend *f; 

    if (llist->head == NULL) { 
     printf("Empty list \n"); 
     return; 
    } 

    for(n = llist->head; n != NULL ; n = n->next) { 
     f = n->value; 
     printf("%s %s %d \n", f->firstname, f->lastname, f->birthdate); 
    } 

    return; 
} 

friend * create_friend(char *fn, char *ln, char *dob) 
{ 
    friend *fp = NULL; 

    if ((fp = malloc(sizeof(friend))) == NULL) { 
     printf("unable to allocate memory \n"); 
     exit(1); 
    } 

    strcpy(fp->firstname, fn); 
    strcpy(fp->lastname, ln); 
    strcpy(fp->birthdate, dob); 

    return fp; 
} 

void freeList(linkedlist *llist) 
{ 
    node *cur = llist->head; 
    node *prev = cur; 
    friend *f; 

    while (cur != NULL) { 
     prev = cur; 
     cur = cur->next; 
     f = prev->value; 
     printf("freeing .. %s %s %d \n", f->firstname, f->lastname, f->birthdate); 
     free(prev->value); 
     free(prev); 
    }  

    return; 
} 

int main(void) 
{ 
    linkedlist ll; 
    friend *f; 

    ll.head = NULL; 

    f = create_friend("firstname1", "lastname1", "12345678"); 
    addHead(&ll, f); 

    f = create_friend("firstname2", "lastname2", "12345678"); 
    addHead(&ll, f); 

    f = create_friend("firstname3", "lastname3", "12345678"); 
    addHead(&ll, f); 

    printList(&ll); 

    freeList(&ll); 
    ll.head = NULL; 

    printList(&ll); 

    return 0; 
} 

희망이 있습니다.

관련 문제