# Insert a number into a linked list in ascending order

Discussion in 'C++' started by askmatlab@gmail.com, Jan 24, 2007.

1. ### Guest

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:

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)

Thank you
-Daniel

, Jan 24, 2007

2. ### Victor BazarovGuest

wrote:
> I would like to insert a number into a linked list in ascending order.
>
> Is the following function correct?

Probably not, since you're not getting the right result with it :-/

>
> void insert(Node **node, int v)
> {
> Node *tmp = (Node *)malloc(sizeof(Node));

I suggest you *move* 'tmp->value = v;' statement here. And why are
you usuing 'malloc'? It's so much simpler with 'new':

Node *tmp = new Node;

And does your 'Node' class have a constructor? It should, probably.

> while(*node && (*node)->value < v) node = &(*node)->next;

> tmp->value = v;
> tmp->next = *node;

You're not making the "previous" node in the sequence aware of the
insertion. It has to be aware if you want your sequence to run
correctly. (I suppose you have a singly linked list here)

Node *prev = NULL;
while (*node && (*node)->value < v) {
prev = *node;
node = &(*node)->next;
}

if (prev)
prev->next = tmp;
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.

What if you insert 1?

>
> Then the new linked-list will become as follows if I understand
> correctly.
>
> NodeA(2) -> NodeB(4) -> NodeC(7) -> NULL
> NodeD(5) -> NodeC(7)
>
>

See above. I didn't test it, though.

V
--

Victor Bazarov, Jan 24, 2007

3. ### =?iso-8859-1?q?Erik_Wikstr=F6m?=Guest

On Jan 24, 3:44 pm, wrote:
> 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;
>
> }

Does not seem right, you forgot to attach the new node to the previous
one.

Given the follownig:

struct Node {
Node* next;
int value;
}

The algorithm would be something like:

void insert(Node* node, int v) {
Node* n = new Node; // use malloc if you want
while (node != 0 && node->next != 0 && node->next->value < v)
node = node->next;
n->value = v;
n->next = node->next;
node->next = n;
}

I think, I have not tried it.

--
Erik WikstrĂ¶m

=?iso-8859-1?q?Erik_Wikstr=F6m?=, Jan 24, 2007
4. ### DanielGuest

Hello Victor:

void insert(Node **node, int v) {

Node *tmp = new Node(v);
Node *prev = NULL;

while (*node && (*node)->value < v) {
prev = *node;
node = &(*node)->next;
}

if (prev) // the while loop runs at least once.
{
prev->next = tmp;
tmp->next = *node;
}
else // never enter into the while loop
{
tmp->next = *node;
*node = tmp;
}

} // end of function insert

Hopefully, this time it is correct.

Thank you again

Daniel, Jan 24, 2007
5. ### DanielGuest

Hello Erik:

The original code was written by me and I got it somewhere.

> Given the follownig:
>
> struct Node {
> Node* next;
> int value;
>
> }The algorithm would be something like:
>
> void insert(Node* node, int v) {
> Node* n = new Node; // use malloc if you want
> while (node != 0 && node->next != 0 && node->next->value < v)
> node = node->next;
> n->value = v;
> n->next = node->next;
> node->next = n;
>
> }I think, I have not tried it.

This solution doesn't work if you have a linked-list as follows:

NodeA(7) -> NULL;

NodeA(7) -> NodeB(6) -> NULL;

I have posted a new solution and wish it is right.

-Daniel

Daniel, Jan 24, 2007
6. ### Daniel T.Guest

In article <>,
wrote:

> 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;
> }

What happened when you tested it? For example, what happens with the
following main? (You may need to #include <cassert> )

int main()
{
Node* n = 0;
insert( &n, 2 );
assert( n->value == 2 );
assert( n->next == 0 );

insert( &n, 4 );
assert( n->value == 2 );
assert( n->next->value == 4 );
assert( n->next->next == 0 );

insert( &n, 7 );
assert( n->value == 2 );
assert( n->next->value == 4 );
assert( n->next->next->value == 7 );
assert( n->next->next->next == 0 );

insert( &n, 5 );
assert( n->value == 2 );
assert( n->next->value == 4 );
assert( n->next->next->value == 5 );
assert( n->next->next->next->value == 7 );
assert( n->next->next->next->next == 0 );

}

> 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)

You answer it however you think it should be answered. Does the problem
require the insert function to maintain a sorted list? What should the
insert function do if the list passed in wasn't sorted in the first
place?

so they will know, before turning it in, if it is correct.

I've tutored several people going to several different colleges, and it
always amazes me that not one of the teachers seem to bother teaching
their students how to test code for conformance to the spec.

Daniel T., Jan 24, 2007
7. ### Daniel T.Guest

"Victor Bazarov" <> wrote:

> > 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.

>
> What if you insert 1?

int main()
{
Node* n = 0;
insert( &n, 4 );
assert( n->value == 4 );
assert( n->next == 0 );

insert( &n, 1 );
assert( n->value == 1 );
assert( n->next->value == 4 );
assert( n->next->next == 0 );
}

None of the asserts fire with his original code.

Granted, the use of malloc in a c++ program is odd, and he would
probably be better off with some member-functions. However, his initial
code passed every test I could think of.

Daniel T., Jan 24, 2007