fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Node{
  5. int data;
  6. Node* pNext;
  7. };
  8.  
  9. void remove1stX(Node* &pFirst, int x);
  10. void removeAllXs(Node* &pFirst, int x);
  11. void removeBasedOnK(Node* &pHead, int k);
  12.  
  13. //-----------------------------------------------------//
  14.  
  15. void remove1stX(Node* &pFirst, int x) {
  16. if(pFirst == nullptr) return;
  17. if(pFirst->data == x) {
  18. Node* pLast = pFirst;
  19. while(pLast->pNext != pFirst)
  20. pLast = pLast->pNext;
  21.  
  22. if(pLast == pFirst) {
  23. delete pFirst;
  24. pFirst = nullptr;
  25. return;
  26. }
  27.  
  28. pLast->pNext = pFirst->pNext;
  29. delete pFirst;
  30. pFirst = pLast->pNext;
  31. return;
  32. }
  33.  
  34. Node* cur = pFirst;
  35. while(cur->pNext->data != x &&
  36. cur->pNext->pNext != pFirst->pNext) {
  37. cur = cur->pNext;
  38. }
  39. if(cur->pNext->data == x) {
  40. Node* tmp = cur->pNext;
  41. cur->pNext = cur->pNext->pNext;
  42. delete tmp;
  43. }
  44. }
  45.  
  46.  
  47. //-----------------------------------------------------//
  48.  
  49. void removeAllXs(Node* &pFirst, int x) {
  50. if(!pFirst) return;
  51. Node* cur = pFirst;
  52. while(cur->pNext != pFirst) {
  53. if(cur->pNext->data == x) {
  54. Node* tmp = cur->pNext->pNext;
  55. delete cur->pNext;
  56. cur->pNext = tmp;
  57. }
  58. else cur = cur->pNext;
  59. }
  60. if(cur->pNext->data == x) {
  61. Node* tmp = cur->pNext->pNext;
  62. if(cur->pNext == pFirst) pFirst = tmp;
  63. if(pFirst->pNext == pFirst)
  64. pFirst = nullptr;
  65. delete cur->pNext;
  66. cur->pNext = tmp;
  67. }
  68. }
  69.  
  70. //-----------------------------------------------------//
  71.  
  72. void deleteNext(Node* cur) {
  73. Node* tmp = cur->pNext;
  74. cur->pNext = tmp->pNext;
  75. cout << tmp->data << " ";
  76. delete tmp;
  77. }
  78.  
  79. void removeBasedOnK(Node* &pFirst, int k) {
  80. if(pFirst == nullptr) return;
  81. int n = 1;
  82. Node* mFirst = pFirst;
  83. while(pFirst->pNext != mFirst)
  84. {
  85. pFirst = pFirst->pNext;
  86. n++;
  87. }
  88. while(n > 0) {
  89. for(int t = 1; t <= (k+n-1) % n; t++)
  90. pFirst = pFirst->pNext;
  91. deleteNext(pFirst);
  92. n--;
  93. }
  94. pFirst = nullptr;
  95. }
  96.  
  97. //-----------------------------------------------------//
  98.  
  99. int main() {}
Success #stdin #stdout 0.01s 5376KB
stdin
Standard input is empty
stdout
Standard output is empty