Lista jednokierunkowa usuwanie elementów - błąd ze znalezieniem elementu

0

Mam kod implementujący listę jednokierunkową:

#include <cstdio>
#include <ctime>

struct Data
{
    char imie[13];
    char nazwisko[13];
};

struct Node
{
    Node* next;
    int index;
    Data data;

} *root = NULL;


Node* findBefore(int index)
{
    if (root != NULL && root->index == index)
        return root;
    else
    {
        Node* tmp = root;
        while (tmp != NULL && tmp->next != NULL)
        {
            if (tmp->next->index == index)
                return tmp;
            tmp = tmp->next;
        }
    }

    return NULL;
}


Node* find(int index)
{
    Node* tmp = root;
    while (tmp != NULL)
    {
        if (tmp->index == index)
            return tmp;
        tmp = tmp->next;
    }

    return NULL;
}


bool add(int index, Data data)
{
    Node* tmp = root;
    root = new Node;
    root->next = tmp;
    root->data = data;
    root->index = index;

    return true;
}

void remove(Node* node)
{

    printf("bla\n");
    Node* tmp;
    if (node == root)
    {
        printf("bla_root\n");
        tmp = root;
        root = tmp->next;
        tmp = NULL;
        printf("bla_root 2\n");

        return;
    }
    printf("bla\n");
    tmp = node->next;
    printf("Adres: %p\n",node->next->next);
    if(node->next->next != NULL)
    {
        printf("bla-if\n");
        node->next = node->next->next;
    }
    else
    {
        printf("bla_else\n");
        delete node->next;
    }


    return;
}

void printList(Node* node)
{
    while (node != NULL)
    {
        printf("%d %s %s\n", node->index, node->data.imie, node->data.nazwisko);
        node = node->next;
    }
}

int main()
{
    int n;
    char c;
    Data dane;
    unsigned long time = 0;

    while(1)
    {
        scanf("%c",&c);
        switch (c)
        {
        case 'a': //if(c=='a')
        {
            scanf("%d %s %s", &n, dane.imie, dane.nazwisko);
            if(add(n,dane))
                printf("Added.\n");
            else
                printf("Not added - Already here.\n");
        }
        break;
        case 'f': //else if(c=='f')
        {
            scanf("%d", &n);
            Node *f = find(n);
            if (f) printf("Indeks: %d Imie: %s Nazwisko %s\n", f->index, f->data.imie, f->data.nazwisko);
            else printf("Nie znaleziono kurwa.\n");
        }
        break;
        case 'd': //else if(c=='r')
        {
            scanf("%d", &n);
            Node *f = findBefore(n);

            if(f)
            {
                printf("usuwa\n");
                remove(f);
                printf("usuniete\n");

            }
            else printf("Nie znaleziono kurwa(usun).\n");
        }
        break;
        case 's':
            time = clock();
            break;
        case 't':
            printf("%lu \n", clock() - time);
            break;
        case 'p': //else
        {

            printList(root);
        }
        }
    }
}

Od 8h szukam błędu, ale nie wiem co jest. Mam plik testowy o zawartości:

s 
a 3972882 Jan Ławrynowicz
a 2054823 Bożydar Ławrynowicz
a 1040048 Adam Morozowski
a 2795993 Arek Morozowski
a 3794728 Jan Żeromski
a 8127434 Bożydar Winotuski
a 4839279 Jan Zięba
a 5420307 Ola Żyła
a 8643569 Arek Kożydrowski
a 9731486 Adam Żyła
t 
s 
f 8127434 
f 8643569 
f 3794728 
f 5420307 
f 9731486 
f 2795993 
f 2054823 
f 4839279 
f 3972882 
f 1040048 
t 
s 
d 2054823 
d 2795993 
d 3794728 
d 8643569 
d 5420307 
d 3972882 
d 9731486 
d 8127434 
d 4839279 
d 1040048 
t

I w którymś momencie (nie wiem w którym) wywala, że nie znalazło danego elementu. Gdzie jest błąd?

0

Ogranicz wczytywanie znaków aby nie można było przpełnić bufora wpisując większą liczbę znaków:

scanf("%d %12s %12s", &n, dane.imie, dane.nazwisko);

1 użytkowników online, w tym zalogowanych: 0, gości: 1