fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct node* head;
  4. struct node{
  5. long long data;
  6. struct node* next;
  7. };
  8. void insert(long long x)
  9. {
  10. struct node* temp1=(node*)malloc(sizeof(struct node));
  11. temp1->data=x;
  12. struct node* temp2=head;
  13. if(head==NULL)
  14. {
  15. head=temp1;
  16. head->next=NULL;
  17. }
  18. else
  19. {
  20. while(temp2->next!=NULL)
  21. {
  22. temp2=temp2->next;
  23. }
  24. temp1->next=NULL;
  25. temp2->next=temp1;
  26. }
  27. }
  28. void print()
  29. {
  30. struct node* temp=head;
  31. while(temp!=NULL)
  32. {
  33. printf("%lld ",temp->data);
  34. temp=temp->next;
  35. }
  36. }
  37. int main()
  38. {
  39. long long n,i,x;
  40. head=NULL;
  41. scanf("%lld",&n);
  42. for(i=0;i<n;i++)
  43. {
  44. scanf("%lld",&x);
  45. insert(x);
  46. print();
  47. }
  48. }
  49.  
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
Standard output is empty