PDA

View Full Version : Doubly Linked List???


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?

Jim R
03-25-2007, 06:00 PM
The part of insert() that you quoted doesn't initialize currentPtr, but maybe that gets done in the elided section. Presumably you want to set currentPtr = sPtr.

In addAfter(), I assume you want to copy the passed string into the new node, but that doesn't appear to happen, unless it's buried in the part that Yahoo turned into "...".