My problem is that the cards are not being deleted correctly, like this:
1 Front: t | Back: t
2 Front: test | Back: test
When I delete the second card, it works fine.
But when I delete the first card, there’s an issue.
After deleting the first card, the output looks like this:
1 Front: □□=P□ | Back: □□=P□
2 Front: test | Back: test
It prints these strange characters instead of displaying:
1 Front: test | Back: test
as it should.
this is my code:
funktion.c
void delete_deck(struct Node** decks, int deck_num) {
struct Node* previous = NULL;
struct Node* current = *decks;
int index = 1;
while (current != NULL && index < deck_num) {
previous = current;
current = list_get_next(current);
index++;
}
if (current != NULL) {
// Deck gefunden
Deck* deck_to_delete = (Deck*)list_get_data(current);
struct Node* current_card = deck_to_delete->cards;
while (current_card != NULL) {
struct Node* next_card = list_get_next(current_card);
Entry* card_data = (Entry*)list_get_data(current_card);
free(card_data->front);
free(card_data->back);
free(card_data);
free(current_card);
current_card = next_card;
}
// Deck löschen
free(deck_to_delete->name);
free(deck_to_delete);
if (previous == NULL) {
*decks = list_get_next(current);
} else {
list_set_next(previous, list_get_next(current));
}
free(current);
my_printf("Deck erfolgreich gelöscht.\n");
current = *decks;
index = 1;
} else {
my_printf("Ungültige Decknummer.\n");
}
}
void delete_card(Deck* deck, int card_num) {
if (deck == NULL || deck->cards == NULL) {
my_printf("Kein Deck oder keine Karten zum Löschen vorhanden.\n");
return;
}
struct Node* current = list_get_first(deck->cards);
if (current == NULL) {
my_printf("Die Kartenliste ist leer.\n");
return;
}
struct Node* previous = NULL;
int index = 1;
while (current != NULL && index < card_num) {
previous = current;
current = list_get_next(current);
index++;
}
if (current == NULL) {
my_printf("Karte mit der Nummer %d existiert nicht.\n", card_num);
return;
}
Entry* card = (Entry*)list_get_data(current);
// Erste Karte
if (previous == NULL) {
deck->cards = list_get_next(current);
} else {
list_set_next(previous, list_get_next(current));
}
// Karte freigeben
free(card->front);
free(card->back);
free(card);
free(current);
my_printf("Karte an Index %d wurde gelöscht.\n", card_num);
}
[node.c]
typedef struct Entry {
char* front;
char* back;
Entry;
typedef struct Deck {
char* name; // Name des Decks
struct Node* cards; // Zeiger auf die Karten-Liste
Deck;
struct Node {
void* data;
struct Node* next; // Nächster Node
struct Node* prev; // Vorheriger Node
};
I’ve already debugged the issue but, unfortunately, I haven’t been able to find a solution.
and i have created a test
main_test.c:102:test_remove_card:FAIL: Expected ‘Karte 2 – Vorderseite’ Was NULL
You need to sign in to view this answers
Leave feedback about this