linked list.

Revision en1, by vsanjay_nitdgp, 2015-08-11 07:53:55

sir....the following code....i used succesfully compiled and got correct answer...i.e insert a number at end position...this is my code

include<stdio.h>

include<stdlib.h>

struct node* head; struct node{ long long data; struct node* next; }; void insert(long long x) { struct node* temp1=(node*)malloc(sizeof(struct node)); temp1->data=x; struct node* temp2=head; if(head==NULL) { head=temp1; head->next=NULL; } else { while(temp2->next!=NULL) { temp2=temp2->next; } temp1->next=NULL; temp2->next=temp1; } } void print() { struct node* temp=head; while(temp!=NULL) { printf("%lld ",temp->data); temp=temp->next; } } int main() { long long n,i,x; head=NULL; scanf("%lld",&n); for(i=0;i<n;i++) { scanf("%lld",&x); insert(x); print(); } }

but first time i got wrong because in print fuction in while loop i used "temp->next!=NULL" instead of "temp".....could any one say why did u get wrong...before.

Tags data structures

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English vsanjay_nitdgp 2015-08-11 07:55:30 2 Tiny change: 'y why did u get wrong' -> 'y why did i get wrong'
en2 English vsanjay_nitdgp 2015-08-11 07:54:48 713
en1 English vsanjay_nitdgp 2015-08-11 07:53:55 1037 Initial revision (published)