2 Replies - 1219 Views - Last Post: 01 November 2009 - 02:44 PM Rate Topic: -----

#1 drake1012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-February 09

Struggling! Need help with insert into double linked list

Posted 01 November 2009 - 05:57 AM

I am in desperate need of help! I am having an issue inserting the word into the correct list. Each array index in the array of pointers is a letter of the alphabet, and the words need to be put into the correct index according to the first letter of the word. Can I get some help with fixing this issue with my insertWord function?

Here is the text file I am using:

I am excited to be taking this class. Programming isn't
easy but I feel I am getting better. One day when I am
out of the military I hope to work for some IT business.
The job can't be part-time either!
67dd a09dd.

Here is the program file:

# include <iostream>
# include <iomanip>
# include <fstream>
# include <string>
# include <cctype>
# include <cstddef>

using namespace std;

const int MAX_SIZE = 26;

struct wordRec {
string newWord;
wordRec *previous;
wordRec *next;
};

void programIntro();
wordRec* initializeList ();
void initializeArray (wordRec *[], int& index);
void readTextFile (ifstream& wordIn, wordRec *letters[], int& index, int& wrdIndex);
void checkInputWord (ifstream& wordIn, string& word);
void insertWord (ifstream& wordIn, wordRec* [], int& index, string& word, int& wrdIndex);
void wordListPlacement (ifstream& wordIn, wordRec *letters[], int& index, int& wrdIndex);

int main (int argc, char* argv[]) {

wordRec* letters[MAX_SIZE];
wordRec *head = NULL;

int count;
int indexes;
int wordCount;
string words;
int wordIndex;

ifstream wordIn;

wordIn.open (argv[1]);

if (!wordIn) {
cout << "Error! Text File did not open!" << endl;
system ("pause");
return 1;
}
else 
initializeArray (letters, indexes);
readTextFile (wordIn, letters, indexes, wordIndex);
wordIn.close ();

system ("pause");
return 0;
}

void initializeArray (wordRec *letters[], int& index)

{
for (index = 0; index < MAX_SIZE; index++) {
letters[index] = NULL;
}

return;
}

void readTextFile (ifstream& wordIn, wordRec *letters[], int& index, int& wrdIndex)

{ 
wordRec *head = letters[index];
string word;
char character;
char letter;

while (wordIn) { 
wordIn >> word; 

// calculates the specific list each word will go into
character = word[0];
letter = toupper(character);
wrdIndex = letter - 'A'; 

// checks the word format for errors
checkInputWord (wordIn, word);

// uppercases all the words in the file
for (int indx = 0; indx < word.size(); indx++) {
word[indx] = static_cast<char>(toupper(word[indx]));
}

insertWord (wordIn, letters, index, word, wrdIndex);
}
return;
}

void checkInputWord (ifstream& wordIn, string& word)

{

// strips off any punctuation if there is any
for (int index = 0; index < word.length(); index++) {
if ((ispunct(word[index])) && (word[index] != '\'') && (word[index] != '-')) {
word.erase(index);
}
}

// ignores the word if it contains any digits and does not add it to the list!
for (int index = 0; index < word.size(); index++) {
if (isdigit (word[index])) {
wordIn.ignore (word[index], word.length() );
}
}

return;
} 

void insertWord (ifstream& wordIn, wordRec *letters [], int& index, string& word, int& wrdIndex)

{
wordRec *head = NULL;
wordRec *newNode = NULL;

if (head == NULL) {
newNode = new (nothrow) wordRec;
newNode->newWord = word;
newNode->previous = NULL;
newNode->next = newNode;

if (head != NULL) 
head->previous = newNode;

head = newNode;
letters[index] = head;
letters[index]->next = head;
letters[index]->previous = head;

}
return;
}


*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 01 November 2009 - 06:14 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Struggling! Need help with insert into double linked list

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6037
  • View blog
  • Posts: 23,435
  • Joined: 23-August 08

Re: Struggling! Need help with insert into double linked list

Posted 01 November 2009 - 06:16 AM

Where's your indentation? And next time...USE CODE TAGS!!! The directions are EVERYWHERE, I don't understand how you people miss them:

:code:

wordRec *head = NULL;
wordRec *newNode = NULL;

if (head == NULL) {


head is ALWAYS going to be NULL, because two lines prior you're setting it to NULL. I don't think that's what you want.
Was This Post Helpful? 0
  • +
  • -

#3 gronk  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 168
  • Joined: 28-October 09

Re: Struggling! Need help with insert into double linked list

Posted 01 November 2009 - 02:44 PM

Also I recommend working out on paper what you want to happen. Draw boxes to represent nodes in the linked list and arrows to represent the pointers. You'll get something like this:
+---+   +---+
|  N|-->|  N|->0
|P  |<--|P  |
+---+   +---+


(N=next, P=previous)
Then draw a new box to represent the new node you want to insert:
+---+   +---+
|  N|-->|   |
|   |<--|P  |
+---+   +---+

	+---+
	|  N|
	|P  |
	+---+


and work out how you want the pointers to change so that the new node is in the list. For example N on the top left will need to point to the new one, as will P on the top right. Then N and P on the new node will need pointing somewhere. This will be a lot easier with pencil and paper than ASCII art and trying to figure it out in your head. Keep track carefully of what you want to do, and you'll be ready to code it up. Also run through this for (a) an empty list, ( b ) inserting at the beginning and ( c) inserting at the end, to ensure you keep track of the N's and P's for all possible cases.

This post has been edited by gronk: 01 November 2009 - 02:46 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1