Thread: Help creating lists of pointers

  1. #1
    Registered User
    Join Date
    Oct 2004

    Help creating lists of pointers


    I have build a binary tree and each node of this tree contains a number of messages sent, message received and messages transmitted. What I want to do is to go through the tree and add the information of each node into a separated list (there's a different list for the 3 type of message).

    Now my tree is already build with the correct information and I know how to go through the tree in a fix order. My problem is when it comes to creating the lists. First, I have declared the three of them like this:

    #define MAX_MES_SNT 25          /* max number of messages sent           */
    #define MAX_MES_TRS 250            /* max number of messages transmited  */
    #define MAX_MES_REC 25          /* max number of messages received     */
    typedef struct pnode* PtrListItem;
    typedef struct pnode
         PtrNode ptrNode;     /* points on an element of the tree    */
         PtrListItem next;       /* points on the next listitem             */
    PtrListItem messSnt[MAX_MES_SNT+1];
    PtrListItem messTrs[MAX_MES_TRS+1];
    PtrListItem messRec[MAX_MES_REC+1];

    So far, so good I hope. Then, I start reading elements of the tree and this is where I'm bugged. Let's say the first node has 1 sent msg, 5 transmited msg and 2 received msg. I need to add a listitem to the list messSnt[1] (the ptrNode member of this new listitem will point on this node), add an a list item to messTrs[5] (the prtNode member of this new listitem will also points on this node) and same thing for the received msg. This way, once I finish going through the tree, I will have build every lists needed to print my reports.

    I started creating the function that build the lists and here's what it looks like (note that I started by doing it only for 1 kind of msg).

    void CreateLists(PtrNode childTree)
         if (childTree)
                    /* We need to go through the tree in in an ascending order */
                    /* Find the first element of the tree                                   */
                    /* Creates a new list item and allocates memory */
              PtrListItem PtrCur = (PtrListItem ) malloc (sizeof (Pnode));
              PtrCur->ptrNode = childTree;
                    /* Should add a pointer to the current node into the correct list... */
              messSnt[childTree->nbMessSnt] = PtrCur;
              PtrCur->next = NULL;
                    /* Do the right side of the tree */
    If would really appreciate if anyone could tell me how to actually add an item correctly to my lists and if I'm going in the right direction.
    If you need more information or more code, please let me know and I will post it.

    Thanks a lot for your time and support,

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    If I'm way off the mark, I appologize. But this sounds like what you want:
    void Place(PtrListItem list[], int index)
      PtrListItem list_node;
      list_node = malloc(sizeof *list_node);
      if (list_node == NULL)
      /* Front insertion. Modify this if you need a special ordering */
      list_node->ptrNode = root;
      list_node->next = list[index];
      list[index] = list_node;
    void AssignLists(PtrNode root)
      Place(messSnt, root->nbMessSnt);
      Place(messTrs, root->nbMessTrs);
      Place(messRec, root->nbMessRec);
    void CreateLists(PtrNode root)
      if (root == NULL)
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Thanks a lot Prelude, that is exactly what I was looking for !


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating a new record
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 05-28-2009, 03:56 PM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. pointers and linked lists in place of arrays
    By kordric in forum C++ Programming
    Replies: 8
    Last Post: 05-14-2008, 10:59 AM
  4. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  5. Creating an array of object pointers
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 10:01 PM