Today I started reading about linked lists and I decided to make a program that asks the user how many random numbers wishes to create; then it shows all those random numbers in an unsorted way; right after that it shows the numbers sorted.
At first I did this with arrays and it was pretty easy; but with the linked list I've got no idea of how can I sort all this numbers... here is the code, hope you understand the question!
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
struct number
{
int random_number;
number* p_next_number;
};
number* p_list_numbers = NULL;
number* get_new_number(int i)
{
number* p_number = new number;
p_number->random_number = i;
p_number->p_next_number = p_list_numbers;
p_list_numbers = p_number;
return p_number;
}
void show_numbers(int i)
{
int count_numbers = 0;
while (p_list_numbers != NULL)
{
cout<< p_list_numbers->random_number;
if (count_numbers < (i-1))
{
cout<< ", ";
} else
cout << ".";
p_list_numbers = p_list_numbers->p_next_number;
count_numbers++;
}
}
void order_numbers(int i)
{
int smaller_number = p_list_numbers->random_number;
number* p_smaller_number = p_list_numbers->p_next_number;
for(int j = 0; j < i; j++)
{
p_list_numbers = p_list_numbers->p_next_number;
if (smaller_number > p_list_numbers->random_number)
{
smaller_number = p_list_numbers->random_number;
p_smaller_number = p_list_numbers->p_next_number;
}
}
/* in theory now smaller_number is the smallest number on
the list and p_smaller_number is a pointer pointing to where
the smallest number is, but what do I have to do now? */
}
int main()
{
longint times;
cout<<"How many numbers do you wish me to order? ";
cin>>times;
srand(time(NULL));
for (int i = 0; i < times; i++)
{
int random = rand() % 100;
get_new_number (random);
}
cout<< "These are the original numbers: ";
show_numbers(times);
order_numbers(times);
cout<< "\nThese are the ordered numbers: ";
show_numbers(times);
}
And is there a way of doing it without arrays? Because I would like to order MANY numbers, maybe a million (when I did this program with arrays it couldn't create 1 million numbers, but with linked list it could; I actually was able to create even 10 million random numbers and save them in a linked list wich took almost 18 minutes)
It is not good to store trivial types in linked lists. On 64bit machine you use trice as much memory as you would with array. In your case you should consider deque to store your numbers.
Nevertheless, best approach to sort linked list is mergesort. It is more complex than insertion sort, but it has good asymptomatic and will be way faster in your case.
Perfect!!! Thank you very much!! One last question, as I'm using new memory all the time I'm creating a number, do I need to use the delete funciton? if so, when and how? And what happens if I don't delete? do I ran out of RAM memory or something like that?
No idea what's that... as you can guess I just started with c++ and don't know much... so even after the program is closed, that memory is still being used?
If I shutdown my computer that memory will be freed... right?