Jump to content
  • Advertisement
Sign in to follow this  
DarkThrone

Some help with linked lists.

This topic is 4830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I use this function above to same a linked list word spliter. I don't know why, but it allocs only a limited range of words(between 10 and 20). Can anybody help? void WordList::SplitWordsOntoList(char *st, char delimiter, char lastchar) { unsigned int position = 0; char buffer[50]; unsigned int arraylenght = 0; for(position = 0;position < 50; position++) { if(st[position] != delimiter && st[position] != lastchar) { buffer[arraylenght] = st[position]; arraylenght++; } else { buffer[arraylenght] = '\0'; pttabpos->word = strdup(buffer); pttabpos->nextWord = new WordTable; pttabpos = pttabpos->nextWord; pttabpos->nextWord = NULL; arraylenght = 0; if(st[position] == lastchar) { return; } } } }

Share this post


Link to post
Share on other sites
Advertisement
You're only looping 'position' from 0 to 50 (presumably because that's the size of your buffer). I think you want to loop until the last character is reached.

(There could be other things as well, but I didn't look at it too carefully...)

Share this post


Link to post
Share on other sites
Dear god.

The standard library provides everything you need here.


#include <list>
#include <string>
#include <sstream>
#include <iostream>

typedef std::list<std::string> WordList;

// This could easily be wrapped in an OO style instead. But be sure to
// include a std::list<std::string> as a data member in that case, and do *not*
// inherit from that class - it's not designed for inheritance.
void appendWordsTo(WordList& target, const std::string& wordSource) {
std::stringstream ss(wordSource);
std::string word;
while (ss >> word) { target.push_back(word); }
}

int main() {
WordList tmp;
appendWordsTo(tmp, "I liek pie");
for (WordList::iterator it = tmp.begin(); it != tmp.end(); ++it) {
std::cout << *it << std::endl;
}
}



This works for any number of words, of any length (subject to limitations on available memory), without allocating huge buffers (unless they happen to be necessary) - and also will get all the memory allocation/deallocation correct, without leaks, double deletes or any other such nonsense. Plus it's straightforward. :)

Share this post


Link to post
Share on other sites
ok, Jyk, you're right.

I just change "position < 50" on the loop code for "&st[position] != NULL" and it works well.

Thanks...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!