Question
SEE BOLD FONT BELOW FOR QUESTION //METHOD 1 template T singlyLinkedList::getAtIndex(const unsigned int index)//getAtIndex { if (first == nullptr) { cout < < the list
SEE BOLD FONT BELOW FOR QUESTION
//METHOD 1
template
T singlyLinkedList::getAtIndex(const unsigned int index)//getAtIndex
{
if (first == nullptr)
{
cout << "the list is empty" << endl;
throw 1;
}
if (index >= count)
{
cout << "index is out of bounds" << endl;
throw 1;
}
if (index == 0)
{
return first->info;
}
nodeType* temp = first;
unsigned int track = 0;
while (track != index)
{
temp = temp->link;
track++;
}
return temp->info;
}
//METHOD 2
template
T& singlyLinkedList::operator[](const unsigned int index)
{
if (first == nullptr)
{
cout << "the list is empty" << endl;
throw 1;
}
if (index >= count)
{
cout << "index is out of bounds" << endl;
throw 1;
}
if (index == 0)
{
return first->info;
}
nodeType* temp = first;
int track = 0;
while (track != index)
{
temp = temp->link;
track++;
}
return temp->info;
}
//METHOD 3
template
void singlyLinkedList::insertAtIndex(const unsigned int index, const T& value)
{
if (index > count)
{
cout << "index is out of bounds" << endl;
}
//case where list is empty and index is 0
if (index == 0)
{
nodeType * newNode = new nodeType();
newNode->info = value;
nodeType* temp = newNode;
temp->link = first;
first = temp;
if (last == nullptr)
{
last = temp;
}
count++;
return;
}
//case where user wants to insert at the last index
if (index == count)
{
nodeType * newNode = new nodeType();
newNode->info = value;
last->link = newNode;
count++;
return;
}
//case where user wants to insert in the middel
nodeType * newNode = new nodeType();
newNode->info = value;
nodeType * temp = newNode;
nodeType * placer = first;
int tracker = 0;
while (tracker < index - 1)
{
placer = placer->link;
tracker++;
}
temp->link = placer->link;
placer->link = temp;
count++;
}
//METHOD 5
template
void singlyLinkedList::deleteAtIndex(const unsigned int index)
{
//empty list
if (first == nullptr)
{
cout << "the list is empty" << endl;
return;
}
//user asks for an index that does not exist
if (index > count)
{
cout << "index is out of bounds" << endl;
return;
}
//user wants to delete first item
if (index == 0)
{
nodeType* temp = first;
first = first->link;
delete temp;
if (count == 0)
{
first = nullptr;
last = nullptr;
count--;
return;
}
count--;
return;
}
//user wants to delete the last item
if (index == count - 1)
{
nodeType * temp = nullptr;
nodeType * cur = first;
int tracker = 0;
while (tracker < index)
{
temp = cur;
cur = cur->link;
tracker++;
}
delete cur;
last = temp;
last->link = nullptr;
count--;
return;
}
//user wants to delete an item in the middle
nodeType * temp = nullptr;
nodeType * cur = first;
int tracker = 0;
while (tracker < index)
{
temp = cur;
cur = cur->link;
tracker++;
}
temp->link = cur->link;
delete cur;
count--;
}
//METHOD 4
template
void singlyLinkedList::deleteAllInstances(const T& value)
{
nodeType * cur = first;
nodeType * trailer = nullptr;
while (cur != nullptr)
{
if (cur->info == value)
{
if (cur == first)
{
first = cur->link;
delete cur;
cur = first;
trailer = nullptr;
count--;
}
else
{
trailer->link = cur->link;
delete cur;
cur = trailer;
count--;
}
}
else
{
trailer = cur;
cur = cur->link;
}
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I need to now how to put these methods (above) in to this statement (C++) below:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//**********************************
//Write your code below here //**********************************
template class SinglyLinkedList : public SinglyLinkedListBase { public: //TODO: Declare your 5 methods here. void getAtIndex(const unsigned int index);
};
//TODO: Define your 5 methods here.
//********************************** //Write your code above here //**********************************
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started