fork download
  1. /*Adobe Elitmus Written Test on 27-10-2013 @Bangalore - Algo/DS questions
  2.  *Question 1
  3.  */
  4.  
  5. /* Reverse every alterante K elements of a list
  6.  * It is to be noted that Nodes have to reversed and not the values
  7.  * e.g. if k=3 and list is
  8.  * Input 1->2->3->4->5->6->7->8->9->NULL
  9.  * Output 3->2->1->4->5->6->9->8->7->NULL
  10.  *
  11. */
  12.  
  13. #include<stdio.h>
  14. #include<stdlib.h>
  15.  
  16. typedef struct SLL{
  17. int element;
  18. struct SLL *next;
  19. }Node;
  20.  
  21. Node *start=NULL;
  22.  
  23. void InsertNode(Node *start, int item)
  24. {
  25. Node *tmp=start;
  26.  
  27. if(tmp == NULL)
  28. {
  29. printf("Error : Start pointer is NUll\n");
  30. return;
  31. }
  32.  
  33. //construct a node with item element
  34. Node *node=malloc(sizeof(Node));
  35. node->next=NULL;
  36. node->element=item;
  37.  
  38. while(tmp->next !=NULL) tmp=tmp->next;
  39.  
  40. tmp->next=node; //node inerted at end
  41. }
  42.  
  43. void PrintListElements(Node *start)
  44. {
  45. Node *tmp=start;
  46.  
  47. while(tmp != NULL)
  48. {
  49. printf("%d->",tmp->element);
  50. tmp=tmp->next;
  51. }
  52. printf("NULL\n");
  53. }
  54.  
  55. void ReverseAlternateKElements(Node *start, int K, Node **startPointer)
  56. {
  57. int i,j,count;
  58. Node *current=start;
  59. Node *prev=NULL,*prev2=NULL;
  60. Node *save=NULL;
  61. Node *next=NULL;
  62. Node *tmp=NULL;
  63.  
  64. for(i=0;;)
  65. {
  66. if(current == NULL) break;
  67. if(i%K==0)
  68. {
  69. count=0;
  70. save=current;
  71. //printf("current=%d\n",current->element);
  72. for(j=0;j<K;)
  73. {
  74. if(current == NULL) break;
  75. next=current->next;
  76. current->next=prev;
  77. prev=current;
  78. current=next;
  79. j++;
  80. ++count;
  81. }
  82. if(i<K)
  83. {
  84. *startPointer=prev;
  85. tmp=prev;
  86. }
  87. save->next=current; // for last node of the series to point to next current;
  88.  
  89. if(prev2 != NULL)
  90. prev2->next=prev;
  91. if(current == NULL) break;
  92. //PrintListElements(tmp);
  93. i=i+count+1;
  94.  
  95. }
  96. else
  97. {
  98.  
  99. current = current->next;
  100. if(current == NULL) break;
  101. i++;
  102. if(i%K==0)
  103. {
  104. prev=current;
  105. prev2=current;
  106. current=current->next;
  107. }
  108. }
  109. }
  110.  
  111. printf("----Reversal Done!!! :)\n");
  112. //PrintListElements(tmp);
  113.  
  114. }
  115.  
  116.  
  117.  
  118. int main()
  119. {
  120. int K=4; //Input
  121. start=malloc(sizeof(Node));
  122.  
  123.  
  124. start->next=NULL;
  125. start->element=1; // Inputs
  126. InsertNode(start,2); //Inputs .....
  127. InsertNode(start,3);
  128. InsertNode(start,4);
  129. InsertNode(start,5);
  130. InsertNode(start,6);
  131. InsertNode(start,7);
  132. InsertNode(start,8);
  133. InsertNode(start,9);
  134. InsertNode(start,10);
  135. InsertNode(start,11);
  136. InsertNode(start,12);
  137. InsertNode(start,13);
  138. InsertNode(start,14);
  139.  
  140. printf("Element Before Reversal:\n");
  141. PrintListElements(start);
  142.  
  143. ReverseAlternateKElements(start, K, &start);
  144.  
  145.  
  146. printf("Element After Reversal:\n");
  147. PrintListElements(start);
  148.  
  149. return 0;
  150. }
  151.  
Success #stdin #stdout 0s 2424KB
stdin
Standard input is empty
stdout
Element Before Reversal:
1->2->3->4->5->6->7->8->9->10->11->12->13->14->NULL
----Reversal Done!!! :)
Element After Reversal:
4->3->2->1->5->6->7->8->12->11->10->9->13->14->NULL