Dear Programmers,
i have a linked list problem. I have have a number, for example 197, and put it in an array----> [0] = 1 , [1]=9, [2]=7
So far so good. Now, i want to make a linked list of these numbers. I have made the following code:
The problem is, that it's nog working. I think there is something wrong in the linking....
Code:int length = 0; char buffer[50]; itoa(num, buffer, 10); //find the length of the charater number for(int i=0; i<10; i++){ if(buffer[i] == '\0'){ length = i; } } printf("%d\n",length); struct node *head; struct node *lp; if(length>0){ //allocate memory for the first elemement. //head = (node*)malloc(sizeof(struct node)); head =(struct node *) malloc(sizeof(struct node)); head->data = buffer[0]; printf("%c\n",head->data); head->next=0; } lp = head; for(int i=1; i<length;i++){ //because 0 is done above) //lp->next = (node*)malloc(sizeof(struct node)); lp->next =(struct node *) malloc(sizeof(struct node)); //lp =(struct node *) malloc(sizeof(struct node)); lp = lp->next; lp->data = buffer[i]; printf("%c\n",lp->data); head->next=0; } /* printing the entire list */ //lp = head; while( lp != NULL ) //continue whilst there are nodes left { printf("%c\n", lp->data ); //print out the current node lp = lp->next; //goto the next node in the list } return head;



LinkBack URL
About LinkBacks


