fork(1) download
  1. #include<iostream>
  2. #include<cstdio>
  3. using namespace std;
  4. struct node
  5. {
  6. int data;
  7. node* next;
  8. };
  9. node* head=NULL;
  10. void insert(int x,int t)
  11. {
  12. node* temp;
  13. temp=new node();
  14. temp->data=x;
  15. temp->next=NULL;
  16. if(t==1)
  17. {
  18. temp->next=head;
  19. head=temp;
  20. return;
  21. }
  22. node* temp1;
  23. temp1=head;
  24. int m;
  25. m=1;
  26. while(m<=t-1)
  27. {
  28. temp1=temp1->next;
  29. m++;
  30. }
  31. temp->next=temp1->next;
  32. temp1->next=temp;
  33.  
  34. }
  35. void print()
  36. {
  37. node* temp=head;
  38. while(temp!=NULL)
  39. {
  40. printf("%d\n",temp->data);
  41. temp=temp->next;
  42. }
  43. }
  44. int main()
  45. {
  46. int x,j,t,l;
  47.  
  48. int i=0;
  49. while(i<1)
  50. {
  51. cin>>x>>t;
  52. insert(x,t);
  53. cout<<"do you want to insert more press 1 for yes and 2 for no";
  54. cin>>l;
  55. if(l==1)
  56. i=0;
  57. }
  58. print();
  59. return 0;
  60. }
Runtime error #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
Standard output is empty