Linked List Issues
I have a linked list class that links structs that contain a string. My function to add a node is this:
void CLinkedList::AddWord(char *word)
{
if(Head == NULL)
{
Head = new LinkedWord;
strcpy(Head->word,word);
Head->next = NULL;
Head->prev = NULL;
Tail = Head;
}
else
{
LinkedWord* temp = Head;
Head = new LinkedWord;
strcpy(Head->word,word);
Head->prev = temp;
Head->next = NULL;
temp ->next = Head;
}
NumWords++;
}
If I add one word to the list, it is fine. However, if I add a second word, it changed the first one, adding strange characteres to the end of it, like this: testà¢
Then, if I add a third word, it does this to the second word, and so on like that. I have no idea why this is occuring, I can''t seem to trace the problem.
I am also having difficulty with my function to clear the entire list:
void CLinkedList::ClearList()
{
LinkedWord* walker = Head;
while(walker != NULL)
{
LinkedWord* temp = walker;
walker = walker->prev;
delete temp;
}
Head = NULL;
Tail = NULL;
NumWords = 0;
}
If there are more then four or five words on the list, when the program tries to execute delete temp, it crashes.
Any help on these issues would be greatly appreciated.
I don''t think strcpy copies the NULL-terminator which is important for other functions to know where the string ends.
I think you need to use either memcpy or strncpy to do that.
Also I don''t see anywhere where you allocate any memory for the Head->word array. However considering my state of mind(tired) at the moment that might not be necessary :/ heh, i should probably not be looking at linked lists at all
ah anyways hopes that was any help
I think you need to use either memcpy or strncpy to do that.
Also I don''t see anywhere where you allocate any memory for the Head->word array. However considering my state of mind(tired) at the moment that might not be necessary :/ heh, i should probably not be looking at linked lists at all
ah anyways hopes that was any help
I don''t think strcpy copies the NULL-terminator which is important for other functions to know where the string ends.
strcpy does copy the null terminator, otherwise the resulting copy wouldn''t be a string and the function would be useless.
I think you need to use either memcpy or strncpy to do that.
memcpy, not really. strncpy is always a good idea though.
Also I don''t see anywhere where you allocate any memory for the Head->word array.
That does indeed looks like a problem.
LordN - You should consider using the standard C++ list class instead of making your own.
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
strcpy does copy the null terminator, otherwise the resulting copy wouldn''t be a string and the function would be useless.
I think you need to use either memcpy or strncpy to do that.
memcpy, not really. strncpy is always a good idea though.
Also I don''t see anywhere where you allocate any memory for the Head->word array.
That does indeed looks like a problem.
LordN - You should consider using the standard C++ list class instead of making your own.
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
Is this some homework where you need to write your own linked list implementation? If not, there''s no compelling reason to not use STL.
In any case, consider making some abstraction. A linked list is a generic term and does typically not have methods such as "AddWord". If you extend your application and need a linked list to keep track of DVDs, airplanes, cars or whatever, you cannot re-use this list. Making a templated version would help.
In any case, consider making some abstraction. A linked list is a generic term and does typically not have methods such as "AddWord". If you extend your application and need a linked list to keep track of DVDs, airplanes, cars or whatever, you cannot re-use this list. Making a templated version would help.
Thanks for your help. I allocated memory for the word array and know the glitch is gone. I have never heard of STL, could someone elaborate?
http://www.sgi.com/tech/stl/
A simple example...
edit: added example
edit: fixed quotes
[edited by - MaulingMonkey on July 28, 2003 2:38:28 AM]
A simple example...
#include <iostream>#include <string>#include <list> //one of many STL header filesusing namespace std;int main( ... ){ cout << "Enter some things... Type 'quit' on it's own line to quit." << endl; list< string > mylist; while( true ) { string input; cin >> input; mylist.push_back( input ); if (input == "quit") { cout << "You typed:" << endl; list< string >::iterator listitem = mylist.begin(); while (listitem != mylist.end()) { cout << (*listitem) << endl; ++listitem; } return 0; } }}
edit: added example
edit: fixed quotes
[edited by - MaulingMonkey on July 28, 2003 2:38:28 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement