I wrote this function that creates new nodes.
When I add only one node, the program works but if I add the second node I get a segmentation fault, so clearly the problem lies in the "else" part of the function "add_node()" but I can't figure it out.
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
char *city;
struct node *next;
}node;
node *start = NULL;
node *current;
void add_node(char *cityname) {
node *y = malloc(sizeof(node));
y->city = cityname;
y->next = NULL;
current = start;
if(start == NULL) {
start = y;
} else {
while (current != NULL) {
current = current->next;
}
current->next = y;
}
}
int main() {
add_node("Paris");
add_node("London");
current = start;
while(current != NULL) {
printf("%s\n", current->city);
current = current->next;
}
}