2016-03-30 9 views
0

Ich habe mir online viele verschiedene Fragen angeschaut und kann nicht herausfinden, was ich falsch mache. Ich könnte jetzt in die falsche Richtung gehen, weil ich so viele verschiedene Dinge ausprobiert habe.C Simple LinkedList

Ich versuche nur eine einfache single LinkedList in C zu erstellen. Ich kann nicht herausfinden, wie man die Liste in Verbindung bringt.

:

Die Struktur für mein Knoten

typedef struct node 
{ 
    double x;   // x-coordinate of this point in the tour 
    double y;   // y-coordinate of this point in the tour 
    struct node* next; // Pointer to the next node in the linked list 
} Node; 

Das ist mein Code ist die Liste zu machen, ich in Haupt

Node* addFront(Node* first, double x, double y) { 

    first = malloc(sizeof(Node)); 
    if (first == NULL) { 
     first->x = x; 
     first->y = y; 
     first->next = NULL; 
    } 
    else { 
     Node * temp = malloc(sizeof(Node)); 
     temp->x = x;   
     temp->y = y;      
     temp->next = first;     
     first = temp;  
    } 
    //Temp testing 
    int size = 0; 
    Node * current = first; 
    while (current->next != NULL) { 
     printf("(%.4f, %.4f)\n", current->x, current->y); 
     current = current -> next; 
     size++; 
    } 
    printf("Size: %d\n", size); 

    return first; 
} 

Einige Notizen einen leeren Knoten zuerst = NULL konstruieren Zu überprüfen, ob der erste Wert null ist, sollte nicht notwendig sein ... Die Liste sollte nur mit der else-Anweisung erstellt werden können. (Was ich denke)

Nach dem Hinzufügen der If/Else-Anweisung bekomme ich, was scheint eine Endlosschleife mit C nur im Zufallsspeicher zeigt, die schließlich zu Segmentierungsfehler führt.

Ich weiß einfach nicht wo ich mich sonst noch orientieren kann. Vielen Dank im fortgeschrittenen Stadium!

+1

Der Block' if (erste == NULL) {...} 'doesn t Sinn machen. Wenn 'first'' NULL' ist, können Sie 'first-> x = x;' usw. nicht tun. Es bedeutet nur, dass 'malloc' zuvor fehlgeschlagen ist, um Speicher zuzuordnen. – pzaenger

Antwort

4

Dieser Block ist nicht sinnvoll überhaupt:

first = malloc(sizeof(Node)); 
if (first == NULL) { 
    first->x = x; 
    first->y = y; 
    first->next = NULL; 
} 

Wahrscheinlich Sie die first = malloc(sizeof(Node)); innerhalb des Blocks bewegen wollte. Es würde funktionieren, aber es ist völlig unnötig, weil es logisch gleich dem else Block wäre. So kann man sich nur den zweiten Block verlassen:

Node * temp = malloc(sizeof(Node)); 
    temp->x = x;   
    temp->y = y;      
    temp->next = first;     
    first = temp; 
    return first; 
    // or rather return temp directly 

Es gibt einen weiteren Punkt - Sie sollen Fehler hinzufügen, bei der Handhabung malloc läuft aus dem Speicher, so dass Sie für temp == NULL überprüfen sollen, und entsprechend handeln (Rückkehr NULL von Funktion oder Wasauchimmer...).

+0

Vielen Dank. Ich denke, ich habe es ohne die if-Anweisung richtig gemacht, ich muss einfach die Waffe gesprengt haben und habe es nicht richtig getestet. –

0

Für den Anfang ist sogar die erste Anweisung der Funktion falsch, weil der Wert des Parameters first überschrieben wird.

Node* addFront(Node* first, double x, double y) { 

    first = malloc(sizeof(Node)); 
    //... 

Die Funktion der folgenden Art und Weise ‚aussehen kann

Node * addFront(Node *first, double x, double y) 
{ 
    Node *temp = malloc(sizeof(Node)); 

    if (temp != NULL) 
    { 
     temp->x = x;   
     temp->y = y;      
     temp->next = first;     

     first = temp; 
    } 

    return first; 
} 

Oder mit einem Testcode

Node * addFront(Node *first, double x, double y) 
{ 
    Node *temp = malloc(sizeof(Node)); 

    if (temp != NULL) 
    { 
     temp->x = x;   
     temp->y = y;      
     temp->next = first;     

     first = temp; 
    } 

    // start pf inline test 
    size_t size = 0; 

    for (Node *current = first; current != NULL; current = current->next) 
    { 
     printf("(%.4f, %.4f)\n", current->x, current->y); 
     ++size; 
    } 
    printf("Size: %zu\n", size); 
    // end pf inline test 

    return first; 
}