fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. // Node structure for the circular doubly linked list
  6. struct Node {
  7. int data;
  8. Node* next;
  9. Node* prev;
  10.  
  11. Node(int val) : data(val), next(nullptr), prev(nullptr) {}
  12. };
  13.  
  14. class CircularDoublyLinkedList {
  15. private:
  16. Node* head;
  17.  
  18. public:
  19. CircularDoublyLinkedList() : head(nullptr) {}
  20.  
  21. // Function to insert a node at the end of the list
  22. void insert(Node* newNode) {
  23. if (head == nullptr) {
  24. head = newNode;
  25. head->next = head;
  26. head->prev = head;
  27. } else {
  28. Node* last = head->prev;
  29. last->next = newNode;
  30. newNode->prev = last;
  31. newNode->next = head;
  32. head->prev = newNode;
  33. }
  34. }
  35.  
  36. // Function to unlink a node from the list
  37. void unlink(Node* node) {
  38. if (head == nullptr) return;
  39. // If only one node exists, we delete it and set head to nullptr
  40. if (head == node && head->next == head) {
  41. delete head;
  42. head = nullptr;
  43. } else {
  44. // Removing the node from the list
  45. node->prev->next = node->next;
  46. node->next->prev = node->prev;
  47. // updating head node if deleted node is head node
  48. if (head == node) head = node->next;
  49. delete node;
  50. }
  51. }
  52.  
  53. // Function to splice another circular doubly linked list after a given node
  54. void splice(Node* node, CircularDoublyLinkedList& otherList) {
  55. if (head == nullptr || node == nullptr || otherList.head == nullptr) return;
  56.  
  57. Node* nextNode = node->next;
  58. Node* otherHead = otherList.head;
  59. Node* otherLast = otherHead->prev;
  60.  
  61. // Adjusting pointers to splice the lists
  62. node->next = otherHead;
  63. otherHead->prev = node;
  64.  
  65. nextNode->prev = otherLast;
  66. otherLast->next = nextNode;
  67.  
  68. otherList.head = nullptr; // The other list becomes empty after splicing
  69. }
  70.  
  71. // Function to display the circular doubly linked list
  72. void display() {
  73. if (head == nullptr) return;
  74. Node* current = head;
  75. do {
  76. cout << current->data << " ";
  77. current = current->next;
  78. } while (current != head);
  79. cout << endl;
  80. }
  81. };
  82.  
  83. int main() {
  84. CircularDoublyLinkedList CDL;
  85. Node* node1 = new Node(10);
  86. Node* node2 = new Node(20);
  87. Node* node3 = new Node(30);
  88. Node* node4 = new Node(40);
  89. CDL.insert(node1);
  90. CDL.insert(node2);
  91. CDL.insert(node3);
  92. CDL.insert(node4);
  93.  
  94. CircularDoublyLinkedList CDL2;
  95. Node* node5 = new Node(50);
  96. Node* node6 = new Node(60);
  97. CDL2.insert(node5);
  98. CDL2.insert(node6);
  99.  
  100. cout << "Original CDL: ";
  101. CDL.display();
  102.  
  103. cout << "CDL2: ";
  104. CDL2.display();
  105.  
  106. // Unlinking the second node
  107. CDL.unlink(node2);
  108. cout << "After unlinking c2: ";
  109. CDL.display();
  110.  
  111. // Splicing CDL2 after the second node of modified CDL
  112. CDL.splice(node3, CDL2);
  113. cout << "After splicing: ";
  114. CDL.display();
  115.  
  116. return 0;
  117. }
  118.  
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
Original CDL: 10 20 30 40 
CDL2: 50 60 
After unlinking c2: 10 30 40 
After splicing: 10 30 50 60 40