Making a doubly linked list

This is a discussion on Making a doubly linked list within the C Programming forums, part of the General Programming Boards category; consider the following defs: Code: typedef int Position; /*definition for a node for linear linked list*/ typedef struct listnode{ struct ...

  1. #1
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72

    Making a doubly linked list

    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.
    Thanks in advance.

    Code:
    /***********************************************
      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++;
      }    
    }
    NEVER PET YOUR DOG WHILE IT'S ON FIRE!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    Node *mylist;
    
    void prepend( Node *n )
    {
        n->next = mylist;
        n->prev = NULL;
        mylist->prev = n;
        mylist = n;
    }
    This code assumes you have a global list pointer.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Duplicating value of pointer to linked list
    By zephyrcat in forum C Programming
    Replies: 14
    Last Post: 01-22-2008, 03:19 PM
  2. Small help on doubly linked list.
    By Depthcharge101 in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2007, 09:31 AM
  3. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  4. 1st Class LIST ADT
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2001, 07:29 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21