fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Node
  5. {
  6. int value;
  7. Node *next;
  8. };
  9.  
  10. void removeDivFive(Node* head){
  11. int count = 0;
  12. Node *temp = head, *next;
  13.  
  14. while (temp != NULL){
  15. count++;
  16. if ((count % 4) == 0){
  17. if (temp->next != NULL){
  18. next = temp->next->next;
  19. delete temp->next;
  20. temp->next = next;
  21. }
  22. }
  23. temp = temp->next;
  24. }
  25. }
  26.  
  27. void display(Node* head)
  28. {
  29. while (head != NULL){
  30. cout << head->value << ' ';
  31. head = head->next;
  32. }
  33. cout << endl;
  34. }
  35.  
  36. void clear(Node* head)
  37. {
  38. while (head != NULL){
  39. Node *next = head->next;
  40. delete head;
  41. head = next;
  42. }
  43. }
  44.  
  45. int main()
  46. {
  47. Node *head = NULL, **temp = &head;
  48. for(int i = 1; i <= 20; ++i)
  49. {
  50. *temp = new Node{i, NULL};
  51. temp = &((*temp)->next);
  52. }
  53.  
  54. display(head);
  55. removeDivFive(head);
  56. display(head);
  57. clear(head);
  58.  
  59. return 0;
  60. }
Success #stdin #stdout 0.01s 5536KB
stdin
Standard input is empty
stdout
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 
1 2 3 4 6 7 8 9 11 12 13 14 16 17 18 19