943,777 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 484
  • C++ RSS
Jul 14th, 2009
0

Problem with doubly linked lists

Expand Post »
So this is my first time on here asking for help but I do visit this forum every now and again when I am taking a programming class. I am trying to get my doubly linked list to work correctly. Also I am having issues with parsing an input file. The function for the parsing is checkInputWord, and I wanted to see if you guys could see what I was messing up. Then in the insertWord function I am trying to either add a node to an empty array of pointers or one that already has some pointers. Here is my code and hopefully it is written well enough so you can understand what I am trying to do.



C++ Syntax (Toggle Plain Text)
  1. # include <iostream>
  2. # include <iomanip>
  3. # include <fstream>
  4. # include <string>
  5. # include <cctype>
  6. # include <cstddef>
  7.  
  8. using namespace std;
  9.  
  10. const string WORD_FILE = "listing.txt";
  11. const int MAX_SIZE = 26;
  12.  
  13.  
  14. struct wordList {
  15. string newWord;
  16. wordList *previous;
  17. wordList *next;
  18. };
  19.  
  20. void programIntro();
  21. void checkTextFile (ifstream& wordIn);
  22. void initializeArray (wordList *wordArray[], int &index);
  23. void readTextFile (ifstream& wordIn, wordList* wordArray [], int &index, int arrayCount, string &word1);
  24. void checkInputWord (ifstream &wordIn, wordList* wordArray [], string &word1);
  25. void insertWord (wordList *wordArray [], int &index, string &word1);
  26.  
  27. int main () {
  28.  
  29. wordList* theWord[MAX_SIZE];
  30. int arrayCount = 0;
  31. int indexes = 0;
  32. int count;
  33. string words;
  34.  
  35. ifstream wordIn;
  36.  
  37.  
  38.  
  39. programIntro();
  40. checkTextFile (wordIn);
  41. initializeArray(theWord, indexes);
  42. readTextFile (wordIn, theWord, indexes, arrayCount, words);
  43.  
  44.  
  45. system ("pause");
  46. return 0;
  47. }
  48.  
  49. void programIntro ()
  50.  
  51. {
  52. cout << "**************************************************************************" <<endl;
  53. cout << "This program will search a text file and place the words in the text file" << endl;
  54. cout << "into a doubly linked list." << endl << endl;
  55. cout << "**************************************************************************" << endl;
  56.  
  57. return;
  58.  
  59. }
  60.  
  61. void checkTextFile (ifstream& wordIn)
  62.  
  63. {
  64.  
  65. wordIn.open (WORD_FILE.c_str());
  66.  
  67. if (!wordIn)
  68. cout << "Error! Text File did not open!" << endl;
  69.  
  70. else
  71. cout << "File opened correctly!" << endl;
  72.  
  73. return;
  74. }
  75.  
  76.  
  77.  
  78. void initializeArray (wordList *wordArray[], int &index)
  79.  
  80. {
  81.  
  82. for (index = 0; index < MAX_SIZE; index++) {
  83. wordArray[index] = NULL;
  84. }
  85.  
  86. return;
  87. }
  88.  
  89. void readTextFile (ifstream& wordIn, wordList* wordArray [], int &index, int arrayCount, string &word1)
  90.  
  91. {
  92.  
  93. char character;
  94. char letter;
  95. arrayCount = 0;
  96. string word;
  97.  
  98. wordIn >> word1;
  99. cout << " READ WORD: " << word1 << endl;
  100. cout << " ARRAY count = " << arrayCount << endl;
  101. character = word1[0];
  102. letter = toupper(character);
  103. index = letter - 'A';
  104.  
  105. cout << " Index for Word: " << index << endl << endl;
  106. checkInputWord (wordIn, wordArray, word1);
  107. insertWord (wordArray, index, word1);
  108.  
  109. while (wordIn.eof() != true) {
  110.  
  111. arrayCount++;
  112. wordIn >> word1;
  113. checkInputWord (wordIn, wordArray, word1);
  114. character = word1[0];
  115. letter = toupper(character);
  116. index = letter - 'A';
  117. cout << " READ WORD: " << word1 << endl;
  118. cout << " ARRAY count = " << arrayCount << endl;
  119.  
  120.  
  121. cout << " Index for Word: " << index << endl << endl;
  122. insertWord (wordArray, index, word1);
  123.  
  124. }
  125.  
  126. return;
  127. }
  128.  
  129. void checkInputWord (ifstream &wordIn, wordList *wordArray [], string &word1)
  130.  
  131. {
  132. int curr_position = 0;
  133. int next_position;
  134. string badWord;
  135.  
  136. next_position = word1.find_first_of(",.;:)(*&^%$#@!1234567890", curr_position);
  137. badWord = word1.substr(curr_position, next_position - curr_position);
  138.  
  139. cout << " Current position: " << curr_position << endl << endl;
  140. cout << " Next position: " << next_position << endl ;
  141. cout << " Bad Word: " << badWord << endl ;
  142.  
  143. if (next_position != string::npos) {
  144. curr_position = next_position + 1;
  145. next_position = word1.find_first_of(" ,.;:)(*&^%$#@!1234567890", curr_position);
  146. badWord = word1.substr(curr_position, next_position - curr_position);
  147.  
  148. cout << " Current position: " << curr_position << endl << endl;
  149. cout << " Next position: " << next_position << endl << endl;
  150. cout << " Bad Word: " << badWord << endl << endl;
  151. }
  152. return;
  153.  
  154. }
  155.  
  156. void insertWord (wordList *wordArray [], int &index, string &word1)
  157.  
  158. {
  159. wordList *theList;
  160.  
  161. if (wordArray[index] == NULL) {
  162. theList = new wordList;
  163. theList->newWord = word1;
  164. theList->next = NULL;
  165. theList->previous = NULL;
  166. wordArray[index] = theList;
  167. cout << wordArray[index]->newWord << endl;
  168. }
  169.  
  170. else {
  171. wordList *newNode = new wordList;
  172. newNode->newWord = word1;
  173. newNode->previous = NULL;
  174. newNode->next = wordArray[index];
  175. wordArray[index]->previous = newNode;
  176. wordArray[index]->next = wordArray[index]->previous;
  177.  
  178. cout << wordArray[index]->previous->newWord << endl;
  179. cout << wordArray[index]->next->newWord<< endl;
  180. }
  181.  
  182.  
  183. return;
  184. }
Last edited by John A; Jul 14th, 2009 at 2:31 am. Reason: added code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
drake2212 is offline Offline
6 posts
since Jul 2009
Jul 14th, 2009
0

Re: Problem with doubly linked lists

I don't really understand what you are doing. You just have an array of structures of type wordList. So basically you are not using the linked list at all !!! You create a node and insert it into the array. I don't think that's how you would be expected to use linked lists. You can use the list itself as a data-structure. Keep the start of the list with you and traverse it as per your needs.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Jul 14th, 2009
0

Re: Problem with doubly linked lists

Welcome drake2212,
Please post listing.txt file's content.
I have a suggestion, modify else block of insertWord.
C++ Syntax (Toggle Plain Text)
  1. wordList *newNode = new wordList;
  2. newNode->newWord = word1;
  3. newNode->previous = NULL;
  4. newNode->next=NULL;
  5.  
  6. theList = wordArray[index];
  7. while(theList->next!=NULL)
  8. {
  9. theList=theList->next;
  10. }
  11. theList->next=newNode;
  12. theList->next->previous=theList;
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jul 14th, 2009
0

Re: Problem with doubly linked lists

The wordList pointer in insertWord() goes out of scope (and therefore is lost) when insertWord returns to the calling function.
Reputation Points: 395
Solved Threads: 71
Posting Whiz
jencas is offline Offline
362 posts
since Dec 2007
Jul 14th, 2009
0

Re: Problem with doubly linked lists

Click to Expand / Collapse  Quote originally posted by jencas ...
The wordList pointer in insertWord() goes out of scope (and therefore is lost) when insertWord returns to the calling function.
Do you have any suggestions on how to fix this?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
drake2212 is offline Offline
6 posts
since Jul 2009
Jul 14th, 2009
0

Re: Problem with doubly linked lists

Click to Expand / Collapse  Quote originally posted by adatapost ...
Welcome drake2212,
Please post listing.txt file's content.
I have a suggestion, modify else block of insertWord.
C++ Syntax (Toggle Plain Text)
  1. wordList *newNode = new wordList;
  2. newNode->newWord = word1;
  3. newNode->previous = NULL;
  4. newNode->next=NULL;
  5.  
  6. theList = wordArray[index];
  7. while(theList->next!=NULL)
  8. {
  9. theList=theList->next;
  10. }
  11. theList->next=newNode;
  12. theList->next->previous=theList;

Here is what is in the text file.

I like this class. It is much of a challenge. I can't wait to get
some help on this program cuz it is tough!

I tred implementing your suggestion and when I try to output certain indexes I get runtime errors ... any suggestions?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
drake2212 is offline Offline
6 posts
since Jul 2009

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: Creating a simple series ?
Next Thread in C++ Forum Timeline: Problem with dynamic template stack





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


Follow us on Twitter


© 2011 DaniWeb® LLC