Thread: sorting linked list

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    8

    sorting linked list

    well i have a problem sorting the linked list.. its changing the data instead of changing the pointers..

    anything that can be optimized.. i will appreciate too ^^

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    post agenda.dat so i can do some tests

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > struct student* temp = (struct student*)malloc(sizeof(struct student));
    Casting malloc is poor style in a C program, see the FAQ

    > fflush(stdin);
    This is undefined behaviour - see the FAQ.
    If you don't know what undefined behaviour means, then consider this.
    "Your input buffer is flushed, my input buffer is untouched, his hard disk is being reformatted."

    > gets((*temp).name);
    Ugh, the world's most dangerous function. Never use gets()
    Guess what, this is in the FAQ as well.

    > char *str=(char*)malloc(50);
    This is never freed, this is a memory leak.

    As for sorting a linked list, one way is to create a new list from the old list, and have a 'sorted insert' function which adds a node at the correct place in the list.
    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.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    8
    here u have the agenda.txt.. rename to agenda.dat..
    about the sorting.. it cant be sorted that way..

    the problem is here
    Code:
    		
    *temp=curr->data;
    curr->data=menor->data;
    menor->data=*temp;
    wasnt better chaging the next pointer instead of chaning the data pointer?

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    8

    you can refer to this

    Code:
    typedef struct productNode * pNPtr;
    
    typedef struct productNode{
       char brand[PRODUCT_BRAND_MAX + 1];
       char name[PRODUCT_NAME_MAX + 1];
       unsigned price;  /* Stored in cents (not dollars). */
       unsigned qty;
       pNPtr nextProduct;
    } ProductNodeType;
    
    typedef struct{
       CashType coins[DISTINCT_CASH];
       unsigned totalCoins;
       pNPtr headProduct;
       unsigned totalProducts;
    }
    Code:
    int addProduct(VendingMachineType* vm, char* brand, char* name,
          unsigned price, unsigned qty){
       pNPtr cur, prev,head,newProduct;
    
       head = vm->headProduct;
    
       newProduct = malloc(sizeof(ProductNodeType));
    
       if(newProduct==NULL){
          return FAIL;
       }else{
          cur = head;
          prev = NULL;
    
          /*comparison for brand where to put*/
          while((cur!=NULL) && (strcmp(cur->brand, brand)<0)){
             prev = cur;
             cur = cur->nextProduct;
          }
    
          /*comparison for name where to put when the brand is in order*/
          while((cur!=NULL)&& (strcmp(cur->brand, brand)<=0) &&
                (strcmp(cur->name, name)<0)){
             prev = cur;
             cur=cur->nextProduct;
          }
    
          strcpy(newProduct->brand, brand);
          strcpy(newProduct->name, name);
          newProduct->price = price;
          newProduct->qty = qty;
          newProduct->nextProduct = cur;
          vm->totalProducts++;
    
          if(prev==NULL)
             vm->headProduct = newProduct;  
             else 
                prev->nextProduct = newProduct;     
       }
       return SUCCESS;   
    }

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318

    Wink

    Quote Originally Posted by dlcp View Post
    anything that can be optimized.. i will appreciate too ^^
    Then here's some really useful advice on that front:

    Lookup stricmp. You will no longer need to convert string to lowercase using tolower.

    Next thing. Why would your sorting algorithm ever need to malloc anything? All it has to do is swap around a few pointer values within those structs to reorder the list. 'search', 'find', 'input', and 'main' could also be done without it (perhaps statically allocate).

    Dont use an "if .. else it .. else if .. else if" for checking the values of 'option'. Learn about the 'switch' statement.

    Why do your functions return values if you never look at the return value?

    Best not to use 'new' as a variable name. Sure that will compile okay as long as you use a .c file extension, but syntax hilighters still thinks it's a keyword, and it makes porting to C++ harder.

    Your indenting is consistent, but annoying. Every curly brace should be one tab furthur to the left.

    Just a style issue, but those #defines could more tidily be an enum.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  2. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 07:47 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  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