In the code I provided in the zip file, I already have this prototype for a double linked list.
Code:
struct listNode {
char data;
struct listNode *nextPtr;
struct listNode *prevPtr;
Now lets take the Insert function.
Code:
void insert( ListNodePtr *sPtr, char value )
{
ListNodePtr newPtr;
ListNodePtr previousPtr;
ListNodePtr currentPtr;
newPtr = malloc(sizeof( ListNode ) );
if ( newPtr != NULL ) {
newPtr->data = value;
newPtr->nextPtr = NULL;
previousPtr = NULL;
currentPtr = *sPtr;
while ( currentPtr != NULL && value > currentPtr-> data) {
previousPtr = currentPtr;
currentPtr = currentPtr-> nextPtr;
}
if ( previousPtr == NULL ) {
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
}
else {
previousPtr-> nextPtr = newPtr;
newPtr->nextPtr = currentPtr;
}
}
else {
printf( "%c not inserted. No memory available.|n", value );
}
}
Its very obvious to me that this is the part I have to change, and yet this is the part that I dont understand. I understand the rest of the program and the rest of the function insert, but please explain to me what the heck is going on in the following code. It seems weird to think about memory locations pointing to memory locations of memory locations and etc.
Very confusing to me.
Code:
previousPtr = NULL;
currentPtr = *sPtr;
while ( currentPtr != NULL && value > currentPtr-> data) {
previousPtr = currentPtr;
currentPtr = currentPtr-> nextPtr;
}
if ( previousPtr == NULL ) {
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
}
else {
previousPtr-> nextPtr = newPtr;
newPtr->nextPtr = currentPtr;
}