带结构的链接列表

时间:2015-12-08 20:38:58

标签: c

我正在尝试实现一系列链表。现在,链表包含结构而不是单个数据。我不知道如何处理这个问题。当我做以下操作时,我会遇到很多错误。

我应该怎么做? 谢谢

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

void main(){

    int i,j;

    typedef struct{
        int a;
        int b;
    }Integers;

    typedef struct{
        Integers Int;
        struct Node *next;
    }Node;

    Node  *curr,*head,*nullhead;
    curr=(Node *)malloc(sizeof(Node)*100);
    //head=(Node *)malloc(sizeof(Node)*100);


    for (i=0;i<10;i++){
        head[i]= NULL;
        for(j=0;j<10;j++){
            curr[i].Int.a=j;
            curr[i].Int.a=j;
            curr[i].next=head[i];
            head[i]=curr[i];
        }
    }

}

2 个答案:

答案 0 :(得分:2)

您的代码中存在许多问题。

<强> 1。 main()原型

函数main的原型在标准中已明确定义,应为int main(void) { }int main(int argc, char **arg) { }

<强> 2。在头文件中声明您的结构

有些人可能会争辩,但最好在头文件中声明您的结构。这样,只要需要使用结构和typedef,就可以包含此头文件。好吧,这只是干净和可重复使用的代码...

另外,请记住蛇案例中的命名约定。因此,您应该为结构使用小写名称。

第3。算法逻辑

我打赌你想创建一个链表列表。你的逻辑有点不对劲。它没有任何意义。

您尝试访问head元素,但您从未分配过它们。此外,您应该分配一个包含指针而不是结构的数组。因此,对于您的阵列,您应该node** array = malloc(sizeof(node*) * size_of_array)。这样,在每个元素中,您将存储您创建的每个链接列表的头部。

你分配你的结构的方式不是很好,既不可重复使用。你应该有一个函数,它分配一个新元素,将它绑定到列表并返回一个新元素。

您应该始终检查系统调用返回值。

<强>解

注意:我更改了结构名称。小心。

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

typedef struct point_s
{
    int x;
    int y;
} point;

typedef struct node_s
{
    point coord;
    struct node_s* next;
} node;

node* add_front_node(node** head)
{
    node* new_elem;

    new_elem = malloc(sizeof(node));
    if (new_elem != NULL)
    {
        new_elem->next = *head;
        *head = new_elem;
    }
    return new_elem;
}

node* create_list_of(size_t size)
{
    node* list = NULL;

    /* create the list */
    for (int i = 0; i < size; i++)
    {
        /* add a node to the list */
        node* new_node = add_front_node(&list);
        if (new_node == NULL)
            return NULL;
        /* set content with random value */
        new_node->coord.x = i;
    }
    return list;
}

void print_array_linked_list(node** array)
{
    for (int i = 0; i < 10; i++)
    {
        printf("array element :%d\n", i);
        node* head = array[i];
        while (head != NULL)
        {
            printf("%d\n", head->coord.x);
            head = head->next;
        }
    }
}

int main(void)
{
    /*
    * Create array of linked list
    * Note the `node**`, it's an array which hold pointers
    * on node structure which represent the beginning of
    * your linked list! Not directly node structure !
    */
    node** array = malloc(sizeof(node*) * 10);
    if (array == NULL)
        return -1;

    /* Fill the array */
    for (int i = 0; i < 10; i++)
    {
        array[i] = create_list_of(10);
    }

    print_array_linked_list(array);

    return 0;
}

答案 1 :(得分:0)

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

typedef struct _node {
  int data;
  struct _node *next;
} Node;

Node *addfrontnode(Node **head,int data){
  Node *curr;

  curr = (Node *)malloc(sizeof(Node));
  curr->data = data;
  curr->next = *head;
  return curr;
}

int main(void) {
  int i, j;

  Node **head, *curr;
  head = (Node **)malloc(sizeof(Node *));

  for(j=1; j<=10; j++)
  {
    head[j] = NULL;
    curr = NULL;
    for(i=1; i <= 10; i++) {
      curr = addfrontnode(&head[j],i);
      head[j] = curr;
    }
  }

  i = 10;

  while(head[i]) {
    printf("%d\n",head[i]->data);
    head[i] = head[i]->next;
  }

  return 0;
}
相关问题