Issue with Linked Listing

This is a discussion on Issue with Linked Listing within the C Programming forums, part of the General Programming Boards category; Obviously, I am by no means an expert on pointers, but if someone would take a look at the following ...

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

    Issue with Linked Listing

    Obviously, I am by no means an expert on pointers, but if someone would take a look at the following code and give me some hints as to why my insertNode() function does not work properly due to logical errors, it would be much appreciated.

    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 deleteNode();
    void fileOpen(FILE **, char *[]);
    void insertNode(struct employeeNode ****, struct employeeNode **);
    void memAlloc(char [], char [], char[], int, struct employeeNode **);
    void memStore(char [], char [], char [], int, struct employeeNode **);
    void printList(struct employeeNode *);
    void read(FILE *, struct employeeNode **);
    void readHire(FILE *, char [], struct employeeNode ***);
    void readInitSal();
    void readPromotions();
    void readTransfers();
    Code:
    #include "hw3Constants.c"
    
    int main(int argc, char *argv[])
    {
    	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, &startPtr);
    	
    	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 deleteNode()
    {
    	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(struct employeeNode ****startPtr, struct employeeNode **newPtr)
    {
    	struct employeeNode *previousPtr = NULL;
    	struct employeeNode *currentPtr = NULL;
    	
    	(**newPtr).nextNode = NULL;
    	if ( (**newPtr).nextNode == NULL)
    		printf("newPtr.nextNode = null\n");
    	
    	previousPtr = NULL;
    	currentPtr = ***startPtr;
    	
    	if ( (***startPtr) == NULL)
    		printf("start ptr = null\n");
    	if( previousPtr == NULL)
    		printf("prev ptr = null\n");
    	if( currentPtr == NULL)
    		printf("current ptr = null\n");
    	
    	while(currentPtr != NULL)
    	{
    		printf("WHILE PASS\n");
    		previousPtr = currentPtr;
    		currentPtr = currentPtr->nextNode;
    	}
    	
    	if(previousPtr == NULL)
    	{
    		printf("IF PASS\n");
    		if ( (**newPtr).nextNode == NULL)
    			printf("NEXT NODE OF NEW PTR NULL\n\n");
    		(**newPtr).nextNode = ***startPtr;
    		**startPtr = newPtr;
    	}
    	else
    	{
    		previousPtr->nextNode = *newPtr;
    		(**newPtr).nextNode = currentPtr;
    	}
    	return;	
    }
    
    void memAlloc(char lName[], char fName[], char eClass[], int id, 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)
    {
    	
    	strcpy( (**newPtr).firstName, fName);
    	strcpy( (**newPtr).lastName, lName);
    	strcpy( (**newPtr).employeeClass, eClass);
    	(**newPtr).id = id;
    	(**newPtr).nextNode = NULL;
    	
    	return;
    }
    
    void printList(struct employeeNode *currentPtr)
    {
    	printf("PRINT LIST EXECUTE\n");
    	if (currentPtr == NULL)
    	{
    		printf("LIST EMPTY\n");
    	}
    	else
    	{
    		printf("List:\n");
    		
    		while ( currentPtr != NULL )
    		{
    			printf("%s", currentPtr->lastName);
    			currentPtr = currentPtr->nextNode;
    		}
    		printf("NULL\n\n");
    	}
    	return;
    }
    
    void read(FILE *inFile, struct employeeNode **startPtr)
    {
    	char readData[50], asterick='*', selection;
    	fgets(readData, 50, inFile);
    	while(!feof(inFile))
    	{
    		if(readData[0] == asterick)
    		{
    			selection = readData[2];	
    			switch (selection)
    			{
    				case 'H': readHire(inFile, readData, &startPtr);
    							return;
    							break;
    				case 'I': readInitSal();
    							break;
    				case 'P': readPromotions();
    							break;
    				case 'T': readTransfers();
    							break;
    				default: break;
    			}
    		}
    	}
    	return;
    }
    
    void readHire(FILE *inFile, char readData[], struct employeeNode ***startPtr)
    {
    	int x, tempID=0;
    	char *firstName=NULL, *lastName=NULL, *id=NULL, *cl=NULL;
    	char tempClass[20], tempLName[20], tempFName[20];
    	
    	struct employeeNode *newPtr = NULL;
    	
    	for(x=0; x<50; x++)
    	{
    		readData[x] = '\0';		
    	}
    	do 
    	{
    		fgets(readData, 50, inFile);
    		if(readData[0] != '*')
    		{
    			dataProcess(readData, firstName, lastName, id, cl, tempLName, tempFName, tempClass, &tempID);
    			memAlloc(tempLName, tempFName, tempClass, tempID, &newPtr);
    			memStore(tempLName, tempFName, tempClass, tempID, &newPtr);	
    			insertNode(&startPtr, &newPtr);			
    			free(firstName);
    			firstName = NULL;
    			free(lastName);
    			lastName = NULL;
    			free(id);
    			id = NULL;
    			free(cl);
    			cl = NULL;
    		}
    	}while(readData[0] != '*');
    		
    	return;
    }
    
    void readInitSal()
    {
    	return;
    }
    
    void readPromotions()
    {
    	return;
    }
    
    void readTransfers()
    {
    	return;
    }
    Any help would be much appreciated, thank you.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    void insertNode(struct employeeNode ****, struct employeeNode **);
    void memAlloc(char [], char [], char[], int, struct employeeNode **);
    void memStore(char [], char [], char [], int, struct employeeNode **);
    void printList(struct employeeNode *);
    void read(FILE *, struct employeeNode **);
    void readHire(FILE *, char [], struct employeeNode ***);

    You don't need to keep adding an extra level of indirection to your pointers every time you pass it from one function to another.

    The initial call is fine
    read(inFile, &startPtr);

    After that, you can just pass a struct employeeNode ** around by value to get the effect you want.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List remove node issue
    By prihod in forum C Programming
    Replies: 1
    Last Post: 04-19-2008, 09:54 AM
  2. Doubly Linked List Deletion Issue
    By josephjah in forum C++ Programming
    Replies: 22
    Last Post: 07-22-2007, 03:00 PM
  3. Linked list copy constructor issue
    By Craptastic! in forum C++ Programming
    Replies: 1
    Last Post: 08-03-2003, 08:30 PM
  4. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM
  5. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM

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