inserting into a double linked list
So I am to make a linked list that is sorted.
My idea of going about this was when I was inserting it searching where i need to insert it and then inserting it there.
what i have is 2 lists an even list and odd list. I am suppose to keep them seperate and dependign on how many times the word occurs in a letter it will end up in the even or odd list.
i have a working delete function.
actually everything works except the insert function.
struct ItemType
{
string word;
int occurance;
};
struct NodeType
{
ItemType info;
NodeType * next;
NodeType * prev;
};
void SortedList::InsertItem(ItemType& item)
{
location = headPtr;
//bool moreToSearch = true;
NodeType* newPtr;
newPtr = new NodeType;
if(location == NULL)//insert the first word in the list
{
newPtr ->prev = NULL;
newPtr ->next = NULL;
headPtr = newPtr;
}
else
{
cout <<"test 1"<<endl;
if((location->next->info.word > item.word)&& (location->info.word < item.word))//middle of list insert after
{
newPtr-> info = item;
newPtr -> next = location->next->prev;
newPtr -> prev = location;
location ->next->prev = newPtr;
location->next=newPtr;
// moreToSearch = false;
}
else if ((location->info.word < item.word) && (location->next ==NULL))//last in list
{
cout <<"test 2"<<endl;
newPtr -> info=item;
newPtr ->next= NULL;
newPtr ->prev = location;
location ->next = newPtr;
// moreToSearch = false;
}
else if (location->info.word >item.word) // insert the front of the list
{
newPtr ->prev = NULL;
newPtr ->next = location->next;
location->next->prev = newPtr;
headPtr = newPtr;
}
else
location = location ->next; // move to next location
}
}
of course there is more code i just posted what i thought was relivent.... if you need to see everything then just ask ill post it
My idea of going about this was when I was inserting it searching where i need to insert it and then inserting it there.
what i have is 2 lists an even list and odd list. I am suppose to keep them seperate and dependign on how many times the word occurs in a letter it will end up in the even or odd list.
i have a working delete function.
actually everything works except the insert function.
struct ItemType
{
string word;
int occurance;
};
struct NodeType
{
ItemType info;
NodeType * next;
NodeType * prev;
};
void SortedList::InsertItem(ItemType& item)
{
location = headPtr;
//bool moreToSearch = true;
NodeType* newPtr;
newPtr = new NodeType;
if(location == NULL)//insert the first word in the list
{
newPtr ->prev = NULL;
newPtr ->next = NULL;
headPtr = newPtr;
}
else
{
cout <<"test 1"<<endl;
if((location->next->info.word > item.word)&& (location->info.word < item.word))//middle of list insert after
{
newPtr-> info = item;
newPtr -> next = location->next->prev;
newPtr -> prev = location;
location ->next->prev = newPtr;
location->next=newPtr;
// moreToSearch = false;
}
else if ((location->info.word < item.word) && (location->next ==NULL))//last in list
{
cout <<"test 2"<<endl;
newPtr -> info=item;
newPtr ->next= NULL;
newPtr ->prev = location;
location ->next = newPtr;
// moreToSearch = false;
}
else if (location->info.word >item.word) // insert the front of the list
{
newPtr ->prev = NULL;
newPtr ->next = location->next;
location->next->prev = newPtr;
headPtr = newPtr;
}
else
location = location ->next; // move to next location
}
}
of course there is more code i just posted what i thought was relivent.... if you need to see everything then just ask ill post it
