# Making a doubly linked list

• 10-16-2002
mlupo
consider the following defs:
Code:

``` typedef int Position; /*definition for a node for linear linked list*/ typedef struct listnode{   struct listnode *Prev;   ListEntry *entry;   struct listnode *Next; }ListNode; /*definition for a linear linked list*/ typedef struct list{   ListNode *top;   Position position; }List;```
I'm trying to create a doubly linked list appending each node at the head of the list. I guess it's in the same fashion that I'd enqueue a node. But getting the Prev pointer in the algorithm to work is giving me a brain cramp. Below is what I've written thus far....it does seem to work but I still don't think I'm quite doing the right thing or taking all cases into account.
If someone could please critique I would be most grateful.
``` /***********************************************   Build  a complete list   PRE: None   POST: Creates and initializes a List. ************************************************/ void MakeList(List *L, ListEntry *item){   ListNode *tempNode;   tempNode = MakeListNode(item);     //case of empty list.   //else there is already at least 1 node.   if(!L->top){     tempNode->Prev=NULL;     L->top=tempNode;   }else{     tempNode->Next=L->top;     L->top=tempNode;     //tempNode->Prev=NULL;     L->top->Next->Prev=L->top;     L->position++;   }    }```
```Node *mylist; void prepend( Node *n ) {     n->next = mylist;     n->prev = NULL;     mylist->prev = n;     mylist = n; }```