Thread: insert linked list - segment violation

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    25

    insert linked list - segment violation

    what is wrong with this function? This function insert a new node in the linked list, but it'not works.

    Code:
    void insertFrontierAstar ( FrontierNodePtr *headPtr, TreeNodePtr ptr )
    {
    	FrontierNodePtr currentPtr, previousPtr;
    	
    	FrontierNodePtr newPtr;
    
    	newPtr = malloc ( sizeof ( FrontierNode ) );
    
    	if ( newPtr == NULL )                            
    	{
    		printf( "can't allocate memory for FrontierNodePtr\nexit program\n" );
    		exit ( 3 );
    	}
    
    	else
    	{
    		newPtr->leaf = ptr;
    		newPtr->next = NULL;
    
    		previousPtr = NULL;
    		currentPtr = *headPtr;
    
    		while ( currentPtr != NULL && currentPtr->leaf->sum < newPtr->leaf->sum )
    		{
    			previousPtr = currentPtr;
    			currentPtr = currentPtr->next;
    		}
    
    		if ( previousPtr == NULL )
    		{
    			newPtr->next = *headPtr;
    			*headPtr = newPtr;
    			return;
    		}
    
    		if ( currentPtr->leaf->sum > newPtr->leaf->sum )
    		{
    			previousPtr->next = newPtr;
    			newPtr->next = currentPtr;
    		}
    
    		if ( currentPtr->leaf->sum == newPtr->leaf->sum )
    		{
    			while ( currentPtr->leaf->g >= newPtr->leaf->g )
    			{
    				previousPtr = currentPtr;
    				currentPtr = currentPtr->next;
    			}
    
    			previousPtr->next = newPtr;
    			newPtr->next = currentPtr;
    
    		}
    
    
    
    
    
    	}
    
    
    
    }
    thank you in advance
    Code:
    # define EVERYTHINK 0
    int main ( void )
    { 
       while ( C_learning )
                 english_learning ( );
       return EVERYTHINK;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    		if ( currentPtr->leaf->sum > newPtr->leaf->sum )
    		{
    			previousPtr->next = newPtr;
    			newPtr->next = currentPtr;
    		}
    
    		if ( currentPtr->leaf->sum == newPtr->leaf->sum )
    		{
    			while ( currentPtr->leaf->g >= newPtr->leaf->g )
    			{
    				previousPtr = currentPtr;
    				currentPtr = currentPtr->next;
    			}
    
    			previousPtr->next = newPtr;
    			newPtr->next = currentPtr;
    
    		}
    This section can be entered with currentPtr == NULL - which will cause segmentation fault, of course.

    --
    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.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    25
    thank You Mats, for quick response. I' will check it
    Code:
    # define EVERYTHINK 0
    int main ( void )
    { 
       while ( C_learning )
                 english_learning ( );
       return EVERYTHINK;
    }

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Before dereferencing any pointers, be sure to check them for NULL!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    25
    well, i learn my lesson, thank you Mats (again) and Elysia, you are the best,
    here is my function, completed and running.

    Code:
    void insertFrontierAstar ( FrontierNodePtr *headPtr, FrontierNodePtr *tailPtr, TreeNodePtr ptr )
    {
    	FrontierNodePtr currentPtr, previousPtr;  
    	FrontierNodePtr newPtr;
    
    	newPtr = malloc ( sizeof ( FrontierNode ) );
    
    	if ( newPtr == NULL )                             
    	{
    		printf( "can't allocate memory for FrontierNodePtr\nexit program\n" );
    		exit ( 3 );
    	}
    
    	else
    	{
    		newPtr->leaf = ptr;
    		newPtr->next = NULL;
    
    		if ( *headPtr == NULL )
    		{
    			newPtr->next = *headPtr;
    			*headPtr = newPtr;
    		}
    
    		else
    		{
    			previousPtr = NULL;
    			currentPtr = *headPtr;
    
    			if ( currentPtr->leaf->sum > newPtr->leaf->sum )
    			{
    				newPtr->next = *headPtr;
    				*headPtr = newPtr;
    			}
    
    			else if ( currentPtr->leaf->sum < newPtr->leaf->sum )
    			{
    				while ( currentPtr != NULL && currentPtr->leaf->sum < newPtr->leaf->sum )
    				{
    					previousPtr = currentPtr;
    					currentPtr = currentPtr->next;
    				}
    
    				if ( currentPtr == NULL )
    				{
    					( *tailPtr )->next = newPtr;
    					*tailPtr = newPtr;
    				}
    
    				else if ( currentPtr != NULL && currentPtr->leaf->sum > newPtr->leaf->sum )
    				{
    					previousPtr->next = newPtr;
    					newPtr->next = currentPtr;
    				}
    
    				else if ( currentPtr != NULL && currentPtr->leaf->sum == newPtr->leaf->sum )
    				{
    					while ( currentPtr != NULL && currentPtr->leaf->sum == newPtr->leaf->sum
    							&& currentPtr->leaf->g >= newPtr->leaf->g )
    					{
    						previousPtr = currentPtr;
    						currentPtr = currentPtr->next;
    					}
    
    					if ( currentPtr == NULL )
    					{
    						( *tailPtr )->next = newPtr;
    						*tailPtr = newPtr;
    					}
    
    					else
    					{
    						previousPtr->next = newPtr;
    						newPtr->next = currentPtr;
    					}
    				}
    			}
    
    			else
    			{
    				while ( currentPtr != NULL && currentPtr->leaf->sum == newPtr->leaf->sum
    							&& currentPtr->leaf->g >= newPtr->leaf->g )
    				{
    					previousPtr = currentPtr;
    					currentPtr = currentPtr->next;
    				}
    
    				if ( currentPtr == NULL )
    				{
    					( *tailPtr )->next = newPtr;
    					*tailPtr = newPtr;
    				}
    
    				else if ( previousPtr == NULL )
    				{
    					newPtr->next = *headPtr;
    					*headPtr = newPtr;
    				}
    
    				else
    				{
    					previousPtr->next = newPtr;
    					newPtr->next = currentPtr;
    				}
    			}
    		}
    
    	}
    
    
    
    }
    Code:
    # define EVERYTHINK 0
    int main ( void )
    { 
       while ( C_learning )
                 english_learning ( );
       return EVERYTHINK;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. Duplicating value of pointer to linked list
    By zephyrcat in forum C Programming
    Replies: 14
    Last Post: 01-22-2008, 03:19 PM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 07:47 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM