| | |
Problem with doubly linked lists
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2009
Posts: 6
Reputation:
Solved Threads: 0
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)
# include <iostream> # include <iomanip> # include <fstream> # include <string> # include <cctype> # include <cstddef> using namespace std; const string WORD_FILE = "listing.txt"; const int MAX_SIZE = 26; struct wordList { string newWord; wordList *previous; wordList *next; }; void programIntro(); void checkTextFile (ifstream& wordIn); void initializeArray (wordList *wordArray[], int &index); void readTextFile (ifstream& wordIn, wordList* wordArray [], int &index, int arrayCount, string &word1); void checkInputWord (ifstream &wordIn, wordList* wordArray [], string &word1); void insertWord (wordList *wordArray [], int &index, string &word1); int main () { wordList* theWord[MAX_SIZE]; int arrayCount = 0; int indexes = 0; int count; string words; ifstream wordIn; programIntro(); checkTextFile (wordIn); initializeArray(theWord, indexes); readTextFile (wordIn, theWord, indexes, arrayCount, words); system ("pause"); return 0; } void programIntro () { cout << "**************************************************************************" <<endl; cout << "This program will search a text file and place the words in the text file" << endl; cout << "into a doubly linked list." << endl << endl; cout << "**************************************************************************" << endl; return; } void checkTextFile (ifstream& wordIn) { wordIn.open (WORD_FILE.c_str()); if (!wordIn) cout << "Error! Text File did not open!" << endl; else cout << "File opened correctly!" << endl; return; } void initializeArray (wordList *wordArray[], int &index) { for (index = 0; index < MAX_SIZE; index++) { wordArray[index] = NULL; } return; } void readTextFile (ifstream& wordIn, wordList* wordArray [], int &index, int arrayCount, string &word1) { char character; char letter; arrayCount = 0; string word; wordIn >> word1; cout << " READ WORD: " << word1 << endl; cout << " ARRAY count = " << arrayCount << endl; character = word1[0]; letter = toupper(character); index = letter - 'A'; cout << " Index for Word: " << index << endl << endl; checkInputWord (wordIn, wordArray, word1); insertWord (wordArray, index, word1); while (wordIn.eof() != true) { arrayCount++; wordIn >> word1; checkInputWord (wordIn, wordArray, word1); character = word1[0]; letter = toupper(character); index = letter - 'A'; cout << " READ WORD: " << word1 << endl; cout << " ARRAY count = " << arrayCount << endl; cout << " Index for Word: " << index << endl << endl; insertWord (wordArray, index, word1); } return; } void checkInputWord (ifstream &wordIn, wordList *wordArray [], string &word1) { int curr_position = 0; int next_position; string badWord; next_position = word1.find_first_of(",.;:)(*&^%$#@!1234567890", curr_position); badWord = word1.substr(curr_position, next_position - curr_position); cout << " Current position: " << curr_position << endl << endl; cout << " Next position: " << next_position << endl ; cout << " Bad Word: " << badWord << endl ; if (next_position != string::npos) { curr_position = next_position + 1; next_position = word1.find_first_of(" ,.;:)(*&^%$#@!1234567890", curr_position); badWord = word1.substr(curr_position, next_position - curr_position); cout << " Current position: " << curr_position << endl << endl; cout << " Next position: " << next_position << endl << endl; cout << " Bad Word: " << badWord << endl << endl; } return; } void insertWord (wordList *wordArray [], int &index, string &word1) { wordList *theList; if (wordArray[index] == NULL) { theList = new wordList; theList->newWord = word1; theList->next = NULL; theList->previous = NULL; wordArray[index] = theList; cout << wordArray[index]->newWord << endl; } else { wordList *newNode = new wordList; newNode->newWord = word1; newNode->previous = NULL; newNode->next = wordArray[index]; wordArray[index]->previous = newNode; wordArray[index]->next = wordArray[index]->previous; cout << wordArray[index]->previous->newWord << endl; cout << wordArray[index]->next->newWord<< endl; } return; }
Last edited by John A; Jul 14th, 2009 at 2:31 am. Reason: added code tags
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
-chandra
Welcome drake2212,
Please post listing.txt file's content.
I have a suggestion, modify else block of insertWord.
Please post listing.txt file's content.
I have a suggestion, modify else block of insertWord.
C++ Syntax (Toggle Plain Text)
wordList *newNode = new wordList; newNode->newWord = word1; newNode->previous = NULL; newNode->next=NULL; theList = wordArray[index]; while(theList->next!=NULL) { theList=theList->next; } theList->next=newNode; theList->next->previous=theList;
Failure is not fatal, but failure to change might be. - John Wooden
•
•
Join Date: Jul 2009
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
Welcome drake2212,
Please post listing.txt file's content.
I have a suggestion, modify else block of insertWord.
C++ Syntax (Toggle Plain Text)
wordList *newNode = new wordList; newNode->newWord = word1; newNode->previous = NULL; newNode->next=NULL; theList = wordArray[index]; while(theList->next!=NULL) { theList=theList->next; } theList->next=newNode; 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?
![]() |
Similar Threads
- Major Problem with Doubly linked list program (C++)
- Help with Classes and Doubly Linked Lists (C++)
- Doubly Linked Lists (Java)
- Doubly Linked List Problem (C++)
- Stacks using doubly linked lists (C++)
- C/ Need Help with Linked Lists please (C)
- stack of linked lists (C++)
Other Threads in the C++ Forum
- Previous Thread: Creating a simple series ?
- Next Thread: Problem with dynamic template stack
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






