#include <iostream> #include <cstring> #include <string> #include <cstddef>//for NULL using namespace std; class SortedList { private: struct SortedListNode { string word;//holds within the word in the node SortedListNode* next;//pointer to next node }; int size;//number of nodes (words) in the sorted list SortedListNode* head;//pointer to the sorted linked list of words //optional: SortedListNode* tail;//pointer to th //SortedListNode* locatePosition(string aWord, public: SortedList(); ~SortedList(); bool sortedIsEmpty()const; int sortedGetLength()const; void sortedInsert(string newWord); void sortedRemove(string oldWord); void sortedRetrieve(int index, string& theWord)const; friend bool operator==(const SortedListNode& wordA, const SortedListNode& wordB); }; SortedList::SortedList(): size(0), head(NULL) {} SortedList::~SortedList() { delete head;//and not to forget, use size to walk through the sorted list and delete all Nodes } bool SortedList::sortedIsEmpty() const { return size;//if size is 0, it returns 0, if size is any other number- it returns 1 } int SortedList::sortedGetLength()const { return size; } void SortedList::sortedInsert(string newWord) { SortedListNode* newNodePtr = new SortedListNode; newNodePtr->next = NULL; newNodePtr->word = newWord; if(size==0)//insertion at beginning of List { newNodePtr->next = head; head = newNodePtr; size++; } else//insertion not at beginning of list { SortedListNode* prev = NULL; SortedListNode* cur = head; //for(; cur->next != NULL; prev = cur, cur = cur->next);//for(int i = size; i <= size; i++) //{ //if (newNodePtr->word == cur->word); //throw exception- already present //if (newNodePtr->word > cur->word) //{ // if(cur->next = NULL);// then its the last one on list- insert here // } //else //{ //advance by 1 more node // } //if newNodePtr->word < cur->word then prev->next = newNodePtr and newNodePtr->next = cur (insert new node here) //} } } bool SortedList::operator==(const SortedListNode*& wordA, const SortedListNode*& wordB) { string first = wordA->word; string second = wordB->word; const char* forFirst = first.c_str(); const char* forSecond = second.c_str(); return strcmpi(forFirst, forSecond); }
#include <algorithm> for the transform() function
ortedListNode#include <iostream>
#include <cstring>
#include <string>
#include <cstddef>//for NULL
#include <algorithm>
using namespace std;
class SortedList
{
private:
struct SortedListNode
{
string word;//holds within the word in the node
SortedListNode* next;//pointer to next node
};
int size;//number of nodes (words) in the sorted list
SortedListNode* head;//pointer to the sorted linked list of words
//optional: SortedListNode* tail;//pointer to th
//SortedListNode* locatePosition(string aWord,
public:
SortedList();
~SortedList();
int sortedIsEmpty()const;
int sortedGetLength()const;
void sortedInsert(string newWord);
void sortedRemove(string oldWord);
void sortedRetrieve(int index, string& theWord)const;
// Note: if you want to pass pointers to this function
// then change it to * instead of &, but not both.
friend bool operator==(const SortedList::SortedListNode& wordA, const SortedList::SortedListNode& wordB);
};
SortedList::SortedList(): size(0), head(NULL)
{}
SortedList::~SortedList()
{
delete head;//and not to forget, use size to walk through the sorted list and delete all Nodes
}
int SortedList::sortedIsEmpty() const
{
return size;//if size is 0, it returns 0, if size is any other number- it returns 1
}
int SortedList::sortedGetLength()const
{
return size;
}
void SortedList::sortedInsert(string newWord)
{
SortedListNode* newNodePtr = new SortedListNode;
newNodePtr->next = NULL;
newNodePtr->word = newWord;
if(size==0)//insertion at beginning of List
{
newNodePtr->next = head;
head = newNodePtr;
size++;
}
else//insertion not at beginning of list
{
SortedListNode* prev = NULL;
SortedListNode* cur = head;
//for(; cur->next != NULL; prev = cur, cur = cur->next);//for(int i = size; i <= size; i++)
//{
//if (newNodePtr->word == cur->word); //throw exception- already present
//if (newNodePtr->word > cur->word)
//{
// if(cur->next = NULL);// then its the last one on list- insert here
// }
//else
//{
//advance by 1 more node
// }
//if newNodePtr->word < cur->word then prev->next = newNodePtr and newNodePtr->next = cur (insert new node here)
//}
}
}
// Note: See my node in the class above.
bool operator==(const SortedList::SortedListNode& wordA, const SortedList::SortedListNode& wordB)
{
string a = wordA.word;
string b = wordB.word;
transform(a.begin(), a.end(), a.begin(), toupper);
transform(b.begin(), b.end(), b.begin(), toupper);
return a == b ? true : false;
}1) get rid of strcmpi() C function. See my version of the == operator below.
2) add#include <algorithm>for the transform() function
3) The friend function is not part of the class, so it has to reference the SortedListNode by SortedList:ortedListNode
#include <iostream> #include <cstring> #include <string> #include <cstddef>//for NULL #include <algorithm> using namespace std; class SortedList { private: struct SortedListNode { string word;//holds within the word in the node SortedListNode* next;//pointer to next node }; int size;//number of nodes (words) in the sorted list SortedListNode* head;//pointer to the sorted linked list of words //optional: SortedListNode* tail;//pointer to th //SortedListNode* locatePosition(string aWord, public: SortedList(); ~SortedList(); int sortedIsEmpty()const; int sortedGetLength()const; void sortedInsert(string newWord); void sortedRemove(string oldWord); void sortedRetrieve(int index, string& theWord)const; // Note: if you want to pass pointers to this function // then change it to * instead of &, but not both. friend bool operator==(const SortedList::SortedListNode& wordA, const SortedList::SortedListNode& wordB); }; SortedList::SortedList(): size(0), head(NULL) {} SortedList::~SortedList() { delete head;//and not to forget, use size to walk through the sorted list and delete all Nodes } int SortedList::sortedIsEmpty() const { return size;//if size is 0, it returns 0, if size is any other number- it returns 1 } int SortedList::sortedGetLength()const { return size; } void SortedList::sortedInsert(string newWord) { SortedListNode* newNodePtr = new SortedListNode; newNodePtr->next = NULL; newNodePtr->word = newWord; if(size==0)//insertion at beginning of List { newNodePtr->next = head; head = newNodePtr; size++; } else//insertion not at beginning of list { SortedListNode* prev = NULL; SortedListNode* cur = head; //for(; cur->next != NULL; prev = cur, cur = cur->next);//for(int i = size; i <= size; i++) //{ //if (newNodePtr->word == cur->word); //throw exception- already present //if (newNodePtr->word > cur->word) //{ // if(cur->next = NULL);// then its the last one on list- insert here // } //else //{ //advance by 1 more node // } //if newNodePtr->word < cur->word then prev->next = newNodePtr and newNodePtr->next = cur (insert new node here) //} } } // Note: See my node in the class above. bool operator==(const SortedList::SortedListNode& wordA, const SortedList::SortedListNode& wordB) { string a = wordA.word; string b = wordB.word; transform(a.begin(), a.end(), a.begin(), toupper); transform(b.begin(), b.end(), b.begin(), toupper); return a == b ? true : false; }
| DaniWeb Message | |
| Cancel Changes | |