| 12-31-2006, 08:48 PM | #1 | |
I was experimenting with linked lists, and ended up making a class for using them. Its use is specifically to add/find values stored in it. The finding function is working perfectly fine, my problem is with the adding function. Here is my linked list struct: Code:
struct node{
char* value;
node* next;
};Now, with my insert function (to add a string to a list), it works perfectly fine if I give it a constant string like "text", but if I insert a character array, the first time it works fine. After that, if I add another, it turns all the values in the linked list to the value just added. If you still don't understand, here's the code:
Any ideas on what the problem is? |
| 12-31-2006, 09:24 PM | #2 |
It's not a problem in your list implementation, but you're overwriting the characters of your temp-variable in the main function. Since you're just passing pointers to the first character of your id (and not copying the values), both nodes will point to the same temp-variable. Also why reinvent the wheel, when the STL already offers a wide variety of generic container classes? On another note you shouldn't be using a linked list if you have to call the find-operation often because it performs poorly on a list. Better use a hashtable or search tree. I'd recommend using a set< ID > from the STL. Make ID a struct or class, you'll have to implement the ==operator for it to work smooth. Good Luck. |
| 12-31-2006, 09:39 PM | #3 |
I don't want to use the namespace std because it multiplies the size of my program using my compiler. A simple hello world program on my compiler compiled to something like 200kb on Dev C++ and 40kb on Visual C++. My program is currently about 8-9kb in size. Anyways, as you said it was referring to the address of a variable instead of the actual value. What I'm doing in my main file is reading from a file and then adding that value to the list (I could probably change the data structure later). I figured that I needed a variable to read the data into the list, so I did that. Now, I really just want to be able to refer to the value stored in the variable instead of the address. You mentioned having the variable in a struct or class, how exactly would that work in this situation? Another guess I have would be to just malloc new memory for each string I want to add, and then free all that memory when destroying the list. How well would this work? |
| 12-31-2006, 09:46 PM | #4 |
200Kb is almost nothing, and you'd eventually have to use std anyways. |
| 12-31-2006, 09:49 PM | #5 |
Amazing how some people are still trying to save the most insignificant amount of bytes at the cost of multiplying the time it takes to develop something by a factor of hundreds. |
| 12-31-2006, 09:51 PM | #6 |
I guess, but for this simple of a program I don't think that's entirely needed. The malloc for each string I want to add is working fine. EDIT: I'm going to clock the speed of the program. Okay, the speed of the program on a small file that was about 19kb was 15ms for the entire program. Now, on a 239kb .doo file, it took 200ms which I see as perfectly fast enough. |
