Friday, July 31, 2009

Problem with Doubly Linked Lists in C ?????

#include%26lt;stdio.h%26gt;


#include%26lt;malloc.h%26gt;





struct ListEntry


{


int number;


struct ListEntry *next;


struct ListEntry *previous;


};





void main()


{


int i=0;


struct ListEntry start, *node;





start.next ='\0';


start.previous= '\0';





node = %26amp;start;





printf("Address of the 1st node-%26gt;%d\n",node);





//TROUBLE STARTS


for(i=1; i%26lt;=10;i++)


{


node-%26gt;next = (struct ListEntry *)malloc(sizeof(struct ListEntry));


node-%26gt;next-%26gt;previous =node;


node = node-%26gt;next;


printf("Number-%26gt;%d node is %d\n",i,node);


node-%26gt;number = 50+i;


node-%26gt;next= '\0';


}


//TROUBLE ENDS


node = start.next;





do


{


printf("%d\n",node-%26gt;number);


printf("%d\n",node);


node = node-%26gt;next;


}while(node-%26gt;next);





printf("\n\nReversing\n\n");


do


{


printf("%d\n",node-%26gt;number);


printf("%d\n",node);


node = node-%26gt;previous;


}while(node-%26gt;previous);


}


I am having trouble understanding the for loop part !!

Problem with Doubly Linked Lists in C ?????
I believe the for loop will allocate 10 new nodes, setting the appropriate next and prior links, and assign the values 51 through 61 to the "number" variables within the nodes.





The printfs that follow should prove that out as they're responsible for dumping the contents of the list.
Reply:I believe this statement is illegal in C:





node = %26amp;start;


this could be done in C++, but it would be pointless in this context.


No comments:

Post a Comment