468,306 Members | 1,138 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,306 developers. It's quick & easy.

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:

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

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:

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)
Please correct me.
See above. I didn't test it, though.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
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:

Thank you for your comments and this is the reorganized code:

This is the new code following your comments:

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;
insert 6 to this linked-list.

Based on your solution, the produced Linked-List will be
NodeA(7) -NodeB(6) -NULL;
I have posted a new solution and wish it is right.

Thank you for your comments.
-Daniel

Jan 24 '07 #5
In article <11**********************@k78g2000cwa.googlegroups .com>,
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:

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?

Ask your instructor to please show his students how to test their code
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:

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.
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
beacon
19 posts views Thread by beacon | last post: by
3 posts views Thread by Alien | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.