Слишком много проблем с вашим кодом.Например, вы не используете возвращенный список функции вставки в вашем main.Другая проблема заключается в том, что логика в вашей функции вставки кажется неверной, возьмите бумагу и ручку и нарисуйте (читая ваш код строка за строкой), что происходит.Это всегда помогает со списками и указателями.Более того, я не понимаю, зачем делать значение узла указателем, хотя это может быть просто нормальная структура (а не указатель на нее).
Вот мой подход к нему, который может датьВы отправной точкой:
#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;
}
Выход:
(0, 0) -> (1, 2) -> (7, 8) -> NULL