Reputation: 21
So firstly I have IntListInsert which simply populates the list
void IntListInsert(IntList L, int v)
{
assert(L != NULL);
struct IntListNode *n = newIntListNode(v);
if (L->first == NULL)
L->first = L->last = n;
else {
L->last->next = n;
L->last = n;
}
L->size++;
}
Then I have another sorting function If I simply call IntListInsert like this, it populates the lists properly, although not sorted.
void IntListInsertInOrder(IntList L, int v)
{
IntListInsert(L, v);
}
$./list 1 2 3
123
When I try an IF statement to handle empty lists, It only inserts the first object in the list...
void IntListInsertInOrder(IntList L, int v)
{
//check if list is empty
if (L->first == NULL) {
IntListInsert(L, v);
}
}
$./list 1 2 3
1
Greatly appreciate any input :)
Upvotes: 0
Views: 73
Reputation: 311048
The function IntListInsert
appends only one node in each its call.
When the passed list is empty the function sets the pointer first
of the list to the newly created node.
if (L->first == NULL)
L->first = L->last = n;
So after exiting the function L->first
is not equal to NULL
.
On the other hand the function IntListInsertInOrder
calls the function IntListInsert
only when the list is initially empty.
void IntListInsertInOrder(IntList L, int v)
{
//check if list is empty
if (L->first == NULL) {
IntListInsert(L, v);
}
}
So what you are doing is what you are getting.
Upvotes: 0
Reputation: 101
How do you call the IntListInsertInOrder function?
I think the problem is if (L->first == NULL)
is only true for the first input 1
.
After u assigned 1
to the list if (L->first == NULL)
is false.
Upvotes: 1
Reputation: 528
After you've inserted your first value, L->first is no more NULL and the if prevents from inserting other values ? The full code would be usefull
Upvotes: 0