Thread: help with sorting linked list

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

    help with sorting linked list

    hi, I have to create a linked list that uses an .txt file and alphabetizes the names into an output file. im struggling on how to sort the input file.

    here is the code i have so far
    Code:
     #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    struct Node
    {
    char name[15];
    char title[15];
    int year;
    struct Node *next;
    struct Node *prev;
    };
    typedef struct Node* Box;
    Box build_node(FILE *inputp);
    int main()
    {
    Box head=NULL,temp;
    FILE *inputp, *outputp;
    int i;
    inputp = fopen("input.txt", "r");
    outputp=fopen("output.txt", "w");
    head=build_node(inputp);
    printf( "%s, %s, %d \n", head->name, head->title, head->year);
    for(i=0; i<5; i++)
    
    {
    temp=build_node(inputp);
    printf( "%s, %s, %d \n", temp->name, temp->title, temp->year); 
    }
    return 0;
    }
    Box build_node(FILE *inputp)
    {
    Box temp=NULL;
    temp=(Box)malloc(sizeof(struct Node));
    fscanf_s(inputp, "%s", &temp->name);
    fscanf_s(inputp, "%s",  &temp->title);
    fscanf_s(inputp, " %d", &temp->year);
    temp->next=NULL;
    temp->prev=NULL;
    return temp;
    }

    im pretty sure that to sort it i need to use this for loop but i cant figure it out exactly
    Code:
     for(i=0, i<5; i++)
    for(here=head; ? ; here=here->next)
    if(here->data > here->next->data)
    swap(here->data, here->next->data)
    i have attached the input file, any help is appreciated

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    im struggling on how to sort the input file.
    First step is to simply get all you data into a linked list unsorted and loop through and print those unsorted items out... then work on the sorting problem. You currently don't seem to be doing even that. To that end, I've got the following issues with what's been posted so far (this is meant to be constructive criticism here so don't take it the wrong way).

    #1. Your build_node function does not test to make sure the malloc call succeeded. You therefore write to memory (in the fscanf_s calls) that may not be properly initialized.
    #2. Your build_node function casts the return result of the malloc call.. this should be avoided. The return type from a call to malloc is void* and should be safely converted without the need for a cast.
    #3. Your build_node function incorrectly make use of the "address-of" operator when attempting to store data into the name and title character array variables that are already essentially addresses.
    #4. Your build_node function does not call the fscanf_s functions properly. From the MSDN page:
    The main difference between the secure functions (with the _s suffix) and the older functions is that the secure functions require the size of each c, C, s, S and [ type field to be passed as an argument immediately following the variable.
    As an example:
    Code:
    // Incorrect form of call to fscanf_s
    fscanf_s( inputp, "%s", temp->name );  // No size parameter passed as required
    
    // Correct form of call to fscanf_s
    fscanf_s( inputp, "%s", temp->name, 14 );
    #5. Your build_node function makes no attempt to determine if the fscanf_s calls succeeded and to handle such conditions gracefully. Your code treats things as if the fscanf_s calls always succeed and that there is always data available.
    #6. In your main function, you do not build any sort of linked list in the loop with your temp pointer. You seem to simply throw away the value each iteration of the loop. This constitutes a memory leak.
    #7. Even if you handled #6 above correctly, you have no apparent code to properly return (free) the malloc'ed memory.
    #8. Your for loop code prints the values pointed to by the temp pointer without first verifying that temp actually points to valid memory. Again, assuming that it points somewhere "good".

    Work on addressing those and getting everything into an actual linked list that works and prints out everything unsorted and we'll go from there.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List help!!
    By onepuzzledstud in forum C Programming
    Replies: 2
    Last Post: 10-11-2010, 08:36 PM
  2. Circularly-Doubly Linked List implementation
    By BlackOps in forum C Programming
    Replies: 4
    Last Post: 07-19-2009, 04:45 AM
  3. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 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