c - Single list printing error -
this how program doesn't print full list node's data entered last.i couldn't understand problem in linking or not :
basic structure of node
struct node { int data; struct node *link; };
defining header start of link list :
struct node *header;
functions insert , print :
void insertfront_sl(); void print_sl();
the main function :
void main() { clrscr(); header=(struct node *)malloc(sizeof(struct node)); header->link=null; header->data=null; insertfront_sl(); insertfront_sl(); insertfront_sl(); insertfront_sl(); print_sl(); getch(); } void insertfront_sl(){ struct node *temp; int x; temp=(struct node *)malloc(sizeof(struct node)); if(temp==null) { printf("\nmem0ry insufficient .."); } else { printf("\ngot new node \nnow insert data node : "); scanf("%d",&x); temp->data=x; header->link=temp; } } void print_sl(){ struct node *ptr; ptr=header; while(ptr->link !=null) { ptr=ptr->link; printf("%d\t",ptr->data); } }
each time create new node losing reference nodes created previously. after add new node:
header->link=temp;
you need set head of list new node:
header = temp;
you should keep node reference base of list (the first node), , use base starting point printing list.
Comments
Post a Comment