insertEnd() linked list

Currently this is what I have for my insertEnd(). I'm trying to use a linked list and basically insert a value at the end of the list. However, In InsertEnd() I must check for the list being empty. The way it is coded at the moment it causes a problem during the first call to InsertEnd(). 'first' is 0, so 'current' is set to 0. Then I am using 'current->next' in the while loop condition. Since 'current' is 0 this will cause the program to crash. I have to treat the list being empty as a special case within the InsertEnd() function. My question is what do I need to change in order to do this...

If your items are character arrays like that, you shouldn't be using == and != to compare them. That just compares pointers which probably isn't the result you want.

The problem is probably that you don't handle the case where 'item' is not found in the list (likely with the incorrect compare). The while loop executes until 'current' becomes null at which time you'll get an access violation or something when you try to get 'current->next' with the bad value of 'current'. The template function will not work with T being char*. You'll probably have to use std::string which defines == and != to do the right thing.

I'm saying that char*, which appears to be 'T' in your present case, does not behave line int or float. You cannot simply compare pointers. For instance, you might have 'str1' and 'str2', both of which are char* and presently having the value "My String", but str1 != str2. You can use std::string which wraps a class around char* so you can take two strings and do 'stdStr1 == stdStr2;'. It does a proper string compare.

So yes, you could do 'using namespace std;'. You also need to '#include <string>' and use 'std::string' in your template instantiation.

I'm trying to add a function to this little program called GetFirstNode which basically should return the value at the beginning of the list. Here is what I have, but its not quite working. Can you help me?

Your code did not even attempt to output the returned value and you need to use the c_str() method to get a 'const char*' that cout can deal with. There is no implicit cast to 'const char*' for std::string.

drichards, a quick question for you here. Currently, in main() I am hardcoding the words that I want to insert into the front and end functions. Can you tell me how I would read in a simple text file, say that looks like this...

sun
tree
apple

and have that be the data that gets inserted into the front and end. And then when I search I'm not hardcoding the word I am just looking through the file to see if its there. Does that make sense? Basically, I need to use this little txt file instead of me hard coding the words in. Here is what main() currently looks like.

I'm just really trying to use the data from this text file that I read in instead of saying things like, list.insertFront("Tree");

You know what I mean?

I don't know how to pass the values of the text file to this function or any of them for that matter. For example, I have been saying for say, search(). list.search("Tree"); now I want to be able to call that and say, list.search("any of the words in the text file); Does that make sense?

>> I have to read the words in from a txt file and use those. My question is how do I do that?

It is still not clear to me what you're doing with the file. You can use the code above to read the file and do whatever you want with the words. Once variant gets the words and adds them to the list. You don't have to do the list insertion if that's not what you want. The other one puts the words in a vector and calls a method to look through the list for each element of the vector. I assume by your last comment that you don't want to add any more methods to the list class, so just don't make the above method a member of the list class. Either use the first code snippet and call your list search instead of adding the words to the list or use the second code snippet and just make it a regular function rather than a method of the list class. What exactly are you looking for?

1. I can't use vectors for this, so it has to a little easier than that.

Here is what I have so far....maybe this will help to clarify what is happening. To reiiterate I am just simply wanting to use the words in the txt file (tree, sun, star) and insert these to the front, to the end, and then just do a search after the user types in a word to search for. If its on the list then display that it is.

If 'txt' is a std::string, you cannot do either '(f/c)in >> txt;' or 'cout << txt;'. You need:

std::getline(fin,txt);
std::getline(cin,txt);

cout << txt.c_str();

Other than that you seem to be on the right track. You've got a lot of wgat appears to be debug code, so I'm not going to try to clean it up for you. I think you are just having issues with reading/writing the std::string's.

0

Featured Post

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects.
A brief on problem:
Lets take example problem for simplicity:
- I have a G…