468,306 Members | 1,138 Online

# Insert a number into a linked list in ascending order

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 '07 #1
6 19570 as*******@gmail.com 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:

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
--
Jan 24 '07 #2
On Jan 24, 3:44 pm, askmat...@gmail.com 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

Jan 24 '07 #3
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:)

Jan 24 '07 #4
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

Jan 24 '07 #5
as*******@gmail.com 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:

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.
Jan 24 '07 #6
"Victor Bazarov" <v.********@comAcast.netwrote:
If this is correct, how to answer the following case:

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.
Jan 24 '07 #7

### This discussion thread is closed

Replies have been disabled for this discussion.

### Similar topics

 5 posts views Thread by Darryl B | last post: by 6 posts views Thread by massimo | last post: by reply views Thread by Maurice | last post: by 2 posts views Thread by PRadyut | last post: by 4 posts views Thread by Peter Schmitz | last post: by 6 posts views Thread by Joe Estock | last post: by 19 posts views Thread by beacon | last post: by 3 posts views Thread by Alien | last post: by 5 posts views Thread by WarmDismalAgony | last post: by reply views Thread by ravipankaj | last post: by reply views Thread by ravipankaj | last post: by reply views Thread by NPC403 | last post: by reply views Thread by slotstar | last post: by 5 posts views Thread by isladogs | last post: by reply views Thread by Swartbj | last post: by reply views Thread by Teichintx | last post: by 1 post views Thread by Aftab Ahmad | last post: by reply views Thread by Dolfinwu | last post: by