C中的链表是正确构建的列表吗?

时间:2012-01-25 18:12:09

标签: c linked-list singly-linked-list

我正在尝试实现链接列表抽象,但是我遇到了问题。一旦我创建链接列表并添加元素。当我打印列表时,它只以无限循环方式打印其中的第一个元素,这意味着第一个元素链接到自身或打印函数不正确。但是,我找不到问题,有人可以帮忙吗?

以下是列表抽象:

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 个答案:

答案 0 :(得分:3)

for中的printList循环不太正确:

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

这应该是:

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块已经正确处理了这个。

答案 1 :(得分: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);
    }

}

答案 2 :(得分:0)

应该是n = n - >接下来,否则你每次都会得到下一个头部。

答案 3 :(得分:0)

我已对您的计划进行了以下操作:

  • 略微修改了friend结构。为方便起见,将firstname和lastname声明为数组。
  • 写了一个调用其他函数的main()
  • addHead()
  • 中的错误检查
  • 添加了创建create_friend() struct
  • friend函数
  • 添加freeList()以释放malloc()'ed
  • 的内存
  • 更正了打印功能中的循环错误

所以这就是..

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

希望这有帮助!