在链表中存储节点数组

时间:2014-03-09 00:20:44

标签: c arrays linked-list nodes

我在链表中​​存储节点数组时遇到问题,每个节点都包含一个固定大小的值数组。我的程序可以编译没有错误,但没有输出,我不知道为什么。这是我的Node和list的结构函数:

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

#define SIZE 20

typedef struct NODE Node;
struct NODE
{
char *bucket[SIZE]; 
int count;       
Node *next;
};

  Node *new_node()
{
   Node *curr=malloc(sizeof( Node ));
   curr->count=0;
   curr->next=NULL;
   return curr;
}

void add_node(Node *node,char *x)
{

    node->bucket[node->count] = (char *)malloc( strlen( x ) + 1 );
    strcpy(node->bucket[node->count],x);
    node->count++;
}
typedef struct LIST List;

struct LIST 
{
    Node *top;
};

List *construct() 
{
List *list;

list = malloc( sizeof( List ) );
list->top = NULL;

return list;
}
void insert( List *list, char *new_string )
{
 Node *newNode = new_node();
 Node *curr;

 curr = list->top;
if ( NULL == curr)
{
  curr=newNode;
}
add_node(newNode,new_string);

 }


void print( List *list )
{
  Node *curr = list->top;

  while ( NULL != curr ) 
  {
   for(int i=0;i<curr->count;i++)
   {
    printf( "%s\n", curr->bucket[i]);
   }
    curr = curr->next;
  }
} 

这是我的测试功能:

 int main(int argc, char const *argv[])
 {

   List *list=construct();
   char *ch="a";

   insert(list,ch);
   insert(list,ch);
   insert(list,ch);
   insert(list,ch);
   insert(list,ch);
   print(list);


   return 0;
  }

为什么这不符合预期的任何想法?

1 个答案:

答案 0 :(得分:0)

主要问题是您的insert()功能。

void insert(List *list, char *new_string)
{
        Node *newNode = new_node();
        Node *curr;

        curr = list->top;
        if (NULL == curr) {
                /* OK. We set curr to newNode, but what happens to curr? */
                curr = newNode; 
        }
        /* String is added to newNode, but what happens to newNode? */
        add_node(newNode, new_string);
}

如果您改为说(您可能打算这样做):

if (list->top == NULL) {
    list->top = newNode;
}

你会先 newNode,但其余部分会松散。


说到add_node(),它没有任何直接的错误。最大的问题是,您不会检查node->count是否为20.其次是 非常糟糕的名称 。在命名函数时要小心,否则代码将变得非常难以阅读和维护。

add_node 添加节点。这个名字应该反映它的作用。

例如你可以说:
insert应命名为add_nodeadd_node的名称应为add_stringpush_stringbucket_fill等。至少那会更好一点。

施放 malloc

也是多余的
node->bucket[node->count] = (char*)malloc(strlen(str) + 1);

更好(恕我直言):

node->bucket[node->count] = malloc(strlen(str) + 1);

有多种方法可以添加新节点。一种方法是循环到最后并添加:

    while (node->next != NULL) {
            node = node->next;
    }
    node->next = new_node();
    bucket_fill(node->next, str);

如果你有一个单独的结构列表,你可以跟踪头尾:

struct list {
    struct node *head;
    struct node *tail;
};

然后像:

void node_add(struct list *list, char *str)
{
        struct node *new_node = node_create();

        bucket_fill(new_node, str);

        if (list->head != NULL) {
                list->tail->next = new_node;
                list->tail = new_node;
        } else {
                list->head = new_node;
                list->tail = new_node;
        }
}

依此类推。


查看每个步骤并尝试跟踪节点。

通常一个人没有struct liststruct node,但两者都使用node。列表实质上是节点链中的第一个节点。


编码链表时,有一点非常有用,而C一般来说就是使用Valgrind(至少在Linux等上)。为了更有用,您首先需要添加free_list()函数来释放内存。

规则是在使用free编写函数时始终编写malloc函数。

然后使用以下命令运行程序:

$ valgrind ./my_program

如果您使用gcc编译-ggdb来获取Valgrind输出的行号。

这两个对你有用的情况是确保在退出时释放所有内存。这样可以确保您不会遗漏代码中的任何内容。其次,当您访问未初始化的变量等时,您将收到警告。


作为首发,有两件事需要注意:

1。)没有泄漏。 “在退出时使用”应为0。

==13476== HEAP SUMMARY:
==13476==     in use at exit: 0 bytes in 0 blocks
==13476==   total heap usage: 43 allocs, 43 frees, 424 bytes allocated
==13476== 
==13476== All heap blocks were freed -- no leaks are possible

2.使用未初始化的值:

==12972== Conditional jump or move depends on uninitialised value(s)
==12972==    by 0x8048838: insert (foo.c:120)
==12972==    by 0x804890E: main (foo.c:151)
                                      |
                                      |
                                      +--- Shows file and line-number.

或者:

==12820== Use of uninitialised value of size 4
...

之类的。


典型的免费功能可能类似于:

void free_bucket(struct node *node)
{
        int i;

        for (i = 0; i < node->count; ++i) {
                free(node->bucket[i]);
        }
}

void free_list(struct node *head)
{
        struct node *tmp_node;

        while (head != NULL) {
                free_bucket(head);
                tmp_node = head;
                head = head->next;
                free(tmp_node);
        }
}
相关问题