# NUCLEAR RABBIT

Members

540

318 Neutral

• Rank
1. ## Bubble Sort Algorithm Review

Thank you, I appreciate the help! :D I like your suggestions
2. ## Bubble Sort Algorithm Review

Hello!   So I wanted to create a bubble sort algorithm and I have it working properly, I was just wondering if anyone could take a look at my code and give me any advice/suggestions on making it more efficient or if I did anything I should avoid doing. I would really appreciate any tips! I know that typically the bubble sort algorithm shouldn't print out each pass thru or maybe it shouldn't display anything at all, but I just wanted to add those print statements for testing purposes and so that I could see that the algorithm was working properly /////////////////////////////////// // // // Bubble Sort Algorithm // // --------------------- // // // // Bubble Sort Algorithm // // using C++ // // // // Date: 5/12/16 // // // /////////////////////////////////// #include <iostream> #include <string> #include <vector> #include <iterator> //========================================================================================== template <typename T> void displayList(std::vector<T> theList) { if (theList.size() == 0) std::cout << "*** Cannot display empty list ***\n\n"; else { int counter = 1; std::vector<T>::iterator listIter; for (listIter = theList.begin(); listIter != theList.end(); listIter++) { // checks if a comma should be placed after an item if (listIter != theList.end()) { if (counter == theList.size()) std::cout << *listIter << "\n"; else std::cout << *listIter << ", "; } counter++; } } } //========================================================================================== template <typename T> void bubbleSort(std::vector<T> & theList) { if (theList.size() == 0) { std::cout << "BubbleSort Begin:\n" << "----------------------------\n" << "| *Error: List is empty\n" << "----------------------------\n\n"; } else { bool sorted = false; bool altered_list; int passthru_counter = 1; int counter = 0; std::vector<T>::iterator listIter; std::cout << "Intial List Order: "; displayList(theList); std::cout << "\nBubbleSort Begin:\n-------------------------------------------------\n"; while (sorted == false) { counter = 0; altered_list = false; for (listIter = theList.begin(); listIter != theList.end(); listIter++) { // if the list was stepped thru and no items were out of place if (counter + 1 < theList.size()) { if (theList[counter] > theList[counter + 1]) { T temp = theList[counter]; theList[counter] = theList[counter + 1]; theList[counter + 1] = temp; altered_list = true; std::cout << passthru_counter << " pass thru:\t"; displayList(theList); passthru_counter++; break; } else counter++; } else { if (altered_list == false) { sorted = true; break; } } } } std::cout << "-------------------------------------------------\n"; std::cout << "*** The list is sorted\n"; std::cout << "*** # of pass thrus to sort = " << passthru_counter << "\n"; std::cout << "-------------------------------------------------\n\n"; } } //========================================================================================== int main() { std::vector<std::string> testList; // test values testList.push_back("lol"); testList.push_back("omg"); testList.push_back("wtf"); testList.push_back("kek"); testList.push_back("btw"); testList.push_back("fyi"); bubbleSort(testList); system("PAUSE"); return 0; }
3. ## Memory Allocation

lol       You're right, that is an issue. However, I dont think thats it will solve this particular issue with my program though because when I call pop(), I make sure the function is not empty so return NULL isnt ever called. I even tried changing return NULL to return -1 and the same issue happened. Everything is working properly with no crash up until I add the free(temp) statement in the pop() method. If I uncomment that statement, it works perfect besides the fact that the memory leak isnt solved when popping.   Will node solve your issue but FYI, the correct solution in this case is quite simple: template<typename T> T returnDefault(void) { return T(); } This will return a specified default value for all types (int = 0, T* = nullptr, custom type = default-ctor, ...), so just replace return NULL by return T() and you should be fine in this regard.     Thank you, I never learned this       Yes, this is the problem, you should be calling malloc(sizeof(struct Node)) .   However, you can make life easier using new and delete, like this // in push() Node* temp = new Node;   // later, in pop() delete temp; And, I just noticed, in pop(), why are you calling malloc() on temp again, then immediately assigning temp to headNode?  Don't malloc temp in pop().   You need to understand memory allocation a little more, then you should be able to tackle this problem fine.     Yeah I def needed to learn about memory allocation more haha I looked up some tutorials and watched some video explanations and I think I am understanding it better now.     PS - Thank you to everyone that helped me out, I got it working now!

5. ## Memory Allocation

yes isEmpty() is working correctly and yes the objects are created using malloc.       which portion were you referring to?
6. ## Memory Allocation

Hello,   So I am editing a project I had where I was creating a queue. In my pop() method I am having an issue and I am not sure why. I am trying to free the memory of the Node I am popping from the queue and also returning the data from that Node to the user before it is deleted. When I comment out the free() method in the pop() method it works and when I uncomment the free() call I get heap memory crashes. Can anyone please help me see what I am doing wrong?   Queue.inl template <typename T> T Queue<T>::pop() { if (isEmpty()) { std::cout << "pop() was used, but the Queue is empty.\n\n"; return NULL; } else { Node * temp = headNode; T removedItem = temp->data; headNode = headNode->nextNode; std::cout << "pop() was used and " << temp->data << " was removed from the Queue.\n\n"; free(temp); size--; return removedItem; } } Queue.h #pragma once template <class T> class Queue { public: Queue(); Queue(T newItem); void push(T newItem); void pop_all(); T pop(); T peek(); bool isEmpty(); int getSize(); private: struct Node { T data; Node * nextNode; }; Node LinkedList; Node * headNode; // front of the queue Node * tailNode; // End of the Queue int size; }; #include "Queue.inl"
7. ## Data Structures Help!

Yeah, I had to change those as well. The & always slips my mind :rolleyes: haha
8. ## Data Structures Help!

Thanks! I was not aware of having to do this with templates!         Thanks a lot! I used the .inl file method and it worked great!

10. ## C++ Deleting data from files

You're right, I did not even think about crashes during the copying/writing to the file!     Very interesting, I did not know that about fstream, thanks for all the useful information!