Arun Srikanth Sridhar
Arun Srikanth Sridhar

Reputation: 65

Linked lists with structures

I am trying to implement an array of linked lists. Now the linked lists contains a struct not a single data. I am not sure how to proceed with this. When I do the following I get a lot of errors.

How should I go about this ? Thanks

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

}

Upvotes: 1

Views: 144

Answers (2)

Arun Srikanth Sridhar
Arun Srikanth Sridhar

Reputation: 65

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

Upvotes: 0

Valentin Trinqu&#233;
Valentin Trinqu&#233;

Reputation: 1072

There are a lots of problems in your code.

1. main() prototype

the prototype of the function main is well defined in the standard and should be either int main(void) { } or int main(int argc, char **arg) { }

2. declare your structures in a header file

Some people may argue but, it's better to declare your structure in a header file. This way you can include this header file whenever you need to use your structures and typedef. Ok, it's just about clean and reusable code...

Also, keep in mind the naming convention in snake case. Hence, you should use lower case name for your struct.

3. algorithm logic

I bet you would like to create an array of linked list. The logic you have is kinda wrong. It just doesn't make any sense.

You try to access to head elements, but you never allocated them. Moreover, you should allocate an array which holds pointers not structures. So, for your array, you should have node** array = malloc(sizeof(node*) * size_of_array). This way, in every element, you will store the head of every linked list you created.

The way you allocate your structure isn't very nice neither reusable. You should have a function, which allocate a new element, bind it to the list and return a new element.

You should always check system call return values.


SOLUTION

Note: I changed the structures names. Be careful.

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

Upvotes: 2

Related Questions