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. void insert(int x)
  11. {
  12. node* temp;
  13. temp=new node;
  14. temp->data=x;
  15. temp->next=NULL;
  16. if(head==NULL)
  17. head=temp;
  18. else
  19. {
  20. temp->next=head;
  21. head=temp;
  22. }
  23.  
  24. }
  25. void print()
  26. {
  27. node* temp=head;
  28. while(temp!=NULL)
  29. {
  30. printf("%d\n",temp->data);
  31. temp=temp->next;
  32. }
  33. }
  34. int main()
  35. {
  36. int x,n,i;
  37. node* head;
  38. head=NULL;
  39. printf("enter the no of=\n");
  40. scanf("%d",&n);
  41. for(i=0;i<n;i++)
  42. {
  43. printf("enter the elements\n");
  44. scanf("%d",&x);
  45. insert(x);
  46. print();
  47. }
  48. return 0;
  49. }
Success #stdin #stdout 0s 3460KB
stdin
2
2
3
stdout
enter the no of=
enter the elements
2
enter the elements
3
2