Thread: sort linked list by surname

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    sort linked list by surname

    Hey guys could someone please assist me in sorting a linked list by a particular element for example by surname. So when the application reads the file it sorts data in the file by surname.





    Code:
    void loadData(TennisStoreType* ts, char* customerFile, char* stockFile)
    {
    
      /* declaration of variables*/
      char* description;
      char* surname;
      char* firstName;
      char* address;
      char* suburb;
      char sTmp[100];
      char cTmp[100];
      char* unitpricePtr;
      char* stocklevelPtr;
      char* postCodePtr;
      char* phoneNumPtr;
    
      float unitprice;
      int stocklevel;  
      int postCode;
      int phoneNum;
    
    
    
      /* pointing nodes to customer variables*/
    
      CustomerNodeType* newCust;
      CustomerNodeType* curCust;
      CustomerNodeType* prevCust;
    
      StockNodeType* newStock;
      StockNodeType* curStock;
      StockNodeType* prevStock;
      /* variables for files*/
    
      FILE *cFile;
      FILE *sFile;
    
      if((cFile = fopen(customerFile, "r")) == NULL || (sFile = fopen(
      stockFile,"r")) == NULL)
      {
        fprintf(stderr, "error opening file\n");
        exit(EXIT_FAILURE);
    
      }
      else
      {
    
        printf("file was opened correctly");
      }
    
      while(fgets(cTmp, 100, cFile ) != NULL)
      {
       
        /* tokenizing variables*/
    
        surname = strtok(cTmp, ",");
        firstName = strtok(NULL, ",");
        address = strtok(NULL, ",");
        suburb = strtok(NULL, ",");
        postCodePtr = strtok(NULL, ",");
        phoneNumPtr = strtok(NULL,"\0");
        postCode = atoi(postCodePtr);
        phoneNum = atoi(phoneNumPtr);
    
        /* allocating memory using malloc for nodes*/        
       
        newCust = malloc(sizeof(*newCust));
    
        /* start of linked list*/
    
        curCust = ts -> headCust;
        prevCust = NULL;
        
        while(curCust !=NULL)
        {
           prevCust = curCust;
           
           curCust = curCust -> nextCust;
    
        }
        /* copy the string variables into newCust node*/
    
        strcpy(newCust->surname,surname);
        strcpy(newCust->firstName,firstName);
        strcpy(newCust->address,address);
        strcpy(newCust->suburb,suburb);
    
        /* newCust is postCode*/
    
        newCust->postCode=postCode;
    
        /* newCust is phoneNum*/
    
        newCust->phoneNum=phoneNum;
       
        /* printing the preloaded data*/
        printf("\t%s",  newCust -> firstName);
        printf("\t%s",  newCust -> surname);
        printf("\t%s",  newCust -> address);
        printf("\t%s",  newCust -> suburb);
        printf("\t%d",  newCust -> postCode);
        printf("\t%d",  newCust -> phoneNum);
    
        /* keep count of each customer in loop*/
    
        ts -> customerCount++;
    
        /* start of linked list*/
    
        if(prevCust == NULL)
        {
          ts ->headCust = newCust;
    
        }
        else
        {
       
          prevCust -> nextCust = newCust;
    
        }
        /* if there is a previous node get the next node and make
           it the new node*/
      }
      fclose(cFile);
     
        
    
    
    }

  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
    Edit:
    Nevermind
    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
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    How could i recode my linked list so i could sort it by surname in alphabetical order?

  4. #4
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    When you are making a linklist by reading the cfile.Dont add every new node at the list end.Traverse the list and compare(use strcmp()) the new node's surname with surname's in list and find the location adjust the pointer.

  5. #5
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    Could you please give me a small example of how i could do that im a bit confused? Thnks

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Tell me, is this your ONLY alter ego on the web
    http://forums.devshed.com/t293650/s.html
    You're just as prolific there at posting as you are here.
    Or are there other instances of you on other message boards?

    Whilst I could help you, I'm far from convinced that you've even learnt the basics (you've already posted several threads with segfault in the title). Your programs are working simply by luck at the moment, not because of anything that you've actually learnt.

    For example, many times you've been told to check the result of strtok(), yet you consistently continue to ignore people, so there's really no incentive to tell you anything else because all that will happen is you'll cherry-pick the bits which work for you and then move on.

    Your loadData() function has been posted in many different forms, which suggests you just keep adding more and more stuff to it (no program design going on here, you're just hacking away until it works).
    Half the stuff you have there should be in other functions.

    I'm wondering whether to suggest you zip up ALL your code as it now stands (and attach it to a message) so that we can fix what you have so far before you dig yourself an even deeper hole.
    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. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  3. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM