Sorting the linked list

This is a discussion on Sorting the linked list within the C Programming forums, part of the General Programming Boards category; Hello all. I read information from a file and then I convert the times read in based on am/pm to ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    10

    Sorting the linked list

    Hello all. I read information from a file and then I convert the times read in based on am/pm to 24 hour times. After that I am trying to sort the list based on time in ascending order. That is where the sort() comes in however I am not making any headway.

    The file that I am reading from looks something like

    "
    TimeStamp: 1:00PM
    Source : ADM51-28

    TimeStamp: 11:00AM
    Source : ADM51-28

    TimeStamp: 10:59PM
    Source : ADM51-28

    TimeStamp: 11:58PM
    Source : Internet Explorer
    "

    Any suggestions on correctings this sort function will be greatly appreciated. Thanks alot!



    Code:
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    #include <stdlib.h>
    
    typedef struct Node
    {
    	fpos_t begin;
    	fpos_t end;
    	int t_hour;
    	int t_mins;
    	char t_meri[3];
    
    	Node *next;
    }Location;
    
    Location *top , *ptr , *last;
    
    void allmem();
    void convtm();
    void sort();
    
    int main()
    {
    	clrscr();
    	allmem();
    	convtm();
    	sort();
    	getch();
    	return 0;
    }
    
    void sort()
    {
    	Location *j;
    	Location hold;
    
    	ptr = top;
    
    	while(ptr != NULL)
    	{
    		j = ptr->next;
    
    		while(j != NULL)
    		{
    			if(j->t_hour < ptr->t_hour)
    			{
    				hold = *ptr;
    				*ptr = *j;
    				*j = hold;
    			}
    			j = j->next;
    		}
    		ptr = ptr->next;
    	}
    
    
    	ptr = top;
    
    	while(ptr != NULL)
    	{
    		printf("%.2d:%.2d\n" , ptr->t_hour , ptr->t_mins , ptr->t_meri);
    		ptr = ptr->next;
    	}
    }
    
    
    
    
    
    
    
    void convtm()
    {
    	ptr = top;
    
    	while(ptr != NULL)
    	{
    		if(strcmp(ptr->t_meri , "AM") == 0)
    		{
    			if(ptr->t_hour == 12)
    				ptr->t_hour = 0;
    		}
    		else
    		{
    			if(ptr->t_hour == 12)
    				ptr->t_hour = 12;
    			else
    				ptr->t_hour += 12;
    		}
    		ptr = ptr->next;
    	}
    }
    
    
    
    
    
    
    
    
    
    void allmem()
    {
    
    	FILE *read;
    	char str[20];
    	int beginend = 2;
    	read = fopen("spool.dat" , "r");
    	top = NULL;
    
    	while(feof(read) == 0)
    	{
    		fscanf(read , "%s" , str);
    
    		if((strcmp(str , "TimeStamp:") == 0))
    		{
    			if(beginend % 2 == 0)
    			{
    				ptr = (Location *)malloc(sizeof(Location));
    				fscanf(read , "%d:%d%s" , &ptr->t_hour , &ptr->t_mins , ptr->t_meri);
    				fgetpos(read , &ptr->begin);
    				ptr->next = NULL;
    				beginend++;
    
    				if(top == NULL)
    				{
    					top = ptr;
    					last = ptr;
    				}
    				else
    				{
    					last->next = ptr;
    					last = ptr;
    				}
    			}
    			else
    			{
    				fseek(read , -14 , SEEK_CUR);
    				fgetpos(read , &ptr->end);
    				beginend++;
    			}
    		}
    	}
    	fclose(read);
    }

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Swapping the entire struct does nothing for ya. What you need to do is look at the previous node's *next and swap them.
    Edit: Also on your functions you need to put void in the (). And you aren't doing any error checking, this is bad, very very bad.
    Last edited by Thantos; 12-14-2003 at 04:37 PM.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    24

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, 09: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, 11: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, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21