bi-linked list based on one-way linked list

This is a discussion on bi-linked list based on one-way linked list within the C Programming forums, part of the General Programming Boards category; createbi, creates a bi-linked list out of the one-way linked list. I don't see how I do these assignments, without ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    123

    bi-linked list based on one-way linked list

    createbi, creates a bi-linked list out of the one-way linked list.
    I don't see how I do these assignments, without failing, when I have 2 types of struct.
    Please help.
    Code:
    void createbi (FOO **list, FOO1 **bilist)
    {
    	FOO *prev, *crnt, *ahd;
    	crnt=*list;
    	
    	while (crnt!=NULL)
    		crnt= crnt->next;
    	crnt->next=prev;
    	*bilist=crnt;
    	(*bilist)->num =crnt->num;
    	(*bilist)->prev=NULL;
    	(*bilist)->next=prev;
    	while (bilist!=list)
    	{
    		ahd=crnt->next;
    		(*bilist)->prev=ahd;
    		crnt->next=prev;
    		(*bilist)->next=prev;
    		(*bilist)->num=crnt->num;
    		prev=crnt;
    		crnt=ahd;
    	}
    
    (*bilist)->next=NULL;
    }
    Last edited by ronenk; 03-04-2005 at 04:47 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well first off, lose the second indirection and your code will become a lot cleaner.
    Code:
    void foo( struct sNode **sList, struct dNode **dList )
    {
        struct sNode sPtr = *sList;
        struct dNode dPtr = *dList;
        /* Much better already... */
    }
    Wait wait wait wait... Do you already have the second list built? (ie: allocated) If so this is something you can do in your sleep:
    Code:
    void foo( struct sNode **sList, struct dNode **dList )
    {
        struct sNode sPtr = *sList;
        struct dNode dPtr = *dList;
        /* Much better already... */
    
        for( ; sPtr; sPtr = sPtr->next, dPtr = dPtr->next )
        {
            dPtr->data1 = sPtr->data1;
            dPtr->data2 = sPtr->data2;
            ...
           dPtr->dataN = sPtr->dataN;
        }
    }
    That's all folks!

    If you don't have it allocated, then you do the exact same thing, except you allocate a node in there for dPtr, and link it to the double linked list you're building.

    Otherwise you need to explain what it is you're trying to do.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  3. Adding directory/file names to a linked list
    By thoseion in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 12:13 PM
  4. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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