Thread: Sorting Linked Lists

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    5

    Sorting Linked Lists

    I am currently enrolled in a C/C++ course at my given univeristy and was asked to program a linked list and sort it alphabetically and numerically in the following priority: the last name of an employee, the first name of an employee, the id number of an employee, and finally, the classification of an employee.

    I have been successful with sorting by the last name of the employee, yet as I try to sort by first name, then id number, then classification, I received memory leaks and segmentation faults. After spending numerous hours on the issue, I must ask for help.

    Selected Input:
    Code:
    * HIRE
    Jeffries  Tom   449   Boss
    Abrams   Melice  308 Manager
    Jeffries   Tyrone   448    SalesPerson
    Tyson  Kevin 483   Clerk
    Edwards    Jaiki 397   SalesPerson
    Edwards  Jaiki 392   SalesPerson
    O'Neill   Mark 503  Clerk
    * INITIAL SALARIES
    397   4093.04
    308  12019.77
    503 2094.67
    449   80493.97
    483  13486.28
    392   10493.02
    448    14296.08
    * PROMOTIONS
    483   284.06   SalesPerson
    503    194.92 SalesPerson
    397 302.49     Manager
    * TRANSFERS
    449
    397
    * HIRE
    Abernathy  John   521 Manager
    Abernathy   John 302   SalesPerson
    * INITIAL SALARIES
    521  12840.62
    302   2003.08
    * END OF DATA
    Constants:
    Code:
    //Included Libraries
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    //Symbolic Constants
    #define ARGSZ 2
    #define ONE 1
    #define SALARYARRYSZ 2
    #define ZEROD 0.0
    #define ZEROI 0
    
    //Memory Structures
    struct employeeNode
    {
    	char *firstName;
    	char *lastName;
    	int id;
    	char *employeeClass;
    	double salary[SALARYARRYSZ];
    	struct employeeNode *nextNode;
    };
    
    //Function Prototypes
    void cmdLineTest(int, char *[]);
    void dataProcess(char [], char *, char *, char *, char *, char [], char [], char [], int *);
    void fileOpen(FILE **, char *[]);
    void insertNode(FILE *, struct employeeNode **, char []);
    void memAlloc(char [], char [], char [], int , struct employeeNode **);
    void memStore(char [], char [], char [], int , struct employeeNode **);
    void printList(struct employeeNode *);
    void read(FILE *, char []);
    void readInitSal(FILE *, struct employeeNode **, char []);
    void readPromotions(FILE *, struct employeeNode **, char []);
    void readTransfers(FILE *, struct employeeNode **, char[]);
    Driver:
    Code:
    #include "hw3Constants.c"
    
    int main(int argc, char *argv[])
    {
    	char selection, readData[50];
    	struct employeeNode *startPtr = NULL;
    	FILE *inFile = NULL;
    	printf("\n&#37;s %d %s %s %s %s %s %s%c\n\n",	"This is line number", __LINE__,
    										"of the file", __FILE__,
    										"which was\n compiled on", __DATE__,
    										"at time", __TIME__, '.');
    	cmdLineTest(argc, argv);
    	fileOpen(&inFile, argv);
    	read(inFile, readData);
    	while (!feof(inFile))
    	{
    		if (readData[0] == '*')
    		{
    			selection = readData[2];
    			switch (selection)
    			{
    				case 'H':   insertNode(inFile, &startPtr, readData);
    							break;
    				case 'I':	readInitSal(inFile, &startPtr, readData);
    							break;
    				case 'P':	readPromotions(inFile, &startPtr, readData);
    							break;
    				case 'T':	readTransfers(inFile, &startPtr, readData);
    							break;
    				default:	break;
    			}
    		}
    	}
    	printList(startPtr);
    	return;
    }
    
    void cmdLineTest(int argc, char *argv[])
    {
    	if (argc == ARGSZ)
    		printf("The argument supplied is %s\n\n", argv[ARGSZ-ONE]);
    	else
    	{
    			if (argc >2)
    			{
    				printf("Too many arguments supplied. Exiting Program.\n");
    				exit(1);
    			}
    			else
    			{
    				printf("One argument expected. Exiting Program\n");
    				exit(1);
    			}
    	}
    	return;
    }
    
    
    void dataProcess(char data[], char *fName, char *lName, char *id, char *cl, char tempLName[], char tempFName[], char tempClass[], int *tempID)
    {
    	lName = strtok(data, " ");	
    	fName = strtok(NULL, " ");
    	id = strtok(NULL, " ");
    	cl = strtok(NULL, " ");
    	strcpy(tempLName, lName);
    	strcpy(tempFName, fName);
    	strcpy(tempClass, cl);
    	*tempID = atoi(id);
    	return;
    }
    
    void fileOpen(FILE **inFilePtr, char *argv[])
    {
    	if(( *inFilePtr = fopen(argv[ARGSZ-ONE], "r")) == NULL)
    	{
    		printf("File Open Failed. EXITING PROGRAM.\n");
    		exit(1);
    	}
    	return;	
    }
    
    void insertNode(FILE *inFile, struct employeeNode **startPtr, char readData[])
    {
    	
    	int x=0, tempID=0;
    	char *firstName=NULL, *lastName=NULL, *id=NULL, *cl=NULL;
    	char tempClass[20], tempLName[20], tempFName[20];
    	struct employeeNode *newPtr = NULL;
    	struct employeeNode *previousPtr = NULL;
    	struct employeeNode *currentPtr = NULL;	
    	for(x=0; x<50; x++)
    	{
    		readData[x] = '\0';	
    	}
    	fgets(readData, 50, inFile);
    	do
    	{
    			dataProcess(readData, firstName, lastName, id, cl, tempLName, tempFName, tempClass, &tempID);
    			memAlloc(tempLName, tempFName, tempClass, tempID, &newPtr);
    			if(newPtr != NULL)
    			{
    				memStore(tempLName, tempFName, tempClass, tempID, &newPtr);
    				previousPtr = NULL;
    				currentPtr = *startPtr;
    				while( (currentPtr != NULL) && (strcmp(tempLName, currentPtr->lastName) > 0) )
    				{
    						previousPtr = currentPtr;
    						currentPtr = currentPtr->nextNode;
    				}
    				if( (previousPtr == NULL) )
    				{
    					newPtr->nextNode = *startPtr;
    					*startPtr = newPtr;
    				}
    				else
    				{
    					previousPtr->nextNode = newPtr;
    					newPtr->nextNode = currentPtr;
    				}
    			}
    			else
    			{
    				printf("No Memory Available.\n");
    			}
    		fgets(readData, 50, inFile);
    	}while(readData[0] != '*');
    	return;
    }
    
    void memAlloc(char lName[], char fName[], char eClass[], int tempID, struct employeeNode **newPtr)
    {
    	int firstNum, lastNum, classNum;
    	*newPtr = calloc(ONE, sizeof(struct employeeNode));
    	if (newPtr != NULL)
    	{
    		firstNum = strlen(fName);
    		lastNum = strlen(lName);
    		classNum = strlen(eClass);	
    		lName[lastNum] = '\0';
    		fName[firstNum] = '\0';
    		eClass[classNum] = '\0';
    		(**newPtr).firstName = calloc((firstNum+ONE), sizeof(char));
    		(**newPtr).lastName = calloc((lastNum+ONE), sizeof(char));
    		(**newPtr).employeeClass = calloc((classNum+ONE), sizeof(char));
    	}
    	else
    	{
    		printf("No Memory Available. Exiting Program");
    		exit(1);	
    	}
    	return;	
    }
    
    void memStore(char lName[], char fName[], char eClass[], int id, struct employeeNode **newPtr)
    {
    	int x;
    	strcpy( (**newPtr).firstName, fName);
    	strcpy( (**newPtr).lastName, lName);
    	for(x=0; x<50; x++)
    	{
    		if (eClass[x] == '\n')
    		{
    			eClass[x] = '\0';	
    		}
    	}
    	strcpy( (**newPtr).employeeClass, eClass);
    	(**newPtr).id = id;
    	(**newPtr).nextNode = NULL;
    	return;
    }
    
    void printList(struct employeeNode *currentPtr)
    {
    	int numEmployee=1;
    	if (currentPtr == NULL)
    	{
    		printf("List is empty.\n\n");
    	}
    	else
    	{
    		printf("The Employees Are:\n");
    		printf("\n"); 
    		printf("\n");
    		printf("----------------------------------------------------------------------------------------\n");
    		while (currentPtr != NULL)
    		{
    			printf("%3d %X  %-10s%-10s %3d %-15s$%9.2f $%9.2f%13X\n", 
    													numEmployee,
    													currentPtr,
    													currentPtr->firstName,
    													currentPtr->lastName,
    													currentPtr->id,
    													currentPtr->employeeClass,
    													currentPtr->salary[0],
    													currentPtr->salary[1],
    													currentPtr->nextNode);
    			currentPtr = currentPtr->nextNode;
    			numEmployee+=1;
    		}
    		printf("NULL\n\n");
    	}
    	return;
    }
    
    void read(FILE *inFile, char readData[50])
    {
    	fgets(readData, 50, inFile);
    	return;
    }
    
    void readInitSal(FILE *inFile, struct employeeNode **searchPtr, char readData[])
    {
    	int idTrue=0, x=0;
    	double salaryTrue=0.0;
    	char *idStr = NULL, *salaryString = NULL;
    	struct employeeNode *currentPtr;
    	fgets(readData, 50, inFile);
    	do
    	{
    		idStr = strtok(readData, " ");
    		salaryString = strtok(NULL, " ");
    		idTrue = atoi(idStr);
    		salaryTrue = atof(salaryString);
    		fgets(readData, 50, inFile);
    		
    		currentPtr = *searchPtr;
    		while (currentPtr != NULL)
    		{
    			if ( (currentPtr->id) == idTrue)
    			{
    				currentPtr->salary[0] = salaryTrue;	
    			}
    			currentPtr = currentPtr->nextNode;
    		}
    	}while(readData[0] != '*');
    	return;
    }
    
    void readPromotions(FILE *inFile, struct employeeNode **searchPtr, char readData[])
    {
    	int idTrue=0, x=0;
    	double pSalaryTrue=0.00;
    	char pClass[20]={'\0'}, *idStr=NULL, *pSalaryString=NULL, *pClassString=NULL;
    	struct employeeNode *currentPtr;
    	fgets(readData, 50, inFile);
    	do
    	{
    		idStr = strtok(readData, " ");
    		pSalaryString = strtok(NULL, " ");
    		pClassString = strtok(NULL, " ");
    		idTrue = atoi(idStr);
    		pSalaryTrue = atof(pSalaryString);
    		strcpy(pClass, pClassString);
    		for (x=0; x<20; x++)
    		{
    			if (pClass[x] == '\n')
    				pClass[x] = '\0';
    		}
    		currentPtr = *searchPtr;
    		while (currentPtr != NULL)
    		{
    			if ((currentPtr->id) == idTrue)
    			{
    				currentPtr->salary[1] = pSalaryTrue;
    				strcpy((currentPtr->employeeClass), pClass);
    			}
    			currentPtr = currentPtr->nextNode;
    		}
    		fgets(readData, 50, inFile);
    	}while(readData[0] != '*');
    	return;
    }
    
    void readTransfers(FILE *inFile, struct employeeNode **startPtr, char readData[])
    {
    	char idStr[10];
    	int idTrue;	
    	struct employeeNode *previousPtr = NULL;
    	struct employeeNode *currentPtr = NULL;
    	struct employeeNode *tempPtr = NULL;
    	fgets(readData, 50, inFile);
    	do
    	{
    		strcpy(idStr, readData);
    		idTrue = atoi(idStr);
    		if (idTrue == (*startPtr)->id)
    		{
    			tempPtr = *startPtr;
    			*startPtr = (*startPtr)->nextNode;
    			free(tempPtr);
    			tempPtr = NULL;
    		}
    		else
    		{
    			previousPtr = *startPtr;
    			currentPtr = (*startPtr)->nextNode;
    			
    			while ((currentPtr != NULL) && (currentPtr->id != idTrue))
    			{
    				previousPtr = currentPtr;
    				currentPtr = currentPtr->nextNode;
    			}
    			
    			if (currentPtr != NULL)
    			{
    				tempPtr = currentPtr;
    				previousPtr->nextNode = currentPtr->nextNode;
    				free(tempPtr);
    			}
    		}
    		fgets(readData, 50, inFile);
    	}while(readData[0] != '*');
    	return;
    }
    Ouput is as Follows after Sorting By Last Name:
    Code:
    The Employees Are:
    
    
    ----------------------------------------------------------------------------------------
      1 11DF6250  John      Abernathy  302 SalesPerson    $  2003.08 $     0.00     11DF64D0
      2 11DF64D0  John      Abernathy  521 Manager        $ 12840.62 $     0.00     11DF62F0
      3 11DF62F0  Melice    Abrams     308 Manager        $ 12019.77 $     0.00     11DF6570
      4 11DF6570  Jaiki     Edwards    392 SalesPerson    $ 10493.02 $     0.00     11DF6390
      5 11DF6390  Tyrone    Jeffries   448 SalesPerson    $ 14296.08 $     0.00     11DF6610
      6 11DF6610  Mark      O'Neill    503 SalesPerson    $  2094.67 $   194.92     11DF6430
      7 11DF6430  Kevin     Tyson      483 SalesPerson    $ 13486.28 $   284.06            0
    NULL

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't see anywhere in this code where you try to sort on anything but last name?

    But it would go in essentially the same place: after your while loop stops, check whether strcmp returns 0; if so, do your secondary check (also a while loop) and when your secondary check stops see whether you have same first names; if so, do your tertiary check ....

    If it were me, I would make a function like SortsBefore(struct employeeNode *, struct employeeNode *) which would return 1 or 0 depending on which employee should come first in the list.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't see a sort function in your code.

    Sorting a linked list is not entirely trivial, since most simple sorting methods are based on arrays, not linked lists.

    Two obvious solutions are:
    1. A kind of "shuffle up" sort, where you walk through the linked list one item at a time, and swap the current with the next if the criteria requires that (e.g. swap current with next if the last name of next is "less than" the current last name). Whenever you reach the end of the list, start over again, until such a time where there are no swaps.

    2. Regenerate a the list by using an insertion sort algorithm - insertion sort is fairly efficient method where you find the correct space for each item when adding it to the list (or array).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by matsp View Post
    I don't see a sort function in your code.
    Neither do I!
    2. Regenerate a the list by using an insertion sort algorithm - insertion sort is fairly efficient method where you find the correct space for each item when adding it to the list (or array).
    Insertion Sort is the simplest singly-linked-list sorting algorithm there is, I believe.
    It's not too bad performance-wise either, in some cases.

    But yeah, you need to start by choosing a sorting algorithm and start implementing it. Equally as important, you need to write a proper comparison function to use with it.

    Your code is far too liberal with dynamic memory allocation for my liking. It leaks like a sieve too!
    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"

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    5
    Thank you for all the help, and I have successfully implemented a sorting algorithm.

    How could I tighten up my code and prevent the given memory leaks?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Show your current code for a review.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    5
    Code:
    /**********************************************************************************
    PROGRAMMER			: David King
    LANGUAGE			: C
    CLASS				: CSE1320-001, Spring 2008
    COMPILER			: gcc
    OPERATING SYSTEM	: UNIX
    PLATFORM			: OMEGA
    COURSE				: Intermediate Programming
    ASSIGNMENT			: Homework #3
    ASSIGNED			: April 8th, 2008
    FILED AS			: hw3DavidKingDrivers.c
    FILES USED			: hw3DavidKingDrivers.c
    			 	      hw3DavidKingConstants.c
    CONCEPTS			: Structures, C File Processing,
    					  Command Line Parameters,
    					  Singly-Linked Lists
    WEIGHT				: 12%
    PURPOSE				: define main() and all other functions				  
    **********************************************************************************/
    #include "hw3Constants.c"
    
    int main(int argc, char *argv[])
    {
    	char selection, readData[50];
    	struct employeeNode *startPtr = NULL;
    	FILE *inFile = NULL;
    	printf("\n%s %d %s %s %s %s %s %s%c\n\n",	"This is line number", __LINE__,
    										"of the file", __FILE__,
    										"which was\n compiled on", __DATE__,
    										"at time", __TIME__, '.');
    	cmdLineTest(argc, argv);
    	fileOpen(&inFile, argv);
    	read(inFile, readData);
    	while (!feof(inFile))
    	{
    		if (readData[0] == '*')
    		{
    			selection = readData[2];
    			switch (selection)
    			{
    				case 'H':   insertNode(inFile, &startPtr, readData);
    							break;
    				case 'I':	readInitSal(inFile, &startPtr, readData);
    							break;
    				case 'P':	readPromotions(inFile, &startPtr, readData);
    							break;
    				case 'T':	readTransfers(inFile, &startPtr, readData);
    							break;
    				default:	break;
    			}
    		}
    	}
    	printList(startPtr);
    	return;
    }
    
    void cmdLineTest(int argc, char *argv[])
    {
    	if (argc == ARGSZ)
    		printf("The argument supplied is %s\n\n", argv[ARGSZ-ONE]);
    	else
    	{
    			if (argc >2)
    			{
    				printf("Too many arguments supplied. Exiting Program.\n");
    				exit(1);
    			}
    			else
    			{
    				printf("One argument expected. Exiting Program\n");
    				exit(1);
    			}
    	}
    	return;
    }
    
    
    void dataProcess(char data[], char *fName, char *lName, char *id, char *cl, char tempLName[], char tempFName[], char tempClass[], int *tempID)
    {
    	lName = strtok(data, " ");	
    	fName = strtok(NULL, " ");
    	id = strtok(NULL, " ");
    	cl = strtok(NULL, " ");
    	strcpy(tempLName, lName);
    	strcpy(tempFName, fName);
    	strcpy(tempClass, cl);
    	*tempID = atoi(id);
    	return;
    }
    
    void fileOpen(FILE **inFilePtr, char *argv[])
    {
    	if(( *inFilePtr = fopen(argv[ARGSZ-ONE], "r")) == NULL)
    	{
    		printf("File Open Failed. EXITING PROGRAM.\n");
    		exit(1);
    	}
    	return;	
    }
    
    void insertNode(FILE *inFile, struct employeeNode **startPtr, char readData[])
    {
    	
    	int x=0, tempID=0;
    	char *firstName=NULL, *lastName=NULL, *id=NULL, *cl=NULL;
    	char tempClass[20], tempLName[20], tempFName[20];
    	struct employeeNode *newPtr = NULL;
    	struct employeeNode *previousPtr = NULL;
    	struct employeeNode *currentPtr = NULL;	
    	for(x=0; x<50; x++)
    	{
    		readData[x] = '\0';	
    	}
    	fgets(readData, 50, inFile);
    	do
    	{
    			dataProcess(readData, firstName, lastName, id, cl, tempLName, tempFName, tempClass, &tempID);
    			memAlloc(tempLName, tempFName, tempClass, tempID, &newPtr);
    			if(newPtr != NULL)
    			{
    				memStore(tempLName, tempFName, tempClass, tempID, &newPtr);
    				previousPtr = NULL;
    				currentPtr = *startPtr;
    				while( (currentPtr != NULL) && (strcmp(tempLName, currentPtr->lastName) > 0) )
    				{
    						previousPtr = currentPtr;
    						currentPtr = currentPtr->nextNode;
    				}
    				while( (currentPtr != NULL) && (strcmp(tempLName, currentPtr->lastName) == 0) && (strcmp(tempFName, currentPtr->firstName) > 0) )
    				{
    					previousPtr = currentPtr;
    					currentPtr = currentPtr->nextNode;	
    				}
    				while( (currentPtr != NULL) && (strcmp(tempLName, currentPtr->lastName) == 0) && (strcmp(tempFName, currentPtr->firstName) == 0) && ( tempID > currentPtr->id) )
    				{
    					
    					previousPtr = currentPtr;
    					currentPtr = currentPtr->nextNode;	
    				}
    				while( (currentPtr != NULL) && (strcmp(tempLName, currentPtr->lastName) == 0) && (strcmp(tempFName, currentPtr->firstName) == 0) && (tempID > currentPtr->id) && (strcmp(tempClass, currentPtr->employeeClass) > 0) )
    				{
    					previousPtr = currentPtr;
    					currentPtr = currentPtr->nextNode;
    				}
    				if( (previousPtr == NULL) )
    				{
    					newPtr->nextNode = *startPtr;
    					*startPtr = newPtr;
    				}
    				else
    				{
    					previousPtr->nextNode = newPtr;
    					newPtr->nextNode = currentPtr;
    				}
    				printList(*startPtr);
    				printf("[HIRE => INSERT NODE] Read: %s %s %d %s\n\n", tempLName, tempFName, tempID, tempClass);
    			}
    			else
    			{
    				printf("No Memory Available.\n");
    			}
    		fgets(readData, 50, inFile);
    	}while(readData[0] != '*');
    	return;
    }
    
    void memAlloc(char lName[], char fName[], char eClass[], int tempID, struct employeeNode **newPtr)
    {
    	int firstNum, lastNum, classNum;
    	*newPtr = calloc(ONE, sizeof(struct employeeNode));
    	if (newPtr != NULL)
    	{
    		firstNum = strlen(fName);
    		lastNum = strlen(lName);
    		classNum = strlen(eClass);	
    		lName[lastNum] = '\0';
    		fName[firstNum] = '\0';
    		eClass[classNum] = '\0';
    		(**newPtr).firstName = calloc((firstNum+ONE), sizeof(char));
    		(**newPtr).lastName = calloc((lastNum+ONE), sizeof(char));
    		(**newPtr).employeeClass = calloc((classNum+ONE), sizeof(char));
    	}
    	else
    	{
    		printf("No Memory Available. Exiting Program");
    		exit(1);	
    	}
    	return;	
    }
    
    void memStore(char lName[], char fName[], char eClass[], int id, struct employeeNode **newPtr)
    {
    	int x;
    	strcpy( (**newPtr).firstName, fName);
    	strcpy( (**newPtr).lastName, lName);
    	for(x=0; x<50; x++)
    	{
    		if (eClass[x] == '\n')
    		{
    			eClass[x] = '\0';	
    		}
    	}
    	strcpy( (**newPtr).employeeClass, eClass);
    	(**newPtr).id = id;
    	(**newPtr).nextNode = NULL;
    	return;
    }
    
    void printList(struct employeeNode *currentPtr)
    {
    	int numEmployee=1;
    	if (currentPtr == NULL)
    		printf("\nThere store has 0 Employees:\n");
    	else
    		printf("\nThe Employees Are:\n");
    	printf("%63s %10s\n", "INITIAL", "PROMOTION");
    	printf("%3s%9s %-10s%-10s%4s %-15s %9s %10s%13s\n", "NUM", "CURRENT", "FIRST", "LAST", "ID", "CLASSIFICATION", "SALARY", "SALARY", "NEXT");
    	printf("---------------------------------------------------------------------------------------\n");
    	if (currentPtr == NULL)
    		printf("NULL\n");
    	else
    	{
    		while (currentPtr != NULL)
    		{
    			
    			printf("%3d%9X %-10s%-10s%4d %-15s$%9.2f $%9.2f", 
    													numEmployee,
    													currentPtr,
    													currentPtr->firstName,
    													currentPtr->lastName,
    													currentPtr->id,
    													currentPtr->employeeClass,
    													currentPtr->salary[0],
    													currentPtr->salary[1]);
    			if(currentPtr->nextNode != NULL)
    				printf("%13X\n", currentPtr->nextNode);
    			else
    				printf("%13s\n", "NULL");
    		numEmployee+=1;
    		currentPtr = currentPtr->nextNode;
    		}
    	}
    	printf("\n");
    	return;
    }
    
    void read(FILE *inFile, char readData[50])
    {
    	fgets(readData, 50, inFile);
    	return;
    }
    
    void readInitSal(FILE *inFile, struct employeeNode **searchPtr, char readData[])
    {
    	int idTrue=0, x=0;
    	double salaryTrue=0.0;
    	char *idStr = NULL, *salaryString = NULL;
    	struct employeeNode *currentPtr;
    	fgets(readData, 50, inFile);
    	do
    	{
    		idStr = strtok(readData, " ");
    		salaryString = strtok(NULL, " ");
    		idTrue = atoi(idStr);
    		salaryTrue = atof(salaryString);
    		fgets(readData, 50, inFile);
    		
    		currentPtr = *searchPtr;
    		while (currentPtr != NULL)
    		{
    			if ( (currentPtr->id) == idTrue)
    			{
    				currentPtr->salary[0] = salaryTrue;	
    			}
    			currentPtr = currentPtr->nextNode;
    		}
    		printList(*searchPtr);
    		printf("[INITIAL SALARIES => MODIFY NODE] Read: %d %.2f\n\n", idTrue, salaryTrue);
    	}while(readData[0] != '*');
    	return;
    }
    
    void readPromotions(FILE *inFile, struct employeeNode **searchPtr, char readData[])
    {
    	int idTrue=0, x=0;
    	double pSalaryTrue=0.00;
    	char pClass[20]={'\0'}, *idStr=NULL, *pSalaryString=NULL, *pClassString=NULL;
    	struct employeeNode *currentPtr;
    	fgets(readData, 50, inFile);
    	do
    	{
    		idStr = strtok(readData, " ");
    		pSalaryString = strtok(NULL, " ");
    		pClassString = strtok(NULL, " ");
    		idTrue = atoi(idStr);
    		pSalaryTrue = atof(pSalaryString);
    		strcpy(pClass, pClassString);
    		for (x=0; x<20; x++)
    		{
    			if (pClass[x] == '\n')
    				pClass[x] = '\0';
    		}
    		currentPtr = *searchPtr;
    		while (currentPtr != NULL)
    		{
    			if ((currentPtr->id) == idTrue)
    			{
    				currentPtr->salary[1] = pSalaryTrue;
    				strcpy((currentPtr->employeeClass), pClass);
    			}
    			currentPtr = currentPtr->nextNode;
    		}
    		fgets(readData, 50, inFile);
    		printList(*searchPtr);
    		printf("[PROMOTIONS => MODIFY NODE] Read: %d %.2f %s\n\n", idTrue, pSalaryTrue, pClass);
    	}while(readData[0] != '*');
    	return;
    }
    
    void readTransfers(FILE *inFile, struct employeeNode **startPtr, char readData[])
    {
    	char idStr[10];
    	int idTrue;	
    	struct employeeNode *previousPtr = NULL;
    	struct employeeNode *currentPtr = NULL;
    	struct employeeNode *tempPtr = NULL;
    	fgets(readData, 50, inFile);
    	do
    	{
    		strcpy(idStr, readData);
    		idTrue = atoi(idStr);
    		if (idTrue == (*startPtr)->id)
    		{
    			tempPtr = *startPtr;
    			*startPtr = (*startPtr)->nextNode;
    			free(tempPtr);
    			tempPtr = NULL;
    		}
    		else
    		{
    			previousPtr = *startPtr;
    			currentPtr = (*startPtr)->nextNode;
    			
    			while ((currentPtr != NULL) && (currentPtr->id != idTrue))
    			{
    				previousPtr = currentPtr;
    				currentPtr = currentPtr->nextNode;
    			}
    			
    			if (currentPtr != NULL)
    			{
    				tempPtr = currentPtr;
    				previousPtr->nextNode = currentPtr->nextNode;
    				free(tempPtr);
    			}
    		}
    		fgets(readData, 50, inFile);
    		printList(*startPtr);
    		printf("[TRANSFERS => DELETE NODE] Read: %d \n\n", idTrue);
    	}while(readData[0] != '*');
    	return;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting in linked lists
    By vice in forum C Programming
    Replies: 2
    Last Post: 05-07-2005, 10:07 PM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  3. Sorting Linked Lists (code not concept)
    By Newbie Magic in forum C++ Programming
    Replies: 2
    Last Post: 05-11-2004, 08:57 AM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM