Talk about bending over backward to use a linked list when a binary search tree would easily solve the problem.
Let's start by figuring out which part of the task is giving you trouble. Do you know what a linked list is? Have you ever written one? Start by inserting a few numbers in a linked list without trying to get it sorted and without trying to remove duplicates. Post your code when you get that part done and we'll move on to the next stage.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>The trouble that i am having is how to incorporate the random number generator into a linked list
You already have a random number generator, it's as simple as doing this:
int i;
for ( i = 0; i < 10; i++ )
list = list_insert ( list, rand() % 5 + 1 );
So the random number part isn't a problem.
>and no i have never written a linked list
This is your biggest problem right now. First you need to figure out how to make a linked list, then you need to figure out how to make it sorted. Here's a really big hint:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *push_front ( struct node *list, int data )
{
struct node *p = malloc ( sizeof *p );
if ( p != NULL ) {
p->data = data;
p->next = list;
list = p;
}
return list;
}
int main ( void )
{
struct node *list = NULL;
struct node *save;
int i;
for ( i = 0; i < 10; i++ )
list = push_front ( list, rand() % 5 + 1 );
while ( list != NULL ) {
save = list->next;
printf ( "%d ", list->data );
free ( list );
list = save;
}
printf ( "\n" );
return 0;
}
This code inserts each new number onto the front of the list. It's a simple matter to walk through the list and find the sorted location. See if you can do it.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>I compiled it and it says that in line 11 that it can't concert from void * to struct node *
Well, we've now established that you're using C++ and not C. You should mention that explicitly in the future. Use this code instead:
#include <iostream>
using namespace std;
struct node {
int data;
node *next;
};
struct node *push_front ( node *list, int data )
{
node *p = new node;
p->data = data;
p->next = list;
list = p;
return list;
}
int main()
{
node *list = 0;
node *save;
for ( int i = 0; i < 10; i++ )
list = push_front ( list, rand() % 5 + 1 );
while ( list != 0 ) {
save = list->next;
cout<< list->data <<' ';
delete list;
list = save;
}
cout<<'\n';
}
>what do you mean find the sorted location
Say you have an empty list and you want to insert 5, you go ahead and do the same thing as my code by pushing 5 onto the front of the list. Then say you want to insert 9. You can't push 9 onto the front of the list because that's not sorted (assuming ascending order). So you walk through the list until the next node is the end of the list, or you find a node with a value greater than the new node and stick it there. I'm not going to write this for you, so you'd better get your creativity reflex in gear.
>i did not think that this was gunna be this hard...
Just wait until you get to multidimensional linked data structures. ;)
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401