Thursday 15 April 2010

c - How to solve incompatible types in assignment using node pointer to set up a Linked List -



c - How to solve incompatible types in assignment using node pointer to set up a Linked List -

okay have far...

typedef struct node{ int *next; int val; }node; void pqprint(){ node *current=front; printf("queue contains:"); while(current->next!=null){ printf(" %d ", current->val); node temp; temp.next=current->next; current->next=temp; } printf("\n"); }

i maintain getting error stated above current->next=temp;

i didn't find error in description, anyway, there few things here. wrote finish programme based on listing above:

#include <stdio.h> #include <stdlib.h> typedef struct node{ struct node *next; int val; }node; node *front; void pqprint() { node *current=front; printf("queue contains:"); while(current != null){ printf(" %d ", current->val); current = current->next; } printf("\n"); } void main(void) { front end = malloc(sizeof(front)); front->val = 10; front->next = null; pqprint(); }

a few comments: 1. next element in struct shouldn't of type int, compilers complain. 2. need declare list start front, , initialize it. 3. need allocate memory each element in list, typically using malloc(), note illustration doesn't validate result (in real life, must check against null homecoming malloc()). 4. if have pointer left, need utilize -> (not .) refer separate elements in struct pointer points to. 5. guess temp variable confused removed it. note in example, miss first element.

c

No comments:

Post a Comment