M
Hallo prolahmierer,
ich habe mir mal deinen Code angesehen. Dein Fehler war schon beim
Aufbauen der Liste. Die Liste bestand immer nur aus max 2 Elementen.
Ich habe diese Funktion mal berichtigt.
Dann gab es noch ein Problem bei deinem Abzählen. Dies habe ich auch
mal gedebuggt und dir hier meine Lösung gepostet. Wie du siehst, habe
ich ein paar printf's hinzugefügt, damit man sieht, was passiert.
Dies hilft bei der Fehlersuche. Vielleicht lernst du ja so noch etwas.
#include <stdio.h>
#include <stdlib.h>
struct node {
struct node *next;
char* data;
};
typedef struct node* nodeptr;
void print_nodelist(nodeptr hd) {
nodeptr first = hd;
while (hd->next != first) {
printf("data: %s\n", hd->data);
hd = hd->next;
}
printf("data: %s\n", hd->data);
}
nodeptr zaehleAb(nodeptr hd, int silbanz) {
int i;
printf("\n\nAnzahl von silben: %d\n", silbanz);
while(hd->next != hd){
nodeptr bleibt;
for(i=0; i<silbanz-1; ++i) {
bleibt = hd;
printf("bleibt: %s\n", bleibt->data);
hd = hd->next;
}
bleibt->next = bleibt->next->next;
printf("geht: %s\n", hd->data);
free(hd);
hd = bleibt->next;
}
return hd;
}
nodeptr add(nodeptr l, char* data) {
printf("add: %s\n", data);
/* Wenn Liste leer */
if (!l) {
l = malloc(sizeof(*l));
l->data = data;
l->next = l;
} else {
/* Ansonsten n vor l haengen*/
nodeptr n, t;
n = malloc(sizeof(*n));
n->next = l;
n->data = data;
t = l;
while (t->next != l) t = t->next;
t->next = n;
}
return l;
}
int main(int argc, char*argv[]) {
int i;
int silbanz = atoi(argv[1]);
nodeptr hd = NULL;
printf("silbanz: %d\n", silbanz);
for (i=2; i<argc; ++i) hd=add(hd, argv[i]);
print_nodelist(hd);
/* Personen abzaehlen */
hd = zaehleAb(hd, silbanz);
print_nodelist(hd);
/* Zeige die letzte Person */
printf("\nAnd the winner is: %s\n\n", hd->data);
free(hd);
return 0;
}
Gruß mcr