Thread: Delete node

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    88

    Delete node

    I got the search problem figured out, and now am applying the same idea to a delete node function
    but the function fails to return to the main menu after it is done
    Code:
    //Node structure
    typedef struct node_d
    {
    char city[40];
    char state[3];
    char zipcode[6];
    double longitude;//double
    double latitude;//double
    long population;//long
    struct node_d *previous;
    struct node_d *next;
    }Node;
    
    //Header structure
    typedef struct
    {
    struct node_d *psHead;//might change this to node_d
    struct node_d *psTail;
    struct node_d *psCurrent;
    int node_count;
    }Header;
    Code:
    int Delete_Data(Header *psList,Node *list,Node *node, int z)
    {
    	int x, n = 1, indicator = 1; 
    	char answer[40], delete_[1], confirmation[1];
    	strcpy(confirmation,"y");
    	node = psList->psHead;
    	printf("\n\nWhat City or Zipcode would you like to delete?");
    	scanf("%s",&answer);
    	
    		if (isdigit(answer[0]) != 0)
    		{
    			//search for zip
    			for(x=0;x<z;x++)
    			{
    				if( strncmp( answer,(node->zipcode), 6) == 0 )
    				{
    				
    				system("cls");
    				printf("\nZipcode = %s   City = %s   State = %s",node->zipcode, node->city,node->state);
    				printf("\nPopulation = %d  Longitude = %-.2f  latitude = %-.2f\n",node->population,node->longitude, node->latitude);
    				indicator = 0;
    				printf("\nAre you sure you want to delete this record?");
    				printf("\npress 'y' for yes and 'n' for no\n");
    				scanf("%s",&delete_);
    				if(strcmp(delete_, confirmation) == 0)
    				{
    					node->next->previous = node->previous;
    					node->previous->next = node->next;
    					free(node);
    					printf("node deleted");
    					n = 0;
    				}
    				
    				}
    			node = node->next;
    
    			}
    			if(indicator !=0)
    			{
    				printf("\nRecord not found\n\n");
    			}
    
    		}
    		else 
    		{
    		//search for city
    		for(x=0;x<z;x++)
    		{
    			if( strncmp( answer,(node->city), 6) == 0)
    			{
    				
    				system("cls");
    				printf("\nZipcode = %s   City = %s   State = %s",node->zipcode, node->city,node->state);
    				printf("\nPopulation = %d  Longitude = %-.2f  latitude = %-.2f\n",node->population,node->longitude, node->latitude);
    				indicator = 0;
    				printf("\nAre you sure you want to delete this record?");
    				printf("\npress 'y' for yes and 'n' for no\n");
    				scanf("%s",&delete_);
    				if(strcmp(delete_, confirmation) == 0)
    				{
    					node->next->previous = node->previous;
    					node->previous->next = node->next;
    					//free(node);
    					printf("node deleted");
    					
    				}
    				
    			}
    			node = node->next;
    
    		}
    		if(indicator !=0)
    		{
    			printf("\nRecord not found\n\n");
    		}
    		}//end else
    
    return 0;
    }
    You guys have any ideas on why it does this?
    The last thing I get is the printed message "node deleted"
    and then crash

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > char answer[40], delete_[1], confirmation[1];
    > strcpy(confirmation,"y");

    You are leaving no room for the string terminator. delete_ and confirmation should be at least two chars:
    char answer[40], delete_[2], confirmation[2];

    Or alternately you could make them a single char:
    char delete_, confirmation;
    Then use:
    scanf("%c",&delete_);
    if(delete_ == confirmation)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Debugging my AVL tree program.
    By Nextstopearth in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 01:48 AM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM