brian l
03-25-2007, 04:30 PM
I'm trying to make a doublly linked list then uses an addbefore and addafter function the return a pointer to a node. It works when I don't use the function but messes up when I use them
Here is my code some of my code
void insert(ListNodePtr *sPtr, char *string){
some code...( *sPtr is my main node)
.
.
.
while(currentPtr->prevPtr != NULL && (strcmp(string, currentPtr->word) prevPtr;
}
*sPtr = addAfter(currentPtr, string); //ADDING AFTER
}
.
.
.
}
Then here is the add function
ListNodePtr addAfter(ListNodePtr currentPtr, char *string)
{
ListNodePtr newPtr;
newPtr = malloc(sizeof(ListNode)); //allocating memory for new struct node
newPtr->word = (char *) malloc(sizeof(char)*(strlen(string) + 1)); // ALLOCATING MEMORY FOR STRING
if(newPtr == NULL || newPtr->word == NULL){
printf("\n\n***Could not allocate memory***\n\n");
exit(-1);
}
else{
newPtr->prevPtr = currentPtr;
newPtr->nextPtr = currentPtr->nextPtr;
currentPtr->nextPtr = newPtr;
return currentPtr;
}
}
Anyone see a problem?
Here is my code some of my code
void insert(ListNodePtr *sPtr, char *string){
some code...( *sPtr is my main node)
.
.
.
while(currentPtr->prevPtr != NULL && (strcmp(string, currentPtr->word) prevPtr;
}
*sPtr = addAfter(currentPtr, string); //ADDING AFTER
}
.
.
.
}
Then here is the add function
ListNodePtr addAfter(ListNodePtr currentPtr, char *string)
{
ListNodePtr newPtr;
newPtr = malloc(sizeof(ListNode)); //allocating memory for new struct node
newPtr->word = (char *) malloc(sizeof(char)*(strlen(string) + 1)); // ALLOCATING MEMORY FOR STRING
if(newPtr == NULL || newPtr->word == NULL){
printf("\n\n***Could not allocate memory***\n\n");
exit(-1);
}
else{
newPtr->prevPtr = currentPtr;
newPtr->nextPtr = currentPtr->nextPtr;
currentPtr->nextPtr = newPtr;
return currentPtr;
}
}
Anyone see a problem?