| | |
Linked List Problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 22
Reputation:
Solved Threads: 0
Hello everyone,
I need to load a multi-word strings from a file to a linked list in one of my programs, and i'm not sure how to do that.
my text file looks like this- (the name of the text file is: names.txt)
Linda
Martinho
Marla
Jose
Mary
Luka
Joseph
Thiago
Ruba
here is some of my code so far...
Thank you very much in advance,
Doug
I need to load a multi-word strings from a file to a linked list in one of my programs, and i'm not sure how to do that.
my text file looks like this- (the name of the text file is: names.txt)
Linda
Martinho
Marla
Jose
Mary
Luka
Joseph
Thiago
Ruba
here is some of my code so far...
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cassert> #include <String> #include <stdio.h> #include <stdlib.h> using namespace std; template <class TYPE> struct NODE { TYPE data; //Data Item NODE<TYPE> *next; }; template<class TYPE> class multiword { protected: int count; //Variable to store the values of the numbers NODE<TYPE> *first; //pointer to the first NODE public: class NotFound {}; void Initialize(); //Initialize the list with no values bool Empty(); //Check if the list is empty void Destroy(); void fillNames(ifstream& inFile, multiword<TYPE>& cList); //Delete every value of the list int binarysearch(const TYPE& item); //Binary search void additem(const TYPE& add); //Add a value void Insertion(); //Insertion void display(); //Display multiword(); //Constructor(no-arg) ~multiword(); //Destructor }; template<class TYPE> void multiword<TYPE>::fillNames(ifstream& inFile, multiword<TYPE>& cList) { string firstN; int i; TYPE temp; const int number = 10 for(i = 0; i < number; i++) { inFile>>firstN; temp.setName(firstN); cList.insertAt(i, temp); } } template<class TYPE> //Check if the list is empty bool multiword<TYPE>::Empty() { return(first == NULL); } template<class TYPE> //Constructor multiword<TYPE>::multiword() { first = NULL; count = 0; } template<class TYPE> //Destructor multiword<TYPE>::~multiword() { Destroy(); } template<class TYPE> void multiword<TYPE>::Insertion() { nodeType<TYPE> *lastInOrder; nodeType<TYPE> *firstOutOfOrder; nodeType<TYPE> *current; nodeType<TYPE> *trailCurrent; lastInOrder = first; if(first = NULL) cerr << "Cannot sort an empty list." << endl; else if(first->link == NULL) cout << "The list of length1, it is already in order." << endl; else while(lastInOrder->link !=NULL) { firstOutOfOrder = lastInOrder->link; if(firstOutOfOrder->info < first->info) { lastInOrder->link = firstOutOfOrder->link; firstOutOfOrder->link = first; first = firstOutOfOrder; } else { trailCurrent = first; current = first->link; while(current->info < firstOutOfOrder->info) { trailCurrent = current; current = current ->link; } if(current != firstOutOfOrder) { lastInOrder->link = firstOutOfOrder->link; firstOutOfOrder->link = current; trailCurrent->link = firstOufOfOrder; } else lastInOrder = lastInOrder->link } } } template<class TYPE> int multiword<TYPE>::binarysearch(const TYPE& item) { int first = 0; int last = length -1; int mid; bool found = false; while(first <= last && !found) { mid = (first + last) / 2; if(list[mid] == item) found = true; else if(list[mid] > item) last = mid -1; else first = mid + 1; } if(found) return mid; else return -1; } template<class TYPE> //Destroy every element on the list void multiword<TYPE>::Destroy() { NODE<TYPE> *temp; //deallocates memory while(first != NULL) //while there are value in the list { temp = first; //set temp to the first value in the list first = first->next; //put first to the next value delete temp; //delete memory } count = 0; } template<class TYPE> //Initialize the list void multiword<TYPE>::Initialize() { Destroy(); //If any values are found, remove them. }
Thank you very much in advance,
Doug
Last edited by d0ugg; Nov 4th, 2007 at 6:42 pm.
You need to be just a little more careful with names
Insertion() is a bad name because it is not obvious that it sorts the list. InsertionSort() is a good name.
Destroy() is not a good name because it doesn't destroy your object; it only deletes every item in the list. Name it something like Clear() or MakeEmpty(). By the way, Initialize() has the same logical functionality: both clear the list if not already emtpy. I would get rid of Initialize() and just use Clear() or whatever you name it. Actual initialization already occurs properly in the constructor.
Be careful with your types
In general, you should not assume that TYPE has a member function setName(). If you are just making a linked list of strings, there is no real reason to create a new class just to hold it. Just use std::string. Also, in template declarations, use the word typename instead of class. (Yes, yes, I know... semantics. Even so...)
More on fillNames()
You are trying to do two things here: initialize *this from file and initialize some that (which you named cList). If you need two copies of the multiword list, make a copy constructor and copy it that way; Don't force your user to fill two multiword lists just to load from file.
Also, you are using a member function named insertAt() which you have not defined to exist in the multiword class.
I recommend that you get rid of the cList argument and just load words from file into *this's list.
Lastly, you should not hardcode the number of lines in your file. What if you later want to load a list of names 100 names long? Or two? Just read names and append them to the end of the list as long as there are lines in the file.
Oy, that's enough for now. I haven't looked over your sort or search algorithms... but you seem to have a pretty good start. Work on straightening up those things I mentioned and then come back with compiler errors or what is not working right when you use the class.
Good luck.
Insertion() is a bad name because it is not obvious that it sorts the list. InsertionSort() is a good name.
Destroy() is not a good name because it doesn't destroy your object; it only deletes every item in the list. Name it something like Clear() or MakeEmpty(). By the way, Initialize() has the same logical functionality: both clear the list if not already emtpy. I would get rid of Initialize() and just use Clear() or whatever you name it. Actual initialization already occurs properly in the constructor.
Be careful with your types
In general, you should not assume that TYPE has a member function setName(). If you are just making a linked list of strings, there is no real reason to create a new class just to hold it. Just use std::string. Also, in template declarations, use the word typename instead of class. (Yes, yes, I know... semantics. Even so...)
More on fillNames()
You are trying to do two things here: initialize *this from file and initialize some that (which you named cList). If you need two copies of the multiword list, make a copy constructor and copy it that way; Don't force your user to fill two multiword lists just to load from file.
Also, you are using a member function named insertAt() which you have not defined to exist in the multiword class.
I recommend that you get rid of the cList argument and just load words from file into *this's list.
Lastly, you should not hardcode the number of lines in your file. What if you later want to load a list of names 100 names long? Or two? Just read names and append them to the end of the list as long as there are lines in the file.
Oy, that's enough for now. I haven't looked over your sort or search algorithms... but you seem to have a pretty good start. Work on straightening up those things I mentioned and then come back with compiler errors or what is not working right when you use the class.
Good luck.
Last edited by Duoas; Nov 4th, 2007 at 9:13 pm.
•
•
Join Date: Nov 2007
Posts: 22
Reputation:
Solved Threads: 0
Hey,
So I changed a couple things that you told me already, but I have no idea how to do what you told me on the fillname();
Right now it looks like this:
I'm not sure why I added setName and insertAt. Is there any other way to add the text file without calling other classes such as setname and insertat?
Thank you,
Doug
So I changed a couple things that you told me already, but I have no idea how to do what you told me on the fillname();
Right now it looks like this:
C++ Syntax (Toggle Plain Text)
template<typename TYPE> void multiword<TYPE>::fillNames(ifstream& inFile, multiword<TYPE>& cList) { string firstN; int i; TYPE temp; const int number = 10 for(i = 0; i < number; i++) { inFile>>firstN; temp.setName(firstN); cList.insertAt(i, temp); } }
I'm not sure why I added setName and insertAt. Is there any other way to add the text file without calling other classes such as setname and insertat?
Thank you,
Doug
Think about the types of things you have.
You have a NODE class, which is used by the multiword class to store a list of some unspecified TYPE.
The fillNames() function shouldn't know or care what type of thing TYPE is. Just read it from file and store it.
Give it some thought...
Now, when it is time to use your linked list class, what type of thing are you storing in a list? Does it matter if the multiword class has any idea what type that is? Should I be able to say:
or:
or:
Think about it.
You have a NODE class, which is used by the multiword class to store a list of some unspecified TYPE.
The fillNames() function shouldn't know or care what type of thing TYPE is. Just read it from file and store it.
Give it some thought...
Now, when it is time to use your linked list class, what type of thing are you storing in a list? Does it matter if the multiword class has any idea what type that is? Should I be able to say:
multiword<int> my_int_list;or:
multiword<bool> my_bool_list;or:
multiword<Employee> my_employee_list; (where Employee is some class defined elsewhere with the >> and << I/O operators properly overloaded)Think about it.
Last edited by Duoas; Nov 4th, 2007 at 11:57 pm.
![]() |
Similar Threads
- Linked List problem (C++)
- linked list problem!!! (C)
- Doubly Linked List Problem (C++)
- Linked list (C++)
- Linked List problem (C)
- The C++ LINKED LIST (C++)
- remove method linked list (C)
- Linked List & Objects (C++)
- Cannot figure out how to implement linked list and rbtree for a project! (Java)
Other Threads in the C++ Forum
- Previous Thread: help in starting a program
- Next Thread: EOF in a CFile
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






