A
askmatlab
Hello all:
I would like to insert a number into a linked list in ascending order.
Is the following function correct?
void insert(Node **node, int v)
{
Node *tmp = (Node *)malloc(sizeof(Node));
while(*node && (*node)->value < v) node = &(*node)->next;
tmp->value = v;
tmp->next = *node;
*node = tmp;
}
If this is correct, how to answer the following case:
Given a linked-list as follows:
NodeA(2) -> NodeB(4) -> NodeC(7) -> NULL.
Insert 5.
Then the new linked-list will become as follows if I understand
correctly.
NodeA(2) -> NodeB(4) -> NodeC(7) -> NULL
NodeD(5) -> NodeC(7)
Please correct me.
Thank you
-Daniel
I would like to insert a number into a linked list in ascending order.
Is the following function correct?
void insert(Node **node, int v)
{
Node *tmp = (Node *)malloc(sizeof(Node));
while(*node && (*node)->value < v) node = &(*node)->next;
tmp->value = v;
tmp->next = *node;
*node = tmp;
}
If this is correct, how to answer the following case:
Given a linked-list as follows:
NodeA(2) -> NodeB(4) -> NodeC(7) -> NULL.
Insert 5.
Then the new linked-list will become as follows if I understand
correctly.
NodeA(2) -> NodeB(4) -> NodeC(7) -> NULL
NodeD(5) -> NodeC(7)
Please correct me.
Thank you
-Daniel