fork download
  1. #include <iostream>
  2. #include <stdlib.h>
  3. using namespace std;
  4.  
  5. /* structure for a node */
  6. struct node
  7. {
  8. int data;
  9. struct node *next;
  10. node() : data(0), next(NULL){};
  11. };
  12.  
  13. /* Function to insert a node at the begining of a Circular
  14.   linked list */
  15. void push(struct node **head_ref, int data)
  16. {
  17. struct node *ptr = (struct node*)malloc(sizeof(struct node));
  18. ptr->data = data;
  19. ptr->next = *head_ref;
  20. struct node *temp = *head_ref;
  21. /* If linked list is not NULL then set the next of last node.
  22. It is going to last node by circling 1 times.
  23. */
  24. if(*head_ref != NULL){
  25. while(temp->next != *head_ref){
  26. temp = temp->next;
  27. }
  28. //set last node by ptr
  29. temp->next = ptr;
  30. }
  31. else{
  32. // 1 node circular linked list
  33. ptr->next = ptr;
  34. }
  35. // after push ptr is the new node
  36. *head_ref = ptr;
  37. }
  38.  
  39. //get the previous node
  40. struct node* getPreviousNode(struct node* current_node){
  41. struct node* prev = current_node;
  42. while(prev->next != NULL && prev->next->data != current_node->data ){
  43. prev = prev->next;
  44. }
  45. return prev;
  46. }
  47.  
  48.  
  49. /* Given a reference (pointer to pointer) to the head of a list
  50.   and a key, deletes the first occurrence of key in linked list */
  51. void deleteNodeByKey(struct node **head_ref, int key)
  52. {
  53. node dummy;
  54. dummy.next = *head_ref;
  55.  
  56. // Store head node
  57. struct node* current_node = &dummy, *prev = &dummy;
  58. current_node = current_node->next;
  59.  
  60. while(current_node->next != NULL && current_node->data != key){
  61. prev = prev->next;
  62. current_node = current_node->next;
  63. }
  64.  
  65. if(current_node == NULL){
  66. return;
  67. }
  68.  
  69.  
  70. // 11->2->56->12
  71. //Removing the node
  72. prev->next = current_node->next;
  73. current_node->next = NULL;
  74. free(current_node);
  75. *head_ref = dummy.next;
  76. return;
  77.  
  78. }
  79.  
  80.  
  81. /* Function to print nodes in a given Circular linked list */
  82. void printList(struct node *head)
  83. {
  84. struct node *temp = head;
  85. if(head != NULL){
  86. /*
  87. do-while because at 1st temp points head
  88. and after 1 rotation temp wil come back to head again
  89. */
  90. do{
  91. cout<<temp->data<<' ';
  92. temp = temp->next;
  93. }
  94. while(temp != head);
  95. cout<<endl;
  96. }
  97. }
  98.  
  99. int main() {
  100. /* Initialize lists as empty */
  101. struct node *head = NULL;
  102. /* Created linked list will be 11->2->56->12 */
  103. push(&head, 12);
  104. push(&head, 56);
  105. push(&head, 2);
  106. push(&head, 11);
  107. cout<<"Contents of Circular Linked List"<<endl;
  108. printList(head);
  109.  
  110. deleteNodeByKey(&head, 11);
  111. printList(head);
  112.  
  113. return 0;
  114. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
Contents of Circular Linked List
11 2 56 12 
2 56 12 0