I'm confused as to why this isnt working.. am i doing this right?? I have to create a function that does the following:
- Insert a word to the linked list so that the list
// remains sorted alphabetically.
// PRE: The list is sorted alphabetically or is NULL
// POST: The resulting list is sorted alphabetically. If the word is
// in the list already, do nothing.
struct WordNode {
string word;
WordNode *link;
};
here is what i have........
{
while(!list)
{
WordNode *tempNode = new WordNode;
tempNode->word=word;
tempNode->link=list;
list=tempNode;
}
// do a deep copy of list to current
WordNode *current, *lastNode;
current = new(WordNode);
current->word = list->word;
current ->link = NULL;
lastNode = current;
list = list -> link;
while (list!=NULL)
{
WordNode *p = new(WordNode);
p -> word = list -> word;
p->link = NULL;
lastNode -> link = p;
lastNode = p;
list = list -> link;
}
// now sort in alphabetical order
WordNode *last;
while(current!= NULL && current->word<word)
{
last=current;
current=current->link;
}
WordNode*tempNo de = new WordNode;
tempNode->word=word;
//cout << "temp-> word " << tempNode->word << endl;
last->link=tempNod e;
tempNode->link=current ;}
Am I way off track??? it keeps coming up with an error when I try to compile. I can see the problem lies somewhere within
last->link=tempNode. .......
????
- Insert a word to the linked list so that the list
// remains sorted alphabetically.
// PRE: The list is sorted alphabetically or is NULL
// POST: The resulting list is sorted alphabetically. If the word is
// in the list already, do nothing.
struct WordNode {
string word;
WordNode *link;
};
here is what i have........
{
while(!list)
{
WordNode *tempNode = new WordNode;
tempNode->word=word;
tempNode->link=list;
list=tempNode;
}
// do a deep copy of list to current
WordNode *current, *lastNode;
current = new(WordNode);
current->word = list->word;
current ->link = NULL;
lastNode = current;
list = list -> link;
while (list!=NULL)
{
WordNode *p = new(WordNode);
p -> word = list -> word;
p->link = NULL;
lastNode -> link = p;
lastNode = p;
list = list -> link;
}
// now sort in alphabetical order
WordNode *last;
while(current!= NULL && current->word<word)
{
last=current;
current=current->link;
}
WordNode*tempNo de = new WordNode;
tempNode->word=word;
//cout << "temp-> word " << tempNode->word << endl;
last->link=tempNod e;
tempNode->link=current ;}
Am I way off track??? it keeps coming up with an error when I try to compile. I can see the problem lies somewhere within
last->link=tempNode. .......
????
Comment