I have a linked list and I am attempting to insert a new node, which seems to be successful at inserting the node where I want it to go, but the variables keep coming out as NULL. Could someone point to where I am causing this to happen?
Here is the print and insert method.
void printList(node *head)
{
node *p;
p = head;
if(p->next == NULL)
printf("No stops currently in the tour.");
else
{
while(p->next != NULL)
{
printf("Tour Stop: %s - Description: %s\n", p->name, p->name);
p = p->next;
}
}
}
void insertInOrder(node *head, node *newNode)
{
printf("What is the stop you want the new stop to come after? Type 'end' to insert at the end of the tour.");
char key[100];
scanf("%s", &key);
getchar();
node *p;
p = head->next;
if(head->next == NULL)
head->next = newNode;
else if(key == "end")
{
while(p->next != NULL)
p = p->next;
p->next = newNode;
printf("\nAT 57, newNode->info = %s and newNode->name = %s", newNode->info, newNode->name);
}
else
{
while(strcmp(p->name, key) != 0)
{
if(p->next == NULL)
{
printf("Couldn't find the tour stop you requested, inserting at end of tour.");
break;
}
p = p->next;
}
p->next = newNode;
}
And here is the createNewNode method I am using to pass into the insert method
node* createNewNode()
{
node *newNode;
newNode = malloc(sizeof(struct node));
printf("Enter the name of the new tour stop.\n");
char newName[100];
fgets(newName, sizeof(newName), stdin);
newNode->name = newName;
printf("Enter information about the tour stop. Max number of characters you can enter is 1000.\n");
char newDescription[1000];
newNode->info = newDescription;
fgets(newDescription, sizeof(newDescription), stdin);
return newNode;
}