W sumie mogłeś dać taką ilość kodu, by można było sobie potestować tę twoją funkcję.
Ale jest AI i można sobie wygenerować to, czego nie ma ;)
Nie chciało mi się przerabiać twojej funkcji, napisałem własną:
#include <iostream>
struct Node {
int dane;
Node* nast;
};
Node* createLinkedList(int arr[], int n) {
if (n == 0) {
return nullptr;
}
Node* head = new Node{arr[0], nullptr};
Node* curr = head;
for (int i = 1; i < n; i++) {
curr->nast = new Node{arr[i], nullptr};
curr = curr->nast;
}
return head;
}
void destroyLinkedList(Node* head) {
while (head != nullptr) {
Node* next = head->nast;
delete head;
head = next;
}
}
void printLinkedList(Node* head) {
while (head != nullptr) {
std::cout << head->dane << " ";
head = head->nast;
}
std::cout << std::endl;
}
Node* usun(Node* pocz, int K)
{
Node* n0 = nullptr;
Node* n1 = pocz;
while (n1) {
Node* n3 = n1;
Node* n2 = nullptr;
int c = 0;
while(n3 && n3->dane % K == 0) {
n2 = n3;
n3 = n3->nast;
++c;
}
if(c == 0) {
n2 = n3;
n3 = n3->nast;
}
if(c > 1) {
Node* tmp = n3 ? n3->nast : n3;
if(n0) n0->nast = tmp;
if (n1 == pocz) pocz = tmp;
while (c--) {
n3 = n1->nast;
delete n1;
n1 = n3;
}
n1 = tmp;
} else {
n0 = n2;
n1 = n3;
}
}
return pocz;
}
int main(void)
{
//int tab[] = { 10, 10, 10, 9, 9, 9, 8, 8, 6, 5, 4, 3, 2, 2, 1, 1 }; int K = 2; // -> 9 9 4 3 1
//int tab[] = { 6, 6, 6, 3, 2, 1, 1 }; int K = 3; // -> 1 1
int tab[] = { 10, 10, 9, 8, 8, 8, 7, 6, 6, 5, 4, 4 }; int K = 2; // -> pusty
Node* list = createLinkedList(tab, sizeof(tab) / sizeof(int));
list = usun(list, K);
printLinkedList(list);
destroyLinkedList(list);
}