Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 将节点插入到链表的尾部_C_Pointers_Linked List_Nodes_Vertex - Fatal编程技术网

C 将节点插入到链表的尾部

C 将节点插入到链表的尾部,c,pointers,linked-list,nodes,vertex,C,Pointers,Linked List,Nodes,Vertex,我是C语言的新手,不能清楚地理解指针和其他一些方面,因此我试图用C语言中的链表实现一个VertexNode。但是,在代码中将节点插入列表(VertexNode)时,我无法获得正确的行为。有些问题可能很简单,但我现在真的很困惑 这是我的C结构: #include <stdlib.h> #include <stdio.h> #include <assert.h> #include <string.h> #include <stdbool.h>

我是C语言的新手,不能清楚地理解指针和其他一些方面,因此我试图用C语言中的链表实现一个
VertexNode
。但是,在代码中将节点插入
列表(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 7,8

您的代码有太多问题。例如,在main中不使用insert函数的返回列表。另一个问题是插入函数中的逻辑似乎有问题,拿一张纸和一支笔,画出(逐行读取代码)发生了什么。这对列表和指针总是有帮助的。此外,我不明白为什么要将节点的值设为指针,而它可能只是一个普通结构(而不是指向它的指针)

以下是我的方法,可能会给你一个起点:

#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;
}
#包括
#包括
#包括
typedef结构顶点{
int x;
int-y;
}顶点;
typedef结构顶点节点{
顶点v;
结构VertexNode*下一步;
}顶点节点;
typedef结构VertexNode*列表;
VertexNode*makeNode(顶点n){
VertexNode*new=malloc(sizeof(VertexNode));
断言(新!=NULL);
新建->v.x=n.x;
新建->v.y=n.y;
新建->下一步=空;
归还新的;
}
void insertLL(列表*ptraddr,顶点n){
/*插入v作为列表*ptraddr的最后一个元素*/
while(*ptraddr!=NULL)/*转到列表末尾*/
ptraddr=&(*ptraddr)->下一步);/*准备我们需要更改的内容*/
*ptraddr=malloc(sizeof(VertexNode));/*新节点的空间*/
(*ptraddr)->v.x=n.x;/*Put值*/
(*ptraddr)->v.y=n.y;
(*ptraddr)->next=NULL;/*没有next元素*/
}
void showl(列表L){/*打印列表元素*/
while(L!=NULL){/*访问列表元素,直到结束*/
printf((%d,%d-->),L->v.x,L->v.y);/*打印当前元素*/
L=L->next;/*转到下一个元素*/
}
printf(“NULL\n”);/*打印列表末尾*/
}
/*TODO:释放列表*/
内部主(空){
顶点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;
肖威尔(L);
返回0;
}
输出:

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


您没有使用从
insertLL
函数返回的插入结果。再加上,
L=
对该函数的调用方来说没有任何意义,因为
L
是一个值参数(它是一个指针,但这与此无关)。这个站点上有数千个与此逻辑缺陷相关的副本,
cur=new逻辑也被破坏。铅笔+纸+盒子+箭头,再加上调试器,是更好地了解哪里出了问题的最佳方式。'In insertLL
if(L->next)
总是正确的。
#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;
}