Thread: My program isn't inserting a new node in the beginning of the list...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    21

    My program isn't inserting a new node in the beginning of the list...

    Alright, so I basically have three options in the function insertManager. I can put the new node in the beginning, middle, or end. I was able to put it in the end without any problems, but when I try to put it in the beginning the new node either disappears or appears as the second node depending on how I change pPre's definition.

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<crtdbg.h>
    
    #define FLUSH while (getchar () != '\n')
    #define FILE_IN "countries.txt"
    
    // Type Definitions 
    
    typedef char KEY_TYPE;
    typedef struct {
    	char *name;
    	char *capital;
    	long population;
    	} COUNTRY;
    
    typedef struct {
    	KEY_TYPE key[3];
    	COUNTRY list;
    	} DATA;
    
    typedef struct nodeTag {
    	DATA			data;
    	struct nodeTag*	link;
    	} NODE;
    
    // Prototype Declarations
    char getOption	  (void);
    void printMenu    (void);
    
    int   searchList  (NODE *pList, NODE **pPre, NODE **pCur, char target[]);
    NODE *insertNode  (NODE *pList, NODE *pPre,  DATA  item);
    NODE *deleteNode  (NODE *pList, NODE *pPre,  NODE *pCur);
    void  printList   (NODE *pList);
    NODE *destroyList (NODE *pList);
    
    NODE *buildList (void);
    
    NODE *processListManager (NODE *pList);
    void  searchManager      (NODE *pList);
    NODE *insertManager	     (NODE *pList);
    NODE *deleteManager      (NODE *pList);
    
    int getData ( FILE *spIn, DATA *data );
    void insertCommas ( NODE *pWalker, char *population );
    void printCountry ( NODE *list, char populationInCommas[] );
    int insertHandler ( DATA *data, char target[] );
    
    int main ( void )
    {
    
    //  Local Definitions 
        NODE *list = NULL;
    
    //  Statements 
        printf("\t\t LAB 8 - LINKED LISTS\n\n");
    	list = buildList();
        printMenu();
    	processListManager ( list );
        
        
    	printf("\n\t\tEnd of LAB 8 - LINKED LISTS"
    		   "\n\t\tHave a great day!\n");
    
        
        printf( _CrtDumpMemoryLeaks() ? "Memory Leak\n" : "No Leak\n");
    
        return 0;
    
    }// main 
    
    /*	==================== getOption ====================
    	Gets and validates the user's option.
    		Pre		: nothing
    		Post	: valid option returned
        Written by  : Instructor
    */
    char getOption	(void)
    {
    //  Local Definitions 
    	char option;
    
    //  Statements 
    	printf ("\n\nPlease enter the option: ");
    	scanf  ("&#37;c", &option);
        FLUSH;
    	option = toupper (option);
        while(strchr("LSIDME", option) == NULL)
        {
            printf("\..........* Invalid option! ***\n");
            printf("Enter one of the following letters: L, S, I, D, M, E: " );
            scanf  ("%c", &option);
            FLUSH;
    	    option = toupper (option);
        }
    
    	return option;
    
    } // getOption 
    
    /* ============================== printMenu ==============================
    	Prints the menu to the screen.
    	   PRE  : nothing
    	   POST : menu printed
           Written by  : Instructor
    */
    void printMenu (void)
    {
    //  Local Definitions 
    
    //  Statements 
    	printf("\n\t\t**********************"
               "\n\t\t*                    *"
    	       "\n\t\t*   L - List         *"
               "\n\t\t*   S - Search       *"
    	       "\n\t\t*   I - Insert       *"
    	       "\n\t\t*   D - Delete       *"
    	       "\n\t\t*   M - Print Menu   *"
    	       "\n\t\t*   E - Exit         *"
    	       "\n\t\t*                    *"
    	       "\n\t\t**********************");
    
    	return;
    
    } // printMenu 
    
    /* ============================== processListManager ====================
    	Process user's option by calling appropriate functions.
    	   PRE  : pList - a pointer to the first node of a linked list
    	   POST : pList - might be changed after inserting or deleting 
           Written by  : Instructor
    */
    
    NODE *processListManager (NODE *pList)
    {
    //  Local Definitions
    	char option;
    
    //  Statements
    	do
    	{
    		option = getOption();
    		switch(option)
    		{
    		    case 'L' : printList (pList);
    				       break;
    		    case 'S' : searchManager (pList);
    				       break;
    		    case 'I' : pList = insertManager (pList);
    				       break;
    		    case 'D' : // pList = deleteManager (pList);
    					   printf("INITIAL D!!!\n");
    				       break;
    		    case 'M' : printMenu ();
    				       break;
    		    case 'E' : printf("End of processing!\n");
                           break;
               default  :  break;
    		} 
    	} while (option != 'E');
    
    	return pList;
    
    } // processListManager 
    
    NODE *buildList (void)
    {
    // Local Declarations
    	NODE *pList;
    	NODE *pPre;
    	NODE *pCur;
    	DATA data;
    	FILE *spIn;
    	int result;
    
    // Statements
    	if ( ! ( spIn = fopen ( FILE_IN, "r" ) ) )	{
    		printf("Error opening file %s\n", FILE_IN);
    		exit(100);
    	}
    	pList = NULL;
    	while ( getData ( spIn, &data ) ) {
    		result = searchList(pList, &pPre, &pCur, data.key);
    		if ( !result )
    			pList = insertNode(pList, pPre, data);
    	}
    	return pList;
    } // buildList
    
    int getData ( FILE *spIn, DATA *data )
    {
    // Local Declarations
    	int result;
    
    // Statements
    	data->list.name = (char*)calloc(21, sizeof(char));
    	data->list.capital = (char*)calloc(16, sizeof(char));
    	result = fscanf ( spIn, "%2s %20[^;] %*c %15[^;] %*c %ld", 
    					  data->key, data->list.name, 
    					  data->list.capital, &(data->list.population) );
    	if ( result == 4 )
    		return 1;
    	else
    		return 0;
    } // getData
    
    NODE *insertNode  (NODE *pList, NODE *pPre, DATA item)
    {
    // Local Declarations
    	NODE *pNew;
    
    // Statements
    	if ( ! ( pNew = (NODE*)malloc(sizeof(NODE))))
    		printf("Memory is unavailable.\n"),
    			exit(200);
    	pNew->data = item;
    	if ( pPre == NULL ) {
    		pNew->link = pList;
    		pList = pNew;
    	}
    	else {
    		pNew->link = pPre->link;
    		pPre->link = pNew;
    	}
       return pList;
    } // insertNode
    
    int searchList (NODE *pList, NODE **pPre, NODE **pCur, char target[])
    {
    // Local Declarations
    	int found = 0;
    
    // Statements
    	*pPre = NULL;
    	*pCur = pList;
    	while (*pCur != NULL) {
    		if (strcmp ( target, (*pCur)->data.key) == 0 ){
    			found = 1;
    			break;
    		}
    		*pPre = *pCur;
    		*pCur = (*pCur)->link;
    	}
       return found;
    } // searchList
    
    void printList (NODE *pList)
    {
    // Local Declarations
    	NODE *pWalker;
    // Statements	
    	printf(	"== ====================\n"
    			"ID NAME                \n"
    			"== ====================\n");
    	pWalker = pList;
    	while ( pWalker ) {
    		printf("%s %-20s\n",
    			   pWalker->data.key, pWalker->data.list.name);
    		pWalker = pWalker->link;
    	}
    	printf( "=======================" );
       return;
    } // printList
    
    void insertCommas ( NODE *pWalker, char *population )
    {
    // Local Declarations
    	char printString[15];
    	char tempString[15];
    	char insertComma[] = ",";
    	int strLength;
    	int checkRmdr;
    	int i;
    // Statements
    	*printString = '\0';
    	sprintf(tempString, "%ld", pWalker->data.list.population);
    	strLength = strlen ( tempString );
    	checkRmdr = strLength % 3;
    	if ( checkRmdr > 0 ) {
    		strncat ( printString, tempString, checkRmdr );
    		strcat ( printString, insertComma );
    	}
    	for ( i = checkRmdr; i < (int) strlen ( tempString );  ) {
    		strncat ( printString, tempString + i, 3 );
    		i += 3;
    		strcat ( printString, insertComma );
    	}
    	printString[strlen(printString) - 1] = '\0';
    	strcpy (population, printString);
    	return;
    } // insertCommas
    
    void searchManager (NODE *pList)
    {
    // Local Declarations
    	NODE *pPre;
    	NODE *pCur;
    	char target[3];
    	char populationInCommas[15];
    
    // Statements
    	printf("Please enter a key: ");
    	scanf("%2s", target);
    	FLUSH;
    	if ( searchList ( pList, &pPre, &pCur, target ) ) {
    		insertCommas(pCur, populationInCommas);
    		printCountry(pCur, populationInCommas);
    	}
    	else
    		printf("Country could not be found. Please try again.");
       return;
    } // searchManager
    
    void printCountry ( NODE *list, char populationInCommas[] )
    {
    // Statements
    	printf(	"\n        ID: %s\n"
    				"      Name: %s\n"
    			    "   Capital: %s\n"
    				"Population: %s\n", list->data.key, 
    									list->data.list.name,
    									list->data.list.capital, 
    									populationInCommas);
    } // printCountry
    
    NODE *insertManager (NODE *pList)
    {
    // Local Declarations
    	NODE *pPre;
    	NODE *pCur;
    	DATA insertData;
    	int position;
    	char target[3];
    
    // Statements
    	printf("Please enter a key: ");
    	scanf("%2s", target);
    	FLUSH;
    	if ( ! ( searchList ( pList, &pPre, &pCur, target ) ) ) {
    		position = insertHandler ( &insertData, target );
    		
    		switch (position) {
    			case 1:	pPre = pList;
    					break;
    			case 2:	printf("MIDDLE POSITION\n");
    			case 3:	pPre->link = NULL;
    					break;
    		}
    		pList = insertNode( pList, pPre, insertData );
    	}
    	else
    		printf("That country ID already exists. Please try again.");
       return pList;
    }
    
    int insertHandler ( DATA *data, char target[] )
    {
    // Local Declarations
    	int position = 0;
    
    // Statements
    	if ( ! ( data->list.name = (char*)calloc(21, sizeof(char)))) {
    		printf("Memory Unavailable.\n");
    		exit(300);
    	}
    	if ( ! ( data->list.capital = (char*)calloc(16, sizeof(char)))) {
    		printf("Memory Unavailable.\n");
    		exit(400);
    	}
    	strcpy ( data->key, target );
    	printf("Enter the country's name: ");
    	scanf("%s", data->list.name);
    	FLUSH;
    	printf("Enter the country's capital: ");
    	scanf("%s", data->list.capital);
    	FLUSH;
    	printf("Enter the country's population: ");
    	scanf("%ld", &(data->list.population));
    	FLUSH;
    	while ( position < 1 || position > 3 ) {
    		printf("Where would you like to place this new country?\n"
    			   "Beginning(1), Middle(2), End(3): ");
    		scanf("%d", &position);
    		FLUSH;
    		if ( position < 1 || position > 3 )
    			printf("Incorrect option. Please try again.\n");
    	}
    			
       return position;
    } // insertHandler
    The output:
    Code:
                     LAB 8 - LINKED LISTS
    
    
                    **********************
                    *                    *
                    *   L - List         *
                    *   S - Search       *
                    *   I - Insert       *
                    *   D - Delete       *
                    *   M - Print Menu   *
                    *   E - Exit         *
                    *                    *
                    **********************
    
    Please enter the option: l
    == ====================
    ID NAME
    == ====================
    HU Hungary
    MX Mexico
    CN China
    NP Nepal
    MU Mauritius
    FR France
    ES Spain
    EG Egypt
    TW Taiwan
    GR Greece
    US United States
    AU Australia
    LI Liechtenstein
    RU Russia
    CA Canada
    MC Monaco
    BR Brazil
    IR Iran
    DO Dominican Republic
    FJ Figi
    =======================
    
    Please enter the option: i
    Please enter a key: IP
    Enter the country's name: Ippon
    Enter the country's capital: Haga
    Enter the country's population: 123123
    Where would you like to place this new country?
    Beginning(1), Middle(2), End(3): 1
    
    
    Please enter the option: l
    == ====================
    ID NAME
    == ====================
    HU Hungary
    IP Ippon
    MX Mexico
    CN China
    NP Nepal
    MU Mauritius
    FR France
    ES Spain
    EG Egypt
    TW Taiwan
    GR Greece
    US United States
    AU Australia
    LI Liechtenstein
    RU Russia
    CA Canada
    MC Monaco
    BR Brazil
    IR Iran
    DO Dominican Republic
    FJ Figi
    =======================
    
    Please enter the option:
    Last edited by RaDeuX; 12-06-2008 at 07:30 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your insertNode function has a little bit of special case code -- if pPre equals NULL, it inserts the given data at the beginning of the list. Perhaps you should utilize that special case.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    21
    Quote Originally Posted by tabstop View Post
    Your insertNode function has a little bit of special case code -- if pPre equals NULL, it inserts the given data at the beginning of the list. Perhaps you should utilize that special case.
    Exactly my thought. However, when I use this approach for some reason my new node does not come up at all.

    EDIT: Nevermind, for some reason it works fine now. Perhaps it's because I didn't give insertNode a variable to return the pList value to. Thanks again tabstop.
    Last edited by RaDeuX; 12-06-2008 at 07:42 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well good then. Don't forget to actually #include <stdlib.h> and <ctype.h> somewhere.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need urgent help in linked list
    By Cassandra in forum C++ Programming
    Replies: 4
    Last Post: 03-12-2009, 07:47 PM
  2. linked list question
    By brb9412 in forum C Programming
    Replies: 16
    Last Post: 01-04-2009, 04:05 PM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. store data from ifstream and store in link list
    By peter_hii in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2006, 08:50 AM
  5. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 02:48 PM