operator [] linkedList
Is this the right way to to overloading for the operator[]?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
template<class T>
T LinkedList<T>::operator[](int index)
{
Node<T>* nodePtr;
Node<T>* nodeReturn;
int count = 0;
nodePtr = head;
if(nodePtr == NULL)
{
throw "The list is empty";
}
while(nodePtr != NULL)
{
count++;
if(index > count)
{
throw "Invalid index.";
}
}
nodePtr = head;
if((index > 0))
{
while(nodePtr != NULL)
{
if(count == index)
{
nodeReturn = nodePtr;
nodePtr = nodePtr->next;
return nodePtr->data;
}
count++;
}
}
else
{
throw "Invalid index.";
}
}
|
Topic archived. No new replies allowed.