Thread: Sorting alphabetically in C using strcmp

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    45

    Sorting alphabetically in C using strcmp

    Hi, everybody!


    I'm working linked list. But It doesn't work! Why? Please help me.

    Code:
    .....
    .....
    cityNode *insertNewRecord(cityNode *listNode, int id, char name[], int Counter){ 
    
        int i,j;
    
        //char temp[MAX_SIZE];
    
        cityNode *newRecord = (cityNode*)malloc(sizeof(cityNode));
    
        cityNode *temp;
    
     
    
            newRecord->id = id;
    
            strcpy(newRecord->name,name);
    
            newRecord->next = listNode;
    
            listNode = newRecord;
    
     
    
        if(Counter!=1){
    
     
    
            for(i = 0; i < Counter-1; i++){
    
                for(j = 0; j < Counter-i-1;j++){
    
                    if(strcmp(newRecord[j+1].name,newRecord[j].name) <= 0){
    
                      temp = newRecord->name[j];
    
                      newRecord->name[j] = newRecord->name[j+1];
    
                      newRecord->name[j+1] = temp;
    
                    }
    
                }
    
            }
    
        }
    
     
    
        return listNode; 
    
    }
    ...
    ...

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > strcpy(newRecord->name,name);
    Well it looks like record->name is an array, so you need three strcpy calls to 'swap' over the name in your sort.

    You also need to swap record->id at the same time.
    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.

  3. #3
    Registered User
    Join Date
    Nov 2014
    Posts
    45
    Hi Salem thank you your helping.

    It is right code??

    Code:
    cityNode *insertNewRecord(cityNode *listNode, int id, char name[], int Counter){
    
        int i,j,TEM;
        char temp[MAX_SIZE];
        cityNode *newRecord = (cityNode*)malloc(sizeof(cityNode));
    //    cityNode *temp;
    
    
            newRecord->id = id;
            strcpy(newRecord->name,name);
            newRecord->next = listNode;
            listNode = newRecord;
    
    
        if(Counter!=1){
    
    
            for(i = 0; i < Counter-1; i++){
                for(j = 0; j < Counter-i-1;j++){
                    if(strcmp(newRecord[j+1].name,newRecord[j].name) < 0){
                        strcpy(temp,newRecord[j+1].name);
                        TEM = newRecord[j+1].id;
                        strcpy(newRecord[j+1].name,newRecord[j].name);
                        newRecord[j+1].id = newRecord[j].id;
                        strcpy(newRecord[j].name,temp);
                        newRecord[j].id = TEM;
                        }
                    }
            }
    
    
        }
    
    
        return listNode;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Hi Salem thank you your helping.
    > It is right code??
    Well it looks about right, but you really ought to test it to find out for yourself.

    Especially since whether it works or not is heavily dependent on all the code you haven't posted.
    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.

  5. #5
    Registered User
    Join Date
    Nov 2014
    Posts
    45
    Salem, It doesn't work.

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    
    
    #define MAX_SIZE 30
    
    
    typedef struct Cities{
        char name[20];
        int id;
        struct Cities *next;
    }cityNode;
    
    
    cityNode *deleteRecord(cityNode *listNode,int pCode);
    void traverseTheList(cityNode *Node);
    cityNode *insertNewRecord(cityNode *listNode, int id, char name[], int Counter);
    char getOption();
    void printMenu();
    int main()
    {
       int id, Counter = 0, pCode;
       char name[MAX_SIZE], option;
       cityNode *linkList = NULL;
    
    
       static const char exitValue = '4';
    
    
       printMenu();
    
    
       do{
        switch(option = getOption()){
        case '1':
            Counter++;
            printf("\n");
            printf("Sehir plakasi: ");
            scanf("%d",&id);
            fflush(stdin);
            printf("Sehir ismi: ");
            gets(name);
            fflush(stdin);
            linkList = insertNewRecord(linkList,id,name,Counter);
            break;
        case '2':
            Counter--;
            printf("\nPlaka Kodu: ");
            scanf("%d",&pCode);
            linkList = deleteRecord(linkList,pCode);
            break;
        case '3':
            traverseTheList(linkList);
            break;
    
    
        }
    
    
       }while(exitValue !=  option);
    
    
    
    
        return 0;
    }
    
    
    void traverseTheList(cityNode *Node){
    
    
        cityNode *list = Node;
    
    
        while(list!=NULL){
            printf("\n");
            printf("%d --> %s",list->id,list->name);
            list = list->next;
        }
    
    
        printf("\n");
    }
    
    
    cityNode *deleteRecord(cityNode *listNode,int pCode){
    
    
        cityNode *previousNode;
        cityNode *node = listNode;
    
    
        if(listNode == NULL){
            printf("\nListe bos.\n");
            return NULL;
        }
    
    
        if(node->id == pCode){
            printf("\nKayit Silindi.\n");
            node = listNode;
            free(node);
            listNode = listNode->next;
            return listNode;
        }
        else{
            while(node != NULL && node->id != pCode){
                previousNode = node;
                node = node->next;
            }
            if(node == NULL){
                printf("Kayit bulunamadi.");
                return NULL;
            }
            else if(node->id == pCode){
                printf("\nKayit Silindi.\n");
                previousNode->next = node->next;
                free(node);
                return listNode;
            }
        }
    }
    
    
    cityNode *insertNewRecord(cityNode *listNode, int id, char name[], int Counter){
    
    
        int i,j,TEM;
        char temp[MAX_SIZE];
        cityNode *newRecord = (cityNode*)malloc(sizeof(cityNode));
    //    cityNode *temp;
    
    
            newRecord->id = id;
            strcpy(newRecord->name,name);
            newRecord->next = listNode;
            listNode = newRecord;
    
    
        if(Counter!=1){
    
    
            for(i = 0; i < Counter-1; i++){
                for(j = 0; j < Counter-i-1;j++){
                    if(strcmp(newRecord[j+1].name,newRecord[j].name) < 0){
                        strcpy(temp,newRecord[j+1].name);
                        TEM = newRecord[j+1].id;
                        strcpy(newRecord[j+1].name,newRecord[j].name);
                        newRecord[j+1].id = newRecord[j].id;
                        strcpy(newRecord[j].name,temp);
                        newRecord[j].id = TEM;
                        }
                    }
            }
    
    
        }
    
    
        return listNode;
    }
    char getOption(){
    
    
        char opt;
        printf("Secenek: ");
    
    
        switch(opt = getch()){
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':return opt;
            default:printf("Gecersiz bir islem yapildi\n");
        }
    
    
        return opt;
    }
    
    
    void printMenu(){
        printf("----------------------------------\n");
        printf("1 - Yeni kayit ekle\n");
        printf("2 - Kayit sil\n");
        printf("3 - Listeyi goster\n");
        printf("4 - Cikis\n");
        printf("----------------------------------\n");
    }

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    "It doesn't work" does not tell us a whole lot. What input are you giving it, what output are you expecting, and what output are you actually getting?

    Also, it would be a lot easier for us to run/test your code if the prompts were in english.



    Some other things...

    Check your compiler warnings:

    Code:
    /*
    main.c|170|warning: implicit declaration of function 'getch'|
    main.c||In function 'deleteRecord':|
    main.c|123|warning: control reaches end of non-void function|
    ||=== Build finished: 0 errors, 2 warnings ===|
    */
    "getch()" is non-standard, and not a part of any of the headers you included. Use "getchar()" instead.

    Your "deleteRecord()" function might not always return a value, based on how your logic is structured. Since you're assigning this return value to a variable elsewhere, your code has the potential for undefined behavior, if control reaches the end of that function without seeing a "return".


  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I'm thinking about just copying all lines started with dot into signature... Anyway they should be added to the response so often...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If it does not violate any constraints (e.g., on memory, or artificially imposed by an instructor), you could "cheat" by creating a dynamic array (e.g., of pointers to nodes), sort it using qsort by passing a wrapper around strcmp as the comparator callback, then re-form the linked list out of the sorted array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting a linked list alphabetically using strcmp
    By jsuite in forum C Programming
    Replies: 2
    Last Post: 12-07-2012, 09:37 PM
  2. Alphabetically sorting argv[]
    By serg_yegi in forum C Programming
    Replies: 7
    Last Post: 03-27-2010, 07:25 PM
  3. sorting alphabetically
    By ahming in forum C Programming
    Replies: 8
    Last Post: 05-26-2005, 10:34 AM
  4. Sorting Alphabetically
    By orgy230 in forum C Programming
    Replies: 1
    Last Post: 12-04-2004, 02:28 PM