Thread: linked list alfabetical sorting

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    4

    linked list alfabetical sorting

    I try to sort my name inputs alfabetically. Code works, but they sort in wrong order. Where do i make mistake?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    struct person{
    		
    	char *name;
    	char *surname;
    	long int phonenumber;
    };
    
    
    struct node {
    	struct person *person;
    	struct node *next;
    	
    };
    
    
    struct node* start=NULL;
    struct node* temp;
    struct node* temp2;
    struct node* iter;
    struct node* iter2;
    struct node* min;
    struct node* previter;
    
    
    void addrecord(struct person *new_person) {
    	
    	struct node *node = malloc(sizeof(struct node));
    	node->person = new_person;
        node->next = NULL;
        
        if (start == NULL) {
             
            start = node;
        
    	}
    
    
    	else {
            
            temp = start;
            
    		while(temp->next != NULL){
            
    			temp=temp->next;
            	
    		}
    		
    		temp->next=node;
            /*tail->next = node;
            tail = node;*/
        }
    }
    
    
    void printPhoneBook() {
    	
    	char s1 [50];
    	char s2 [50];
    	
        if(start == NULL) {
            printf("\n  Phonebook is empty!\n");
            
        }
    	
    	else if(start->next==NULL) {
    		printf("\n  %s  %s  %d",start->person->name,start->person->surname,start->person->phonenumber);
    		
    	}
    	
    	else {
    		iter = start;
    		iter2 = start->next;
    		
    		for(iter=start; iter!=NULL; iter=iter->next) {
    			min=iter;
    			for(iter2=iter->next; iter2!=NULL; iter2=iter2->next) {
    				strcpy(s1,min->person->name);
    				strcpy(s2,iter2->person->name);
    				if(s2 < s1) {
    					min=iter2;
    				
    					if(iter==start){
    						
    						temp=min->next;
    						min->next=iter;
    						iter->next=temp;
    						start=min;
    						min=iter2;
    					}
    					
    					else if(iter!=NULL && min!=NULL) {
    					
    						previter=start;
    						while(previter->next!=iter) {
    							previter=previter->next;
    						}
    						
    						temp=min->next;
    						min->next=iter;
    						iter->next=temp;
    						previter->next=min;
    					}
    					
    					else if(min!=NULL) {
    						
    						previter=start;
    						while(previter->next!=iter) {
    							previter=previter->next;
    						}
    						
    						temp=NULL;
    						min->next=iter;
    						iter->next=temp;
    						previter->next=min;
    						
    					}
    						
    				}
    				
    			}
    		}
    		
    	
    	iter = start;
    		
    	while(iter != NULL) {
            printf("%s  %s  %d ", iter->person->name, 
                                  iter->person->surname, 
                                  iter->person->phonenumber);
            iter = iter->next;
            if(iter != NULL)
            printf("\n");
            }
            printf("\n");
    	}
    }
        
    int main() {
    	
    	int code;
    	int number;
    
    
    
    
    	
    	while (code != 5) {
    	
    	printf("\n\tPHONEBOOK\n\n");
    	printf("  [1]  Add a contact \n  [2]  Delete a contact \n  [3]  Update a contact \n  [4]  Print phonebook \n  [5]  Exit \n");
    	printf("  Enter your choice: ");
    	scanf("%d",&code);
    	
    	switch(code){
    		case 1:
    			;
    			struct person *newnode = malloc(sizeof(struct person));
        		newnode->name = malloc(100);
        		newnode->surname = malloc(100);
        
       			printf("  Enter the name: ");
        		scanf("%s",newnode->name);
        
        		printf("  Enter the surname: ");
        		scanf("%s",newnode->surname);
        
        		printf("  Enter the phone number: ");
        		scanf("%d",&newnode->phonenumber);
    			
    			addrecord(newnode);
    		break;
    		
    		case 2:
    			printPhoneBook();
                    break;
    }
    }
    }

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    If you need a sorted dynamic "list" use a binary tree...

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    62
    Line 84. This is not how you compare strings. You're comparing the address of s1 to the address of s2. You'll want to use strcmp, instead.

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    4
    Quote Originally Posted by gaxio View Post
    Line 84. This is not how you compare strings. You're comparing the address of s1 to the address of s2. You'll want to use strcmp, instead.
    how should I do that line?

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    You have not checked to see if any malloc calls have succeeded before using them, or freeing them before the programme ends.

    For strcmp()...
    C library function - strcmp()

    If you want to make the list alphabetical, the easiest way is to make an "insert" function. Go through your list each time you add a node, stopping at the correct spot and insert the new node

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting a linked list
    By Anass in forum C Programming
    Replies: 7
    Last Post: 10-27-2010, 06:56 AM
  2. Help Sorting A Linked List
    By Sherina in forum C++ Programming
    Replies: 3
    Last Post: 12-12-2009, 05:46 AM
  3. Sorting a Linked List
    By cannsyl in forum C++ Programming
    Replies: 1
    Last Post: 12-03-2008, 07:20 AM
  4. sorting a linked list
    By sillyman in forum C Programming
    Replies: 4
    Last Post: 04-30-2008, 09:39 AM
  5. sorting linked list
    By bazzano in forum C Programming
    Replies: 1
    Last Post: 05-20-2006, 05:33 AM

Tags for this Thread