Thread: LinkList Sorting in C

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    68

    Lightbulb LinkList Sorting in C

    Hi EveryOne,


    Need Help Baddly, Please a quick look might fix it. The following code has a problem of alphabetize the linklist. this function i can't figure out. Please help me quick i need it. I have tried everything i can. Can some fix it real quick. I also attached the code so that you can see how the who program work. it creat a linklist on random order.

    Thanks
    Code:
    void alphabetize( struct student *ptr)
    {
    	struct student  *sucptr = NULL, *preptr=NULL, *temp = ptr;
    
    	printf("______________________________________________\n");
    	while(temp != NULL)
    	{
    		
    		//alphabetize link list
    		for( sucptr = ptr; sucptr != NULL; sucptr = sucptr->nextptr)	//check list
    		{
    
    			if(strcmp(sucptr->NAME, temp->NAME) == 1)		//find succeeding name
    			{
    				temp->nextptr = sucptr;		//assign nextptr of new ptr to sucptr
    				break;				//break from for loop
    			}
    
    				for( preptr = ptr; preptr != NULL; preptr=preptr->nextptr)
    				{
    					if(preptr->nextptr == sucptr)		//when preptr's nextptr is sucptr...
    					{
    						preptr->nextptr = temp;		//assign preptr's nextptr to ptr of new link
    						break;
    					}
    				}
    
    		}
    		print_list(sucptr);	
    		temp = temp->nextptr;
    	}
    
    	getchar();	
    }

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    /*  **************************************************************
        **************************************************************
    
            The following functions/symbolic constants/arrays  are used 
    		   by generate random student  
        **************************************************************
    	************************************************************** */
    
    
    #define UNOCCUPIED   0
    #define OCCUPIED     1
    
    #define SIZE         25
    
    #define NUMBER_NAME  26
    #define NUMBER_ID    26
    #define NUMBER_GPA  26
    
    char *name_source[]={"Crane", "Daniels", "Lewis","Crabb", "Keaton", "Harris", "Williamson", "Lane", "Richards", "Morris", "Nevins", "John", "Jane", "Wednesday", "Annette", "Noah", "Christopher", "George", "Washington", "Curie", "Rhonda", "Susan", "Mary", "Langely","Thompson", "Reynolds"};
                         
    
    unsigned id_source[]={ 1131, 1134, 1433,1516, 1788, 1899, 1902, 1908, 1923, 1978,2018, 2034, 2077, 2099, 2100,2133,4533,5634,5781, 5892, 5965, 6038, 7891, 7900, 8203, 8244};					  
    
    double gpa_source[] ={1.15, 1.65, 1.88, 1.91, 1.987, 2.00,  2.14, 2.28 , 2.38, 2.45, 2.51, 2.55, 2.66,  2.81, 2.93, 2.99,3.0, 3.33, 3.45, 3.55, 3.65, 3.66, 3.78, 3.88,3.99, 4.00};
    
    
    int name_position[NUMBER_NAME]={UNOCCUPIED};
    int id_position[NUMBER_ID]={UNOCCUPIED};
    int gpa_position[NUMBER_GPA]={UNOCCUPIED};
    
    /*  **************************************************************
        **************************************************************
    
            The following are for your programming assignment 
    		  
        **************************************************************
    	************************************************************** */
    
    
    struct student {
    	char NAME[35];
    	unsigned ID;
    	double GPA;
    	struct student * nextptr;
    };
    
    void alphabetize( struct student *);		//alphabetize linklist
    struct student * list_initialize( int listsize);  // student is to complete
    double generate_avg_gpa(struct student *T);       //student is to complete
    
    
    void free_list(struct student *T);
    void generate_random_student(struct student * S);
    void print_list(struct student *L);
    void space_print(int num);
    
    int main()
    {
    	struct student *toplistptr=NULL;
    	int listsize;
    	char buffer[10];
    	double avg_gpa;
    
    
    	
    	printf("enter a value between 0 and 25\n");
    
    	gets(buffer);
    	listsize=atoi(buffer);
    	listsize%=SIZE;
    
     // UNCOMMENT	
    	toplistptr=list_initialize( listsize);
    	print_list(toplistptr);
    	alphabetize(toplistptr);
    
    	avg_gpa=generate_avg_gpa(toplistptr);
    	printf("\n\n\n*****************************\n*****************************\n");
    	printf("average gpa = %5.4f\n", avg_gpa);
    	printf("\n*****************************\n*****************************\n");
    
    	free_list(toplistptr);
    //------------------------
      
    
    }
    struct student * list_initialize( int listsize)
    {
    	struct student *L=NULL;
    
    	//start modification
    
    	struct student *ptr;
    	int i;
    	L = (struct student *)malloc(sizeof(struct student));	//initialize first link
    	generate_random_student(L);
    	ptr = L;												//use ptr as reference to first link
    
    	for( i=0; i<listsize-1; i++)							//create listsize # of links
    	{
    		ptr->nextptr = (struct student *)malloc(sizeof(struct student));		//set up nextptr of link
    		generate_random_student(ptr->nextptr);
    		ptr = ptr->nextptr;									//move ptr to end | prepare for next looping
    
    	}
    
    	//end modifcation
    	
    
    	return L;
    }
    
    void alphabetize( struct student *ptr)
    {
    	struct student  *sucptr = NULL, *preptr=NULL, *temp = ptr;
    
    	printf("______________________________________________\n");
    	while(temp != NULL)
    	{
    		
    		//alphabetize link list
    		for( sucptr = ptr; sucptr != NULL; sucptr = sucptr->nextptr)	//check list
    		{
    
    			if(strcmp(sucptr->NAME, temp->NAME) == 1)		//find succeeding name
    			{
    				temp->nextptr = sucptr;		//assign nextptr of new ptr to sucptr
    				break;				//break from for loop
    			}
    
    				for( preptr = ptr; preptr != NULL; preptr=preptr->nextptr)
    				{
    					if(preptr->nextptr == sucptr)		//when preptr's nextptr is sucptr...
    					{
    						preptr->nextptr = temp;		//assign preptr's nextptr to ptr of new link
    						break;
    					}
    				}
    
    		}
    		print_list(sucptr);	
    		temp = temp->nextptr;
    	}
    
    	getchar();	
    }
    
    
    
    double generate_avg_gpa(struct student *T)
    {
    	double answer=0.0;
    	int counter = 0;
    
    	struct student *ptr;
    
    	for(ptr = T; ptr != NULL; ptr=ptr->nextptr)
    	{
    		answer = ptr->GPA + answer;
    		counter++;
    	}
    
    	return answer/counter;
    }
    
    
    
    void print_list(struct student *L)
    {
    	printf("NAME");
    	space_print(40-strlen("NAME"));
    	printf("ID");
    	space_print(8);
    	printf("GPA\n");
    	while(L!=NULL)
    	{
    		printf("%s", L->NAME);
    		space_print(40-strlen(L->NAME));
    		printf("%4d", L->ID);
    		space_print(5);
    		printf(" %4.3f\n", L->GPA);
    		L=L->nextptr;
    	}
    }
    
    void space_print(int num)
    {
            while(num-->0)
                    putchar(' ');
    }
    
    
    
    void generate_random_student(struct student * S)
    {
    
    	int i;
    
    	/* generate random name */
    	i=rand()%SIZE;
    	while(name_position[i]!=UNOCCUPIED)
    		i=(i+1)%SIZE;
    	strcpy(S->NAME, name_source[i]);
    	name_position[i]=OCCUPIED;
    
    	/*generate random id */
    	i=rand()%SIZE;
    	while(id_position[i]!=UNOCCUPIED)
    		i=(i+1)%SIZE;
    	S->ID=id_source[i];
    	id_position[i]=OCCUPIED;
    
    	/* generate random gpa */
    	i=rand()%SIZE;
    	while(gpa_position[i]!=UNOCCUPIED)
    		i=(i+1)%SIZE;
    	S->GPA=gpa_source[i];
    	gpa_position[i]=OCCUPIED;
    
    	/* set nextptr to NULL */
    	S->nextptr = NULL;
    
    }
    
    
    void free_list(struct student *T)
    {
    	struct student *tmp1;
    
    	while ( T!=NULL)
    	{
    		tmp1=T;
    		T=T->nextptr;
    		free(tmp1);
    	}
    }

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >Please a quick look might fix it.

    I took a quick look and found:

    >if(strcmp(sucptr->NAME, temp->NAME) == 1)

    The function strcmp() returns 0 when two strings are equal.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    68

    I am getting infinite loop

    Hello Everyone,

    I think I have problem with too many FOR loops and should not be one while loop trivariceing the list modifying the head pointer please can you take a look and see what I am doing wrong, other than strcmp!!!

    I am also trying to debugg with no luck...

    Please help............

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    How are you debugging? Are you using a debugger only? Another usefull technique for debugging is to put printf's in your code. When your program runs, then you can see what happens in the code. Such logging info oftens gives very valuable information on what the program did.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  3. sorting a linklist
    By jampro in forum C Programming
    Replies: 4
    Last Post: 10-21-2003, 07:16 PM
  4. Quick LinkList Help!!adding at the End
    By simly01 in forum C++ Programming
    Replies: 13
    Last Post: 07-28-2002, 07:19 AM
  5. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM