Problem with doubly linked lists

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2009
Posts: 6
Reputation: drake2212 is an unknown quantity at this point 
Solved Threads: 0
drake2212 drake2212 is offline Offline
Newbie Poster

Problem with doubly linked lists

 
0
  #1
Jul 14th, 2009
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.



  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
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 440
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 68
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: Problem with doubly linked lists

 
0
  #2
Jul 14th, 2009
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.
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,585
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 458
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Problem with doubly linked lists

 
0
  #3
Jul 14th, 2009
Welcome drake2212,
Please post listing.txt file's content.
I have a suggestion, modify else block of insertWord.
  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;
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: Problem with doubly linked lists

 
0
  #4
Jul 14th, 2009
The wordList pointer in insertWord() goes out of scope (and therefore is lost) when insertWord returns to the calling function.
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 6
Reputation: drake2212 is an unknown quantity at this point 
Solved Threads: 0
drake2212 drake2212 is offline Offline
Newbie Poster

Re: Problem with doubly linked lists

 
0
  #5
Jul 14th, 2009
Originally Posted by jencas View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 6
Reputation: drake2212 is an unknown quantity at this point 
Solved Threads: 0
drake2212 drake2212 is offline Offline
Newbie Poster

Re: Problem with doubly linked lists

 
0
  #6
Jul 14th, 2009
Originally Posted by adatapost View Post
Welcome drake2212,
Please post listing.txt file's content.
I have a suggestion, modify else block of insertWord.
  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?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC