2

リンクリストの抽象化を実装しようとしていますが、問題が発生しています。リンクリストを作成し、それに要素を追加したら。リストを印刷すると、リストの最初の要素のみが無限ループで印刷されます。つまり、最初の要素がそれ自体にリンクされているか、印刷関数が正しくありません。しかし、私は問題を見つけることができません、誰かが助けることができますか?

リストの抽象化は次のとおりです。

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;

プログラムは、より大きなものの一部であるため、この抽象化に従う必要があります。以下は、リストを出力し、リストの先頭にノードを追加する必要がある関数です。

 /* 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);
    }

}

ありがとうございました

4

4 に答える 4

3

forループインはprintList完全に正しくありません:

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

これは読むべきです:

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

それ以外の場合、2回目の反復以降、n毎回同じ値に設定されます。

以下はあなたが抱えている問題とは関係ありませんが、とにかくそれについて言及したいと思いました。次のコードでは:

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

}

の場合llist == NULLllist->head = nセグメンテーション違反になります。

現在の署名がである場合、(エラーメッセージを出力してベイルアウトする以外に)addHead()できることは多くありませんllistNULL

代わりにllist->head、がNULLであるかどうかを確認する場合は、elseブロックがすでにそれを正しく処理しているため、これを行う必要はありません。

于 2012-01-25T18:17:01.983 に答える
0

私はあなたのプログラムに次のことをしました:

  • 構造を少し変更しましたfriend。便宜上、配列として名と姓を宣言しました。
  • main()他の関数を呼び出すaを書いた
  • エラーチェックインaddHead()
  • 構造体create_friend()を作成する関数を追加しましたfriend
  • 'edfreeList()されたメモリを解放するために追加されましたmalloc()
  • 印刷機能のループエラーを修正しました

だからここに行きます。

#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;
}

お役に立てれば!

于 2012-01-25T23:09:36.567 に答える
0

試す:

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);
    }

}
于 2012-01-25T18:16:00.320 に答える
0

n = n-> nextである必要があります。そうでない場合は、毎回頭の次を取得するだけです。

于 2012-01-25T18:18:40.093 に答える