fork download
  1. #include<iostream>
  2. #include<stdio.h>
  3. using namespace std;
  4. struct node
  5. {
  6. int data;
  7. node* next;
  8. };
  9. node *head=NULL;
  10. node *last;
  11. void insert(int x)
  12. {
  13. node* temp;
  14. temp=new node;
  15. temp->data=x;
  16. temp->next=NULL;
  17.  
  18. if(head==NULL){
  19. head=temp;
  20. last=temp;
  21. }
  22. else
  23. {
  24. last->next=temp;
  25. last=temp;
  26. }
  27. }
  28.  
  29.  
  30. void print()
  31. {
  32. node* temp=head;
  33. while(temp!=NULL)
  34. {
  35. printf("%d\n",temp->data);
  36. temp=temp->next;
  37. }
  38. }
  39. int main()
  40. {
  41. int x,n,i;
  42. node* head;
  43. head=NULL;
  44. printf("enter the no of=\n");
  45. scanf("%d",&n);
  46. for(i=0;i<n;i++)
  47. {
  48. printf("enter the elements\n");
  49. scanf("%d",&x);
  50. insert(x);
  51. print();
  52. }
  53. return 0;
  54. }
  55.  
Success #stdin #stdout 0s 3460KB
stdin
6
4
7
2
8
9
3
stdout
enter the no of=
enter the elements
4
enter the elements
4
7
enter the elements
4
7
2
enter the elements
4
7
2
8
enter the elements
4
7
2
8
9
enter the elements
4
7
2
8
9
3