2016-06-03 14 views
-4
#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; 
} 
+0

Debugger ................. –

+1

Bitte lesen Sie http://stackoverflow.com/help/how-to-ask. Sag uns, was nicht funktioniert. –

+0

können Sie Ihre ** Strukturdefinition ** zu Ihrem Code hinzufügen – Cherubim

Antwort

4
void push (node_t *root, int value) 

Der root Knoten wird nie an den Aufrufer zurückgegeben. Sie müssen entweder den Knoten node_t* für den Knoten root als Zeiger akzeptieren, also node_t**, um das Stammverzeichnis an seinem ursprünglichen Speicherort zuordnen zu können, oder den neu zugewiesenen Knoten current zurückgeben.

node_t * push (node_t *root, int value) 

Dann;

node_t * push (node_t *root, int value) { 
    node_t* current = root; 
    /*...*/ 
    return current; 
} 

Gegeben der Aktualisierungscode; das obige ist immer noch anwendbar, aber Sie Demo-Beispiel im Haupt würde Änderung benötigen. Die node_t** Option wäre besser.

Ihre push() Funktion hat zwei Probleme; Akzeptieren des node_t** und Festlegen des root Knotens beim Erstellen; und den neuen Knoten korrekt einzufügen, wenn die root bereits erstellt ist;

// accept the root as node_t** 
void push (node_t **root, int value) { 
    node_t* current = *root; // get the root node 

    if (current == NULL) { 
     current = (node_t*)malloc(sizeof(node_t)); 
     current->value = value; 
     current->next = NULL; 
     *root = current; // set the root node 
    } 
    else { 
     while (current->next != NULL) { 
      current = current->next; 
     } 

     // create the new node in the place of the "next" node 
     current->next = (node_t*)malloc(sizeof(node_t)); 
     current->next->value = value; 
     current->next->next = NULL; 
    } 
} 

Eine vollständige Auflistung;

#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; 
     *root = current; 
    } 
    else { 
     while (current->next != NULL) { 
      current = current->next; 
     } 

     current->next = (node_t*)malloc(sizeof(node_t)); 
     current->next->value = value; 
     current->next->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); // note the additional & 
    push(&root, 10); 
    push(&root, 15); 
    print(root); 

    return 0; 
} 

Hinweis; Dies wurde kompiliert mit -std=c11 Sprachniveau Kompatibilität.

+0

oder besser machen root eine 'node_t * root' eine globale Variable – Cherubim

+0

Doppelzeiger sind manchmal für mich überwältigend, danke – noissue