# Plz help with placing node in a data structure

• 01-31-2003
slopeski0007
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;           }   }```
• 01-31-2003
Shiro
> 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

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

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);```
• 01-31-2003
Magos
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.
• 01-31-2003
slopeski007
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
• 01-31-2003
Shiro
>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++;
• 01-31-2003
slopeski007
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