I have problem with this section of my code for, some reason everything seems to work fine but when I print out the whole linked list it only prints the header and next node if it went through "if (compare_node(list, node) == -1)" function. I have written that rest = list, but operations I do on rest doesn't seem to affect the list variable. I need help on how to link the output of rest variables to list variable which is the header of my code.
link *add_node(link *list, link *node) { // list = header, node = new node I want to insert alphabetically
link* temp;
link* rest;
rest = list;
if (compare_node(list, node) == -1){
temp = list;
list = node;
list->next = temp;
printf("%s->%s->%s...\n",list->node_str, list->next->node_str, list->next->next->node_str);
printf("nodes interchanged\n");
}
else{
while(rest != NULL){
if(compare_node(rest, node) == -1){
printf("initial rest value: %s\n", rest->node_str);
temp = rest;
rest = node;
rest->next = temp;
printf("nodes interchanged, new rest value: %s\n", rest->node_str); //nodes interchanged: name
printf("%s->%s->%s...\n",list->node_str, list->next->node_str, list->next->next->node_str);
return list;
}
rest = rest->next;
printf("next rest value: %s\n", rest->node_str); //next rest value: name
}
rest = node;
printf("new rest value %s\n", rest->node_str);
}
return list;
}
my output looks like this:
Enter the user name for user N1: mike
Enter the user name for user N2: ann
ann->mike->(null)...
nodes interchanged
Enter the user name for user N3: luka
next rest value: mike
initial rest value: mike
nodes interchanged, new rest value: luka
ann->mike->(null)...
Enter the user name for user N4: nick
next rest value: mike
next rest value: (null)
new rest value nick
Enter the user name for user N5:
1. ann
2. mike
My whole Code looks like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STR_LEN 80
//STRUCT
typedef struct link_node
{
char node_str[MAX_STR_LEN];
struct link_node *next;
} link;
void Node_Create(link *Node, char* userName, link *nextNode);
int compare_node(link *n1, link *n2);
link *add_node(link *list, link *node);
void display_list(link *head);
//MAIN
int main() {
int num, i = 1;
char name[MAX_STR_LEN];
link *Curr, *Head;
printf("Enter the user name for user N%d: ", i);
gets(name);
Head = (link*)malloc(sizeof(link));
Node_Create(Head, name, NULL);
i++;
while(1){
printf("Enter the user name for user N%d: ", i);
gets(name);
if (strcmp(name, "\0") == 0) break;
Curr = (link*)malloc(sizeof(link));
Node_Create(Curr, name, NULL);
Head = add_node(Head, Curr);
i++;
}
display_list(Head);
printf("\n%s\n", Head->node_str);
return 0;
}
//FUNCTION FOR CREATING NODES
void Node_Create(link *Node, char* userName, link *nextNode) {
strcpy(Node->node_str, userName);
Node->next = nextNode;
}
//FUNCTION FOR COMPARING NODES
int compare_node(link *n1, link *n2) {
link* Temp;
if(n1->node_str[0] > n2->node_str[0])
return -1;
else if(n1->node_str[0] == n2->node_str[0])
return 0;
else
return 1;
}
//FUNCTION FOR ADDING NODES
link *add_node(link *list, link *node) { // list = header, node = new node I want to insert alphabetically
link* temp;
link* rest;
rest = list;
if (compare_node(list, node) == -1){
temp = list;
list = node;
list->next = temp;
printf("%s->%s->%s...\n",list->node_str, list->next->node_str, list->next->next->node_str);
printf("nodes interchanged\n");
}
else{
while(rest != NULL){
if(compare_node(rest, node) == -1){
printf("initial rest value: %s\n", rest->node_str);
temp = rest;
rest = node;
rest->next = temp;
printf("nodes interchanged, new rest value: %s\n", rest->node_str); //nodes interchanged: name
printf("%s->%s->%s...\n",list->node_str, list->next->node_str, list->next->next->node_str);
return list;
}
rest = rest->next;
printf("next rest value: %s\n", rest->node_str); //next rest value: name
}
rest = node;
printf("new rest value %s\n", rest->node_str);
}
return list;
}
//FUNCTION FOR DISPLAYING NODES
void display_list(link *head) {
link *rest = head;
int i = 1;
while(rest != NULL){
printf("%d. %s\n", i++, rest->node_str);
rest = rest->next;
}
}
Thanks in advance :)