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