fork download
  1.  
  2. #include<iostream>
  3. #include<cstdlib>
  4. #include<string>
  5. #include<climits>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. struct node
  11. {
  12. int info;
  13. struct node *next;
  14. };
  15.  
  16.  
  17. void push(struct node **manage,int ele)
  18. {
  19. struct node *temp=(struct node *)malloc(sizeof(struct node));
  20. temp->info=ele;
  21. temp->next=*manage;
  22. *manage=temp;
  23. }
  24.  
  25.  
  26. void display(struct node *manage)
  27. {
  28. if(manage==nullptr)
  29. cout<<"\n\n Linked list is empty!!!";
  30. else
  31. {
  32. while(manage!=nullptr)
  33. {
  34. cout<<' '<<manage->info;
  35. manage=manage->next;
  36. }
  37. }
  38. }
  39.  
  40. void Rotate_Linked_List(struct node **headref,int k)
  41. {
  42. struct node *head=*headref,*ahead;
  43. int count1=0;
  44. if(k<=0)
  45. {
  46. cout<<"\n\n Plz give the value of k +ve !!!";
  47. return;
  48. }
  49. while(++count1<k&&head)
  50. {
  51. head=head->next;
  52. count1;
  53. }
  54. if(head==nullptr)
  55. {
  56. cout<<"\n\n Plz enter the value of k Less than or equal to size of the list!!!";
  57. return;
  58. }
  59. ahead=head->next;
  60. head->next=nullptr;
  61. head=ahead;
  62. while(ahead->next!=nullptr)
  63. ahead=ahead->next;
  64. ahead->next=*headref;
  65. *headref=head;
  66. }
  67.  
  68.  
  69. int main()
  70. {
  71. struct node *first=nullptr;
  72. int k=4;
  73. push(&first,60);
  74. push(&first,50);
  75. push(&first,40);
  76. push(&first,30);
  77. push(&first,20);
  78. push(&first,10);
  79. display(first);
  80. Rotate_Linked_List(&first,k);
  81. cout<<"\n\n Rotated Linked List By "<<k<<" Nodes : ";
  82. display(first);
  83. return(0);
  84. }
  85.  
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
 10 20 30 40 50 60

 Rotated Linked List By 4 Nodes :  50 60 10 20 30 40