I am having trouble with this function to bubble sort a linked list. I am losing nodes. Could you please help me understand what I am doing wrong and get this right?

Thanks

void bubble_sort(item *node){ item *first_prev,*first,*second,*temp; int i, j, n = 1; bool sorted = false; //make these 3 pointers point to the head of the list temp = node; first = node; first_prev = node; //count the number of nodes in the list while (first->next != NULL) { n++; first= first->next; } for (i = 1; i <= n; i++) { first = temp; first_prev = temp; second = first->next; for (j=1; j<=(n-i); j++) { if (first->val > second->val) { if (first == temp) { first->next = second->next; second->next = first; temp = second; first = second; } else { first->next = second->next; second->next = first; first_prev->next = second; first = second; } } first_prev = first; first = first->next; second = first->next; } } sorted = true; PrintList();}

I don't see a problem with your bubble sort algorithm. However, keep in mind that after the sorting, 'temp' points to the first node in the sorted list. 'node' might no longer point to the start of the sorted linked list, but will very likely point to another node in the sorted linked list.

After performing the sort, you need to set temp as the new start node of the linked list.

>> 'temp' points to the first node in the sorted list
>> you need to set temp as the new start node of the linked list.

Outside of the loop, after the sorting is over, I set :
node = temp;
first = node;

But still not working. I can't see all nodes sorted.

void bubble_sort(item *node){ item *first_prev,*first,*second,*temp; int i, j, n = 1; bool sorted = false; //make these 3 pointers point to the head of the list temp = node; first = node; first_prev = node; //count the number of nodes in the list while (first->next != NULL) { n++; first= first->next; } for (i = 1; i <= n; i++) { first = temp; first_prev = temp; second = first->next; for (j=1; j<=(n-i); j++) { if (first->val > second->val) { if (first == temp) { first->next = second->next; second->next = first; temp = second; first = second; } else { first->next = second->next; second->next = first; first_prev->next = second; first = second; } } first_prev = first; first = first->next; second = first->next; } } sorted = true; node = temp; first = node; //PrintList(node);}

>>>then call it like this:
> ...
> item* root = new item;
> ...
> root = bubble_sort( root );
> ...

Isn't this more C++ synthax? With the "new" keyword.
It's not working.
I am getting the following error:
\linkedlistexercice\main.cpp(342) : error C2556: 'item *bubble_sort(item *)' : overloaded function differs only by return type from 'void bubble_sort(item *)'

ah, yes, it's C++ - but since you have a 'cpp' file I guess you do C++ and then 'new' is the recommended way to allocate memory.

About the error: I guess you somewhere have a function prototype declaration (maybe in a header or in 'main.cpp') for 'bubble_sort'm somehow like this:

We can't guess what your code looks like, so any suggestions we make will likely not work.

As I asked before, could you please post the complete code here. That way, we can make the correct suggestion right away, and immediately solve your problem.

Just a little worry that I have. I am using malloc() to allocate memory line 96:, I know I should free() the memory to eventually avoid memory leak. But how come I am getting a run-time error if I leave line 120: free(NewItem) in and nothing happens when I comment that out. It just runs ok.

>> But how come I am getting a run-time error if I leave line 120: free(NewItem) in and nothing happens when I comment that out. It just runs ok.

The memory you allocate is still used after the InsertItem function ends. You've placed the node in the linked list.

The free should be done when you remove the node from the linked list and when you destroy the entire linked list. So, it should be in your DeleteItem function, and at the end of main, you should empty the entire list.

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see soâ€¦

Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks. Concludes by examining the means of securing and protecting critical systems and infâ€¦