fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. template<class T>
  5. class LinkedList
  6. {
  7. struct Node{
  8. T data;
  9. Node * next;
  10. Node(T val): data(val), next(nullptr){}
  11. };
  12.  
  13. Node* head;
  14.  
  15. public:
  16.  
  17. LinkedList() : head(nullptr) {}
  18.  
  19. void insert(T val)
  20. {
  21. Node* node = new Node(val);
  22. Node* tmp = head;
  23.  
  24. if(tmp == nullptr)
  25. {
  26. head = node;
  27. }
  28. else
  29. {
  30. while(tmp->next != nullptr)
  31. {
  32. tmp = tmp->next;
  33. }
  34. tmp->next = node;
  35. }
  36. }
  37.  
  38. void swapLinks(T val1, T val2)
  39. {
  40. Node* prevNode1 = nullptr;
  41. Node* prevNode2 = nullptr;
  42. Node* nextNode1 = nullptr;
  43. Node* nextNode2 = nullptr;
  44. Node* node = head;
  45. Node* node1 = search(val1);
  46. Node* node2 = search(val2);
  47. Node* tmp;
  48. //if val1 is head node
  49. if(node1 == head)
  50. {
  51. nextNode1 = node1->next;
  52.  
  53. while(node->next != node2)
  54. {
  55. node = node->next;
  56. }
  57. if(node->next == node2)
  58. {
  59. prevNode2 = node;
  60. nextNode2 = node2->next;
  61. }
  62.  
  63. tmp = node1;
  64. head = node2;
  65. node2->next = nextNode1;
  66. prevNode2->next = node1;
  67. node1->next = nextNode2;
  68. }
  69. //if val2 is head node
  70. else if(node2 == head)
  71. {
  72. nextNode2 = node2->next;
  73.  
  74. while(node->next != node1)
  75. {
  76. node = node->next;
  77. }
  78. if(node->next == node1)
  79. {
  80. prevNode1 = node;
  81. nextNode1 = node1->next;
  82. }
  83.  
  84. tmp = node2;
  85. head = node1;
  86. node1->next = nextNode2;
  87. prevNode1->next = node2;
  88. node2->next = nextNode1;
  89. }
  90. //neirher val1 is head node nor val2
  91. else
  92. {
  93. while(node->next != node1)
  94. {
  95. node = node->next;
  96. }
  97. if(node->next == node1)
  98. {
  99. prevNode1 = node;
  100. nextNode1 = node1->next;
  101. }
  102.  
  103. node = head;
  104.  
  105. while(node->next != node2)
  106. {
  107. node = node->next;
  108. }
  109. if(node->next == node2)
  110. {
  111. prevNode2 = node;
  112. nextNode2 = node2->next;
  113. }
  114.  
  115. prevNode1->next = node2;
  116. tmp = node2->next;
  117. node2->next = node1->next;
  118. prevNode2->next = node1;
  119. node1->next = tmp;
  120. }
  121. }
  122.  
  123. friend std::ostream & operator <<(std::ostream & os, LinkedList<T>& ll)
  124. {
  125. ll.display(os);
  126. return os;
  127. }
  128.  
  129. private:
  130.  
  131. struct Node *search(T val)
  132. {
  133. Node* node = head;
  134. while(node != nullptr)
  135. {
  136. if(node->data == val)
  137. {
  138. return node;
  139. }
  140. node = node->next;
  141. }
  142. std::cerr << "No such element in the List \n";
  143. return nullptr;
  144. }
  145.  
  146. void display(std::ostream& out = std::cout) const
  147. {
  148. Node* node = head;
  149. while(node != nullptr)
  150. {
  151. out << node->data <<" ";
  152. node = node->next;
  153. }
  154. }
  155. };
  156.  
  157. int main()
  158. {
  159. LinkedList<int> ll1;
  160. ll1.insert(10);
  161. ll1.insert(15);
  162. ll1.insert(12);
  163. ll1.insert(13);
  164. ll1.insert(28);
  165. ll1.insert(14);
  166. ll1.insert(16);
  167. std::cout << "LinkedList1 : "<< ll1 <<"\n";
  168. ll1.swapLinks(12, 16);
  169. std::cout << ll1 <<"\n";
  170. LinkedList<char> ll2;
  171. ll2.insert('t');
  172. ll2.insert('r');
  173. ll2.insert('f');
  174. ll2.insert('x');
  175. ll2.insert('a');
  176. ll2.insert('y');
  177. ll2.insert('n');
  178. std::cout << "LinkedList2 : "<< ll2 <<"\n";
  179. ll2.swapLinks('t', 'a');
  180. std::cout << ll2 <<"\n";
  181. LinkedList<std::string> ll3;
  182. ll3.insert("apple");
  183. ll3.insert("dog");
  184. ll3.insert("car");
  185. ll3.insert("code");
  186. std::cout << "LinkedList3 : "<< ll3 <<"\n";
  187. ll3.swapLinks("apple", "car");
  188. std::cout << ll3 <<"\n";
  189. return 0;
  190. }
  191.  
Success #stdin #stdout 0s 15248KB
stdin
Standard input is empty
stdout
LinkedList1 : 10 15 12 13 28 14 16 
10 15 16 13 28 14 12 
LinkedList2 : t r f x a y n 
a r f x t y n 
LinkedList3 : apple dog car code 
car dog apple code