Plz help with placing node in a data structure

This is a discussion on Plz help with placing node in a data structure within the C Programming forums, part of the General Programming Boards category; This funtion halts the program. I'm trying to organize a list of nodes full of city data. They have to ...

  1. #1
    slopeski0007
    Guest

    Plz help with placing node in a data structure

    This funtion halts the program. I'm trying to organize a list of nodes full of city data. They have to be in ascending order of Zip code. Any help with this would be great. I've racked my brain and realize it's a very simple problem. Thanks.

    Scott

    Code:
    struct SListHeader{
    	struct SListNode *psHead;
       struct SListNode *psTail;
       int iNodeCount;
       };
    
    struct SListNode{
    	char City[41];
       char State[3];
       char Zip[6];
       double Longitude;
       double Latitude;
       long Population;
       struct SListNode *Next;
       struct SListNode *Prev;
       };
    
    void vAddListNode (struct SListHeader *psList, struct SListNode *psNode)
    	{
       struct SListNode *psNext;
       psNext = mallloc (sizeof(struct SListNode));
       psList->iNodeCount+1;
       if (psNode->Zip > psList->psHead->Next->Zip)
       	{
          psNode->Next = psNext;
          psNode->Prev = psNext-Prev;
          psNode->Next->Prev = psNext;
          psNode->Prev->Next = psNext;
       	}
       }

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    > psList->iNodeCount+1;

    What is the meaning of this?

    > psNode->Prev = psNext-Prev;

    Don't do this with pointers. Or did you mean

    psNode->Prev = psNext->Prev;

    In that case, psNext->Prev does not point to anything. You should first initialize the psNext node.

    Only if

    psNode->Zip > psList->psHead->Next->Zip

    you add the node, but you don't do that if

    psNode->Zip =< psList->psHead->Next->Zip.

    Your algorithm looks not complete to me. What you could do, in a kind of pseudo:

    Code:
    /* Add a note to a list */
    
    new_node = initialise_new_node()
    
    /* Check if node must be put after the tail */
    if (new_node->element > list_tail->element)
        add_element_after_tail (list_tail, new_node);
        return;
    
    /* Check if node must be put before the head */
    if (new_node->element < list_head->element)
        add_element_before_head (list_head, new_node);
        return;
    
    /* Else the new node must be inserted in the list, search for the
    first element which is larger than the element of new node. The 
    new node must be inserted before that element. */
    
    list_ptr = find_first_larger_element_position (list, new_node);
    insert_new_node (list_ptr, new_node);

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I'm surprised it compiles at all:
    Code:
    mallloc   //Check spelling
    
    psNext-Prev   //As far as I know, minus:s in names are forbidden... You mean a -> right?
    If you want to insert it sorted, you have to traverse the list until you find the right place to insert it at.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    7

    Sorry about the typos

    Those were two typos. I don't have the program with me...just a printout.

    iNodeCount is just an intiger counter. It keeps track of how many nodes have been read in the loop.

    Thanks for your help so far. I can't wait to try it out.

    Scott

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >It keeps track of how many nodes have been read in the loop.

    Then this

    > psList->iNodeCount+1;

    Doesn't do much, there is no assignment in it. It should be:

    psList->iNodeCount += 1;

    Or without an assignment but with the ++ operator:

    psList->iNodeCount++;

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    7
    As far as I can tell the iNodeCount works. Actually, I set it to zero in another funtion. Borland gets to the

    psNode->Next->Prev = psNext;

    line before it stops. I'm just trying to figure out the pointer logic mostly I think.

    Scott

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak
    By jtullo in forum C Programming
    Replies: 7
    Last Post: 12-11-2006, 11:45 PM
  2. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Big help in Astar search code...
    By alvifarooq in forum C++ Programming
    Replies: 6
    Last Post: 09-24-2004, 12:38 PM
  5. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 11:36 AM

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