Portál AbcLinuxu, 27. října 2025 22:20
struct cllnode {
char name[30];
struct cllnode *next, *prev;
};
//The circular linked list tail pointer.
struct cllnode *tail = NULL;
void addNode(struct cllnode *newnode) {
if (tail == NULL) {
tail = newnode;
newnode->next = tail;
newnode->prev = tail;
} else {
newnode->next = tail->next;
newnode->prev = tail;
tail->prev = newnode;
tail->next = newnode;
tail = newnode;
}
}
int main() {
int value = 0;
char student[30];
//node pro traverzovani
struct cllnode *current;
printf("Zadej jmena studentu:\n");
do{
struct cllnode *newnode;
newnode = (struct cllnode *)malloc(sizeof(struct cllnode));
scanf("%s", student);
strcpy(newnode->name, student);
addNode(newnode);
value++;
}while(strcmp(student, "NA") != 0);
//print
current = tail->next;
do {
printf("%s\n", current->name);
current = current->next;
} while (current != tail->next);
//print reverse
current = tail->prev;
do {
printf("%s\n", current->name);
current = current->prev;
} while (current != tail->prev);
printf("Celkovy pocet studentu: %d\n", value - 1);
//Clean up current.
current = NULL;
return 0;
}
Predem diky.
Řešení dotazu:
Nechce se mi to zkoumat moc podrobně, takže tam možná bude víc chyb, ale hlavní problém je asi tady:
--- list.c.orig 2012-04-09 21:44:55.485782284 +0200
+++ list.c 2012-04-09 21:45:32.457339644 +0200
@@ -16,7 +16,7 @@ if (tail == NULL) {
newnode->next = tail->next;
newnode->prev = tail;
- tail->prev = newnode;
+ tail->next->prev = newnode;
tail->next = newnode;
tail = newnode;
(Pozor na Wikipedii a méně používané knihovny - spousta jich to má skoro dobře, až na pár speciálních případů.)
Plus pár otázek "mně to taky nechodí".
A wikipedii, ale to jsi určitě viděl taky.
include/linux/list.h.
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.