fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct Node
  5. {
  6. int data;
  7. struct Node* next;
  8. };
  9. typedef struct Node node;
  10.  
  11. node* head=NULL;
  12.  
  13. void
  14. insert(int x,int t)
  15. {
  16. node* temp;
  17. temp=(node*)malloc(sizeof(node));
  18. temp->data=x;
  19. temp->next=NULL;
  20. if(t==1)
  21. {
  22. temp->next=head;
  23. head=temp;
  24. return;
  25. }
  26. node* temp1;
  27. temp1=head;
  28. int m;
  29. m=1;
  30. while(temp1!=NULL && m<t-1)
  31. {
  32. temp1=temp1->next;
  33. m++;
  34.  
  35. }
  36. if(m==t-1 && temp1!=NULL ){
  37. temp->next=temp1->next;
  38. temp1->next=temp;
  39. }
  40. else
  41. printf("INVALID OPERATION\n");
  42. }
  43.  
  44. void
  45. print()
  46. {
  47. node* temp=head;
  48. while(temp!=NULL)
  49. {
  50. printf("%d\n",temp->data);
  51. temp=temp->next;
  52. }
  53. }
  54.  
  55. int main()
  56. {
  57. int x,j,t,l;
  58. int i=0;
  59. while(i<1)
  60. {
  61. scanf("%d%d",&x,&t);
  62. insert(x,t);
  63. //cout<<"do you want to insert more press 1 for yes and 2 for no";
  64. scanf("%d",&l);
  65. if(l==1)
  66. i=0;
  67. else
  68. i=2;
  69. }
  70. print();
  71. return 0;
  72. }
Success #stdin #stdout 0s 3416KB
stdin
10 1
1
12 1
1
14 2
1
13 4
1
17 3
2
stdout
12
14
17
10
13