#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int value;
struct node* next;
} node_t;
void push (node_t *root, int value) {
node_t* current = root;
if(current == NULL) {
current = (node_t*)malloc(sizeof(node_t));
current->value = value;
current->next = NULL;
}
else {
while(current->next != NULL)
current = current->next;
current = (node_t*)malloc(sizeof(node_t));
current->value = value;
current->next = NULL;
}
}
void print (node_t* root) {
node_t* current = root;
while(current != NULL) {
printf("%d ", current->value);
current = current->next;
}
}
//////////////////////////////////////////////////////////////////
int main(void) {
node_t* root = NULL;
push(root, 5);
push(root, 10);
push(root, 15);
print(root);
return 0;
}
Warum funktioniert dieser Code nicht?Verknüpfen von Listenknoten Erstellen
Ich versuche, eine verknüpfte Liste ohne Initialisierung in main
zu machen. Ist es möglich?
Ich mag die nicht initialisierten root
Knoten von main
nehmen und es in der Funktion zu initialisieren push()
so ist es der erste Knoten in verketteten Liste.
Ich will nicht, dies zu tun:
int main() {
node_t* root = (node_t*)malloc(sizeof(node_t));
root->value = 0;
root->next = NULL;
}
Debugger ................. –
Bitte lesen Sie http://stackoverflow.com/help/how-to-ask. Sag uns, was nicht funktioniert. –
können Sie Ihre ** Strukturdefinition ** zu Ihrem Code hinzufügen – Cherubim