Reputation: 15
I am a new learner to C, and can't clearly understand pointers and some other aspects, so I am trying to implement a VertexNode
with a linked list in C. However, I can't get the proper behavior when inserting a node into the List(VertexNode)
in my code. Some of the problems might be simple, but I'm really confused at the moment.
Here is my C structure:
#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);
}
The output now is
0,0 1,2
I want to get the correct result which is
0,0 1,2 7,8
Upvotes: 1
Views: 569
Reputation: 73394
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
Upvotes: 1