Linked List inside Linked List

This is a discussion on Linked List inside Linked List within the C++ Programming forums, part of the General Programming Boards category; Hello , I have small/big problem: how to implement Linked List inside Linked List ... Code: struct TList1 { int ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    3

    Linked List inside Linked List

    Hello,

    I have small/big problem: how to implement Linked List inside Linked List...

    Code:
    struct TList1
    {
         int value;
         
         TList1 * next;
    };
    
    typedef TList1 * wskList1;
    
    struct TList2
    {
         int value;
    
         wskList1 list;  /* List inside List */
    
         TList2 * next;
         TList2 * prev;
    };
    
    typedef TList2 * wskList2;
    I am looking for a piece of code: Add and Delete elements in List1, which is part of List2.

    My rubbish:
    Code:
    wskList2 AddR( wskList2 &Beg, wskList2 &End, wskList2 &Elem )
    {
    	Elem->next = NULL;
    	
    	if ( Beg == NULL )
    	{ 
    		Beg = Elem;
    		Elem->prev = NULL;
    		
    		Beg->list = NULL;
    	}
    	else
    	{
    		Elem->prev = End;
    		End->next = Elem;
    	}
    	
    	End = Elem;
    
    	return Beg;
    }
    
    wskList1 AddRev( wskList1 &Beg, wskList1 &Elem )
    {
    	if ( Beg == NULL )
    	{
    		Beg  = new TList1;
    		
    		Beg = Elem;
    		Beg->next = NULL;
    	}
    	else
    	{
    		Beg->next = Elem;
    		Elem->next = NULL;
    	}
    
    	return Beg;
    }
    
    //........
    
    wskList2 x;
    
    x->list = AddRev( x->list, RevElement )
    ... it doesn't work properly ;(

    Help Me!

  2. #2
    Registered User
    Join Date
    Apr 2005
    Posts
    30
    How good are you with templates? A good way to implement a linked list inside a linked list is though templates. That way you can just do something like

    Code:
    LinkedList< LinkedList<int> > List;

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, 02:19 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 05:40 PM
  5. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM

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