Thread: Sorting linked list alphabetically

  1. #1
    Registered User
    Join Date
    Nov 2021
    Posts
    1

    Question Sorting linked list alphabetically

    Hi, I am currently doing program where it is linked list and the user will need to input the name in the list. What I am stuck with is that I can't arrange those names alphabetically:confused: I tried arranging it but it only arrange the letters of the first element. For example, the elements are 'mitch, jane, kay, ron' the output will be 'chimt, jane, kay, ron'. But what output I am aiming to is 'jane, kay, mitch, ron'

    Can you guys help me check how can this one be fixed? Below is my code:

    Code:
    #include<stdio.h>#include<stdlib.h>
    #include<string.h>
    
    
    typedef struct listNode {
    	char data[10]; 			
    	struct listNode *nextNode; 
    } list; //'list' is datatype for listNode
    
    
    typedef list *nodeptr;		//having 'nodeptr' as datatype
    
    
    nodeptr current, head, tail, newnode,temp, temp2; //all of this variables are pointer
    int main() {
    /* getting an input from a user and putting it on as a head
    since this is an unsorted list, the first element will be the head*/
    	printf("Enter a name to be listed>> ");
    	
    	current = malloc((10) *sizeof(char));
    	scanf("%s", &current->data); 
    	current->nextNode = NULL;
    
    
    	head=current;
    	tail=current;
    
    
    //and the element follows will either be tail or the current.next
    	printf("Enter a name to be listed>> ");
    
    
        current = malloc((10) *sizeof(char));
    	scanf("%s", &current->data); 
    	current->nextNode = NULL;
    
    
    	tail->nextNode=current;
    	tail=current;
    
    
    	printf("Enter a name to be listed>> ");
    
    
    	current = malloc((10) *sizeof(char));
    	scanf("%s", &current->data); 
    	current->nextNode = NULL;
    
    
    	tail->nextNode=current;
    	tail=current;
    
    
    	printf("Enter a name to be listed>> ");
    
    
    	current = malloc((10) *sizeof(char));
    	scanf("%s", &current->data); 
    	current->nextNode = NULL;
    
    
    	tail->nextNode=current;
    	tail=current;
    	
    	printf("Enter a name to be listed>> ");
    
    
    	current = malloc((10) *sizeof(char));
    	scanf("%s", &current->data); 
    	current->nextNode = NULL;
    
    
    	tail->nextNode=current;
    	tail=current;
    	
    	current = head;
    	int i, j, tmp;
    	int n=5;
    		for (i = 0; i<n; i++){
    			for(j = i+1; j<n; j++){
    				if (current->data[i]>current->data[j]){
    					tmp = current->data[i];
    					current->data[i] = current->data[j];
    					current->data[j] = tmp;
    				}
    			}
    		}
    	
    
    
    		printf("\nNames:\n");
    		while(current != NULL) {
    			printf("\t%s\n", current->data);//displaying element
    			printf("\t%d\n\n", &current->data);//displaying the address of an element
    			current=current->nextNode;		
    	}
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,885
    You want to be doing something like
    strcmp(current->data,current->nextNode->data)

    Then doing a string swap using strcpy.


    As an aside, you really need to start making some functions out of all the repeated code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-23-2015, 11:39 PM
  2. Sorting a linked list alphabetically using strcmp
    By jsuite in forum C Programming
    Replies: 2
    Last Post: 12-07-2012, 09:37 PM
  3. How to sort linked list alphabetically
    By chickenlittle in forum C++ Programming
    Replies: 9
    Last Post: 09-19-2011, 01:36 AM
  4. sort my linked list alphabetically
    By alfd6z in forum C Programming
    Replies: 2
    Last Post: 12-14-2002, 01:33 PM
  5. Inserting alphabetically into a Linked List
    By EDL in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2002, 06:51 AM

Tags for this Thread