Hi all,
I've been having some difficulty with understanding doubly linked lists. I looked at the FAQ and the tutorial about doubly linked lists(which is very short by the way), and think I have a basic understanding. My problem is that I've been given some code, and been asked to tweak the singly linked list into a doubly linked list. So the following is the example provided in the tutorial:
Code:
n->prev = item; /* n's prev pointer */
 n->next = item->next; /* n's next pointer */
 n->next->prev = n; /* n's next->prev pointer */
 n->prev->next = n; /* n's prev->next pointer */
My understanding is that you must have both the previous pointer and the next pointer point to the value input by the user. If I'm wrong in the assumption, please correct me. This is a sample of the code I've been given:
Code:
void insert(listnodeptr *startptr, char value)
{
	listnodeptr newptr;	  /*pointer to new node*/
	listnodeptr previousptr; /*pointer to previous node in list*/
	listnodeptr currentptr;  /*pointer to current node in list*/

	newptr = malloc(sizeof(listnode)); /*allocate new space*/

	if(newptr != NULL){ /*is space available*/
		newptr->data = value; /*place choice in node*/
		newptr->nextptr = NULL; /*New node points to NULL*/
		previousptr = NULL;
		currentptr = *startptr;
		
		/*loop to find correct location in list*/
		while(currentptr != NULL && value > currentptr->data){
			previousptr = currentptr; /*walk through list*/
			currentptr = currentptr->nextptr;
		}/*end while*/

		/*insert new node at start of list*/
		if(previousptr == NULL){
			newptr->nextptr = *startptr;
			*startptr = newptr;
		
		}/*end if*/
		else{
			previousptr->nextptr = newptr;
			newptr->nextptr = currentptr;
		}/*end else*/
	}/*end if*/
	else{
		printf("%c not inserted. No memory available.\n", value);
	}
}/*end function insert*/
How is this not a doubly linked list? After malloc is used to create a new node, both the previous and next pointers are initialized to NULL, then inserted into the list. Then, both previous and next pointers are set to the value.

Any help would be greatly appreciated!