Hello, Im trying to insert some integers to make a sorted list. I am using cursor-based.

Here is the function:

```
void insert_sorted(virtualheap *vh, list *l, int x)
{
int temp, *p;
if(vh->avail != -1)
{
temp = mymalloc(&(*vh));
vh->h[temp].elem = x;
for(p = l; *p != -1 && vh->h[*p].elem <= vh->h[temp].elem ; *p = vh->h[*p].next);
if(*p == -1)
*p = temp;
else
{
vh->h[temp].next = *p;
*p = temp;
}
}
else
printf("No heap available\n");
}
```

Example, I will input 5, 2 and 1, it will work perfectly but if I will input 5, 3 and 6 (it is not in descending order), only 6 will be in the list. It will only work perfectly if I will input numbers in descending order.

Variable p is supposed to traverse through the list and stop when it reaches the correct position. I need to make variable p a pointer because I need it to change the value of the one it is pointing to. Variable l is only supposed to hold the first array of the list. I will only change it if x is smaller than any of the elements in the list.

The problem is, when p is traversing, variable l will still get p's value. I dont know any way for p to get l's value without changing l.

And yeah, the list should be a pass-by address.

Can anyone help me..