#include <iostream>
using namespace std;
struct Node{
int data;
Node* pNext;
};
void remove1stX(Node* &pFirst, int x);
void removeAllXs(Node* &pFirst, int x);
void removeBasedOnK(Node* &pHead, int k);
//-----------------------------------------------------//
void remove1stX(Node* &pFirst, int x) {
if(pFirst == nullptr) return;
if(pFirst->data == x) {
Node* pLast = pFirst;
while(pLast->pNext != pFirst)
pLast = pLast->pNext;
if(pLast == pFirst) {
delete pFirst;
pFirst = nullptr;
return;
}
pLast->pNext = pFirst->pNext;
delete pFirst;
pFirst = pLast->pNext;
return;
}
Node* cur = pFirst;
while(cur->pNext->data != x &&
cur->pNext->pNext != pFirst->pNext) {
cur = cur->pNext;
}
if(cur->pNext->data == x) {
Node* tmp = cur->pNext;
cur->pNext = cur->pNext->pNext;
delete tmp;
}
}
//-----------------------------------------------------//
void removeAllXs(Node* &pFirst, int x) {
if(!pFirst) return;
Node* cur = pFirst;
while(cur->pNext != pFirst) {
if(cur->pNext->data == x) {
Node* tmp = cur->pNext->pNext;
delete cur->pNext;
cur->pNext = tmp;
}
else cur = cur->pNext;
}
if(cur->pNext->data == x) {
Node* tmp = cur->pNext->pNext;
if(cur->pNext == pFirst) pFirst = tmp;
if(pFirst->pNext == pFirst)
pFirst = nullptr;
delete cur->pNext;
cur->pNext = tmp;
}
}
//-----------------------------------------------------//
void deleteNext(Node* cur) {
Node* tmp = cur->pNext;
cur->pNext = tmp->pNext;
cout << tmp->data << " ";
delete tmp;
}
void removeBasedOnK(Node* &pFirst, int k) {
if(pFirst == nullptr) return;
int n = 1;
Node* mFirst = pFirst;
while(pFirst->pNext != mFirst)
{
pFirst = pFirst->pNext;
n++;
}
while(n > 0) {
for(int t = 1; t <= (k+n-1) % n; t++)
pFirst = pFirst->pNext;
deleteNext(pFirst);
n--;
}
pFirst = nullptr;
}
//-----------------------------------------------------//
int main() {}