944,066 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4573
  • C++ RSS
Nov 4th, 2007
0

Linked List Problem

Expand Post »
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...
C++ Syntax (Toggle Plain Text)
  1.  
  2.  
  3.  
  4. #include <iostream>
  5. #include <cassert>
  6. #include <String>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. using namespace std;
  10.  
  11. template <class TYPE>
  12. struct NODE
  13. {
  14. TYPE data; //Data Item
  15. NODE<TYPE> *next;
  16. };
  17.  
  18. template<class TYPE>
  19. class multiword
  20. {
  21. protected:
  22. int count; //Variable to store the values of the numbers
  23. NODE<TYPE> *first; //pointer to the first NODE
  24. public:
  25. class NotFound {};
  26. void Initialize(); //Initialize the list with no values
  27. bool Empty(); //Check if the list is empty
  28. void Destroy();
  29. void fillNames(ifstream& inFile, multiword<TYPE>& cList); //Delete every value of the list
  30. int binarysearch(const TYPE& item); //Binary search
  31. void additem(const TYPE& add); //Add a value
  32. void Insertion(); //Insertion
  33. void display(); //Display
  34. multiword(); //Constructor(no-arg)
  35. ~multiword(); //Destructor
  36. };
  37.  
  38. template<class TYPE>
  39. void multiword<TYPE>::fillNames(ifstream& inFile, multiword<TYPE>& cList)
  40. {
  41. string firstN;
  42. int i;
  43.  
  44. TYPE temp;
  45. const int number = 10
  46.  
  47. for(i = 0; i < number; i++)
  48. {
  49. inFile>>firstN;
  50. temp.setName(firstN);
  51. cList.insertAt(i, temp);
  52. }
  53. }
  54.  
  55. template<class TYPE> //Check if the list is empty
  56. bool multiword<TYPE>::Empty()
  57. {
  58. return(first == NULL);
  59. }
  60.  
  61. template<class TYPE> //Constructor
  62. multiword<TYPE>::multiword()
  63. {
  64. first = NULL;
  65. count = 0;
  66. }
  67.  
  68. template<class TYPE> //Destructor
  69. multiword<TYPE>::~multiword()
  70. {
  71. Destroy();
  72. }
  73.  
  74. template<class TYPE>
  75. void multiword<TYPE>::Insertion()
  76. {
  77. nodeType<TYPE> *lastInOrder;
  78. nodeType<TYPE> *firstOutOfOrder;
  79. nodeType<TYPE> *current;
  80. nodeType<TYPE> *trailCurrent;
  81.  
  82. lastInOrder = first;
  83.  
  84. if(first = NULL)
  85. cerr << "Cannot sort an empty list." << endl;
  86. else
  87. if(first->link == NULL)
  88. cout << "The list of length1, it is already in order." << endl;
  89. else
  90. while(lastInOrder->link !=NULL)
  91. {
  92. firstOutOfOrder = lastInOrder->link;
  93. if(firstOutOfOrder->info < first->info)
  94. {
  95. lastInOrder->link = firstOutOfOrder->link;
  96. firstOutOfOrder->link = first;
  97. first = firstOutOfOrder;
  98. }
  99. else
  100. {
  101. trailCurrent = first;
  102. current = first->link;
  103. while(current->info < firstOutOfOrder->info)
  104. {
  105. trailCurrent = current;
  106. current = current ->link;
  107. }
  108.  
  109. if(current != firstOutOfOrder)
  110. {
  111. lastInOrder->link = firstOutOfOrder->link;
  112. firstOutOfOrder->link = current;
  113. trailCurrent->link = firstOufOfOrder;
  114. }
  115. else
  116. lastInOrder = lastInOrder->link
  117. }
  118. }
  119. }
  120.  
  121.  
  122. template<class TYPE>
  123. int multiword<TYPE>::binarysearch(const TYPE& item)
  124. {
  125. int first = 0;
  126. int last = length -1;
  127. int mid;
  128.  
  129. bool found = false;
  130.  
  131. while(first <= last && !found)
  132. {
  133. mid = (first + last) / 2;
  134.  
  135. if(list[mid] == item)
  136. found = true;
  137. else
  138. if(list[mid] > item)
  139. last = mid -1;
  140. else
  141. first = mid + 1;
  142. }
  143. if(found)
  144. return mid;
  145. else
  146. return -1;
  147. }
  148.  
  149. template<class TYPE> //Destroy every element on the list
  150. void multiword<TYPE>::Destroy()
  151. {
  152. NODE<TYPE> *temp; //deallocates memory
  153. while(first != NULL) //while there are value in the list
  154. {
  155. temp = first; //set temp to the first value in the list
  156. first = first->next; //put first to the next value
  157. delete temp; //delete memory
  158. }
  159. count = 0;
  160. }
  161.  
  162. template<class TYPE> //Initialize the list
  163. void multiword<TYPE>::Initialize()
  164. {
  165. Destroy(); //If any values are found, remove them.
  166. }

Thank you very much in advance,

Doug
Last edited by d0ugg; Nov 4th, 2007 at 6:42 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
d0ugg is offline Offline
22 posts
since Nov 2007
Nov 4th, 2007
0

Re: Linked List Problem

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.
Last edited by Duoas; Nov 4th, 2007 at 9:13 pm.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 4th, 2007
0

Re: Linked List Problem

Hey,
Thank you for the help...
I will work on those ideas that you gave me.
Doug
Reputation Points: 10
Solved Threads: 0
Newbie Poster
d0ugg is offline Offline
22 posts
since Nov 2007
Nov 4th, 2007
0

Re: Linked List Problem

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:
C++ Syntax (Toggle Plain Text)
  1. template<typename TYPE>
  2. void multiword<TYPE>::fillNames(ifstream& inFile, multiword<TYPE>& cList)
  3. {
  4. string firstN;
  5. int i;
  6.  
  7. TYPE temp;
  8. const int number = 10
  9.  
  10. for(i = 0; i < number; i++)
  11. {
  12. inFile>>firstN;
  13. temp.setName(firstN);
  14. cList.insertAt(i, temp);
  15. }
  16. }

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
d0ugg is offline Offline
22 posts
since Nov 2007
Nov 4th, 2007
0

Re: Linked List Problem

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:
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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Apr 27th, 2010
0

i need help please.........

i have these problem and i need some one to answer it please.....


Develop menu driven C++ program to manage the College of Science Book Store. Your program should maintain three linked lists: one for books, another one for students and a third one for the loans transactions. When your program starts it should read the books information, students information and loans information from three separate files “books.txt”, “students.txt” and “loans.txt”. An example format of theses files is shown below. You need to:
1. Design and implement a Book structure to encapsulate a book object. Each book has title, author, ID and number of copies. Assume book IDs are unique.
2. Design and implement a BookList class to encapsulate a linked list of Book objects. Provide all necessary functions.
3. Design and implement a Student structure to encapsulate a student object. Each student has name and ID. Assume student IDs are unique.
4. Design and implement a StudentList class to encapsulate a linked list of Student objects. Provide all necessary functions.
5. Design and implement a Loan structure to encapsulate loan transactions in the book store. A Loan object has the Book ID, the Student ID, and the semester when the book is borrowed (e.g. SP10).
6. Design and implement a LoanList class to encapsulate a linked list of Loan objects. Provide all necessary functions.
7. Your program should provide a menu that would allow for the following functionalities:
a. Adding and deleting a book.
b. Adding and deleting a student.
c. Borrowing a book: this means creating a new node in the LoansList with the student ID and the Book ID.
d. Returning a book: this means deleting the node from the LoansList with the specific Student ID and Book ID.
e. List all books borrowed by a specific student.
f. List all students borrowing a specific book.
g. List all books ordered by ID, ascending and designingly. Use recursive functions.
h. List all students ordered by name.
8. Use operator overloading as necessary and other OOP rules and concepts.
9. Handle all errors using appropriate exception handling techniques. Errors include, but are not limited to, no copies to lend a book, no student with a given id/name, no book with a given id/name, missing or wrong information in the input files.
10. When the user chooses to quit the program, you should write the contents of all lists to data files.


Books.txt

Book 1 title
Book 1 author
Book 1 ID
Book 1 copies
Book 2 title
Book 2 author
Book 2 ID
Book 2 copies

Students.txt

Student 1 name
Student 1 ID
Student 2 name
Student 2 ID


Loans.txt

Student 1 ID
Book 1 ID
Semester 1
Student 2 ID
Book 2 ID
Semester 2

/////
i hope some one help me.....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ssuss is offline Offline
2 posts
since Apr 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Array IndexOf
Next Thread in C++ Forum Timeline: Reverse Polish Notation calculator Help please!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC