Koray Akpınar
Koray Akpınar

Reputation: 1

printf segmentation fault and wrong sorted linked list

This is my algorithm for adding nodes to a linked list which is in a sorted way for surnames of persons.

Here is the code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

 

struct Node {
    char Name[1024];
    char Surname[1024];
    char Telno[1024];
    struct Node* next;
};
 



void Add(struct Node** firstnode, struct Node* NewNode)
{
    struct Node* tempo;
    //printf("%d",strcmp((*firstnode)->Surname,NewNode->Surname));
    if (*firstnode == NULL || (strcmp((*firstnode)->Surname,NewNode->Surname) > 0)) {
        
        NewNode->next = *firstnode;
        *firstnode = NewNode;
    }
    else {
        
        tempo = *firstnode;
        while (tempo->next != NULL && strcmp(tempo->Surname,NewNode->Surname) < 0) {
            tempo = tempo->next;
        }
        NewNode->next = tempo->next;
        tempo->next = NewNode;
    }
}
 

 

struct Node* CreateNode(char name[1024], char surname[1024], char telno[1024])
{
    
    struct Node* NewNode = (struct Node*)malloc(sizeof(struct Node));
 
    
    strcpy(NewNode->Name,name);
    strcpy(NewNode->Surname,surname);
    strcpy(NewNode->Telno,telno);
    NewNode->next = NULL;
 
    return NewNode;
}
 

void Printlinkedlist(struct Node* head)
{
    struct Node* temp = head;
    while (temp != NULL) {
        printf("%s %s %s\n", temp->Name,temp->Surname,temp->Telno);
        temp = temp->next;
    }
}
 
struct Node* head = NULL;
struct Node* temp;

int main()
{
    
    int personcount;
    char name[1024],surname[1024],telno[1024];
    printf("Please give the count of person:");
    scanf("%d", &personcount);
    
    
    for (int i = 0; i < personcount; i++) {
        
        printf("Please give the name of %d. person:", i + 1);
        scanf(" %s", &name);
        
        printf("Please give the surname of %d. person:", i + 1);
        scanf(" %s", &surname);
        
        printf("Please give the phone number of %d. person:", i + 1);
        scanf(" %s", &telno);
        
        temp = CreateNode(name,surname,telno);
        
        Add(&head, temp);
    }
    
    
    printf("\n -------------- Linkedlist --------------\n");
    Printlinkedlist(head);


    return 0;
    
}

The first problem is this: For example, if I enter people's surnames as G, A, L, E, K (So first person's last name will be "G", second person's last name will be "A" etc..), it gives an incorrectly ordered output.

And the second one is: If I delete the comment line characters behind the printf inside the add function, I get a segmentation fault that I don't understand why

Thanks for the answer.

Upvotes: 0

Views: 43

Answers (1)

einpoklum
einpoklum

Reputation: 131626

It should first be said that you could, and should, have figured it out yourself by either:


But, more to the point, let's have a look at (some of) your code:

if (*firstnode == NULL || /* another condition */) {
    // do stuff
}
else {
    // so *firstnode may be NULL here 
    tempo = *firstnode;
    while (tempo->next != /* some value */ && /* another condition*/ ) {
        // do stuff
    }
    // do stuff
}

See the problem? tempo could get assigned a NULL point, and then de-referenced to get to the next field. That would likely cause a segmentation fault.

Upvotes: 1

Related Questions