Thread: Linked List Deletion

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

    Linked List Deletion

    My cancel function stops working if I delete the node. Any hints you can give me as to why?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef char FName[30];
    typedef char LName[30];
    
    typedef struct PassengerInfo
    {
           FName   firstname;
           LName   lastname;
           struct PassengerInfo *Link;
    } FlightData;
    
                 typedef FlightData *FlightPointer;
                 FlightData *Passenger, *PassengerData;
    
    void Book(FlightData **PassengerData, char *first, char *last)
    {
         FlightData *Passenger, *Locate;
         
         // Allocate new nodes for Passenger with firstname & lastname and Link == NULL 
         Passenger = (FlightData*)malloc(sizeof(FlightData));
         strcpy(Passenger->firstname, first);
         strcpy(Passenger->lastname, last);
         Passenger->Link = NULL;
         
         // Check to see if PassengerData is empty.
         if ((*PassengerData) == NULL)
         {
               *PassengerData = Passenger;
         }
         
         else
         {
             // Locate last node
             Locate = *PassengerData;
             
             while (Locate->Link != NULL)
             {
                   Locate = Locate->Link;
             }
             
             // Link node to end of list.
             Locate->Link =  Passenger;
         }
    }
    
    void Cancel(FlightData **PassengerData, char *first, char *last)
    {
         FlightData *CurrentNode, *Temporary;
         CurrentNode = NULL;
         Temporary = NULL;
         
         // Do nothing if PassengerData is empty
         if ((*PassengerData) == NULL)
         {
    		printf("\nThere are no passengers booked for this flight.\n");
         }
    
    	if(strcmp(((*PassengerData)->firstname), first) == 0)
    	{
    		if(strcmp(((*PassengerData)->lastname), last) == 0)
         		{
    			Temporary = (*PassengerData);
                	(*PassengerData) = (*PassengerData)->Link;
                	free(Temporary);
                	printf("\nBooking has been cancelled for %s, %s.\n", last, first);
    		}	
         }
    
    	// Initialize pointers and make them point to first node.
          CurrentNode = (*PassengerData);
    	
    	while(CurrentNode->Link != NULL)
    	{
    		if(strcmp((CurrentNode->Link->firstname), first) != 0)
    		{
    			CurrentNode = CurrentNode->Link;
    		}
    	}
    	
    	if(CurrentNode->Link == NULL && (strcmp((CurrentNode->Link->firstname), first) != 0))
    	{
    		printf("\nThat passenger is not booked for this flight.\n");
    	}
    	
    	Temporary = CurrentNode->Link;
    	CurrentNode->Link = CurrentNode->Link->Link;
    	free(Temporary);
    	printf("\nBooking has been cancelled for %s, %s.\n", last, first); 
    }
    
    FlightData *Confirm(char *first, char *last, FlightData *PassengerData)
    {
              FlightData *Passenger;
              
              Passenger = PassengerData;
              
              while (Passenger != NULL)
              {
                    if (strcmp((Passenger->firstname), first) == 0)
                    {
    				if(strcmp((Passenger->lastname), last) == 0)
    				{
    					printf("\n%s, %s is booked on this flight.\n", last, first);
                          		break;
    				}
    				
    				else
                    		{
    					Passenger = Passenger->Link;
                    		}
              		}
    		}
              
              if(Passenger == NULL)
              {
    			printf("\n%s, %s is not booked on this flight\n", last, first);
    	    }
    }
    
    void Display(FlightData *PassengerData)
    {
         FlightData *Passenger;
         
         printf("\nPassenger List: \n");
         
         Passenger = PassengerData;
         
         while (Passenger != NULL)
         {
               printf("\n%s, %s\n", Passenger->firstname, Passenger->lastname);
               Passenger = Passenger->Link;
         }
         
    }
    
    int main(void)
    {
        FILE* ifp;
        FlightData *PassengerData;
        
        PassengerData = NULL;
        
        	char first[30];
    	char last[30];
    	char action[15];
    	char filein[25];
    	char* remove;
    	
    	printf("Please enter the name of the file to be read.\n");
    	fgets(filein, sizeof(filein), stdin);
    	
    	if((remove = strchr(filein, '\n')) != NULL)
          {
    		*remove = '\0';
          }
    	
    	ifp = fopen(filein, "r"); 
    	
    	while (!feof(ifp))
    	{
              fscanf(ifp, "%s\n", &action);
              
              if (strcmp(action, "BOOK") == 0)
              {
                    fscanf(ifp, "%s %s", &first, &last);
                    Book(&PassengerData, first, last);
              }
              
              else if (strcmp(action, "CANCEL") == 0)
              {
                   fscanf(ifp, "%s %s", &first, &last);
                   Cancel(&PassengerData, first, last);
                         
              }
              
              else if (strcmp(action, "CONFIRM") == 0)
              {
                   fscanf(ifp, "%s %s", &first, &last);
                   Confirm(first, last, PassengerData);
              }
              
              else if (strcmp(action, "DISPLAY") == 0)
              {
                   Display(PassengerData);
              }
              
              
        } 
        
    	system("pause");
    	return 0;
    }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    A few things,

    * Don't use feof() to control a loop
    * Don't cast malloc()
    * Check the value of the pointer returned by malloc() before dereferencing it (ie it's not NULL)
    * Check the return value of fopen() to ensure it didn't failed (ie, not NULL)
    * Stick to spaces OR tabs for indenting not both
    * Don't typedef arrays, it's confusing as hell

    Code:
    if(CurrentNode->Link == NULL && (strcmp((CurrentNode->Link->firstname), first) != 0))
    Naughty, I think you mean CurrentNode->Link != NULL && ...

    I assume FlightData.Link is a pointer to the next node in the list? Other than that, Cancel() is a bit confusing and I'm tired
    Have you tried using a debugger?
    Last edited by zacs7; 04-20-2008 at 05:30 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM