c - dealing with array of linked list -


my approach:

an array of fixed-length (lets 20) each element pointer first node of linked list. have 20 different linked list.

this structure:

struct node{        char data[16];        struct node *next; }; 

my declaration array

struct node *nodesarr[20]; 

now add new node 1 of linked list, this:

struct node *temp;  temp = nodesarr[i]; // declared , less 20 addnode(temp,word); // word declared (char *word) , has value ("hello") 

the addnode function:

void addnode(struct node *q, char *d){     if(q == null)         q = malloc(sizeof(struct node));     else{         while(q->next != null)             q = q->next;          q->next = malloc(sizeof(struct node));         q = q->next;     }      q->data = d; // must done using strncpy     q->next = null;  } 

and print data array of linked list, this:

void print(){     int i;     struct node *temp;      for(i=0 ; < 20; i++){         temp = nodesarr[i];         while(temp != null){             printf("%s\n",temp->data);             temp = temp->next;         }     } } 

now compiler gives no error, program run , pass data it, , when call print doesn't print thing,,??

update::

after edited code (thx you), think problem in print function,, idea ?

the problem lies in addnode(). when list empty do:

q = malloc(sizeof(struct node)); 

but scope of q limited addnode(). should have declared addnode() as

void addnode(struct node **q, char *d) 

and adjust code accordingly:

*q = malloc(sizeof(struct node)); 

and on...


Comments

Popular posts from this blog

Delphi Wmi Query on a Remote Machine -