将节点插入到链表的尾部

时间:2018-06-04 16:18:41

标签: c pointers linked-list nodes vertex

我是C的新学员,并且不能清楚地理解指针和其他方面,所以我试图用C中的链表实现VertexNode。但是,我可以&#39 ; t在我的代码中将节点插入List(VertexNode)时获得正确的行为。有些问题可能很简单,但我现在真的很困惑。

这是我的C结构:

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdbool.h>

typedef struct Vertex {
    int x;
    int y;
} Vertex;

typedef struct VertexNode {
    Vertex *v;
    struct VertexNode *next;
} VertexNode;

typedef struct VertexNode *List;

List insertLL(List, Vertex *n);
void showLL(List);

VertexNode *makeNode(Vertex *n) {
    VertexNode *new = malloc(sizeof(VertexNode));
    assert(new != NULL);
    new->v = n;
    new->next = NULL;
    return new;
}

List insertLL(List L, Vertex *n) {
    // add new node at the end
    VertexNode *new = makeNode(n);
    if (L){
        if (!L->next) L->next = new;
        if (L->next){
            VertexNode *cur = NULL;
            cur = L->next;
            while(cur){
                //L->next = cur;
                cur = cur->next;
            }
            cur = new;
        }
    }
    if(!L){
        L = new;
        L->next = NULL;
    }
    return L;
}

void showLL(List L) {
    if (L == NULL)
        putchar('\n');
    else {
        printf("%d,%d ", L->v->x,L->v->y);
        showLL(L->next);

    }
    }

int main(){

    Vertex *v1,*v2,*v3;

    v1=(Vertex*) malloc(sizeof(Vertex));
    assert(v1 != NULL);
    v2=(Vertex *) malloc(sizeof(Vertex));
    assert(v2 != NULL);
    v3=(Vertex*) malloc(sizeof(Vertex));
    assert(v3 != NULL);
    v1->x=0;
    v1->y=0;
    v2->x=1;
    v2->y=2;
    v3->x=7;
    v3->y=8;

    VertexNode *L = makeNode(v1);

    insertLL(L, v2);
    insertLL(L, v3);
    showLL(L);
}

现在的输出是

0,0 1,2 

我想得到正确的结果

0,0 1,2 7,8

1 个答案:

答案 0 :(得分:1)

There are too many issues with your code. For example, you do not use the returned list of the insert function in your main. Another issue is that the logic in your insert function seems faulty, take a paper and a pen, and draw (reading your code line by line) what happens. This always helps with lists and pointers. Moreover, I don't see why make the value of a node a pointer, while it could just be a normal struct (and not a pointer to it).

Here is my approach to it, which might give you a starting point:

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

typedef struct Vertex {
    int x;
    int y;
} Vertex;

typedef struct VertexNode {
    Vertex v;
    struct VertexNode *next;
} VertexNode;

typedef struct VertexNode *List;

VertexNode *makeNode(Vertex n) {
    VertexNode *new = malloc(sizeof(VertexNode));
    assert(new != NULL);
    new->v.x = n.x;
    new->v.y = n.y;
    new->next = NULL;
    return new;
}

void insertLL(List *ptraddr, Vertex n) {
                                                /* Insert v as last element of list *ptraddr */
    while (*ptraddr != NULL)                    /* Go to end of list */
        ptraddr = &((*ptraddr)->next);          /* Prepare what we need to change */
    *ptraddr = malloc(sizeof(VertexNode));      /* Space for new node */
    (*ptraddr)->v.x = n.x;                      /* Put value */
    (*ptraddr)->v.y = n.y;
    (*ptraddr)->next = NULL;                    /* There is no next element */
}

void showLL(List L) {                           /* Print elements of list */
    while (L != NULL) {                         /* Visit list elements up to the end */
        printf("(%d, %d)--> ", L->v.x,L->v.y);  /* Print current element */
        L = L->next;                            /* Go to next element */
    }
    printf("NULL\n");                           /* Print end of list */
}

/* TODO: Free the list! */

int main(void) {

    Vertex v1, v2, v3;
    v1.x=0; v1.y=0;
    v2.x=1; v2.y=2;
    v3.x=7; v3.y=8;

    VertexNode *L = makeNode(v1);

    insertLL(&L, v2);
    insertLL(&L, v3);
    showLL(L);

    return 0;
}

Output:

(0, 0)--> (1, 2)--> (7, 8)--> NULL