In: Computer Science
Can anyone make a documentation and comments for the following code? Documentation in doxgen format please. Thank you!
And simple comments for the code. Thank s.
template <typename T>
bool LinkedList<T> :: search(const T& item) const
{
// if list is not empty display the items
Node<T>* current = first;
while (current != NULL)
{
if (current->info == item)
{
return true;
}
current = current->link;
}
return false;
}
template <typename T>
void LinkedList<T> :: deleteNode(const T& item)
{
Node<T>* current = first;
Node<T>* previous = NULL;
while (current != NULL)
{
if (current->info == item)
{
if (previous == NULL)
{
first = first->link;
}
else
{
previous->link = current->link;
if (previous->link == NULL)
{
last = previous;
}
}
free(current);
count--;
return;
}
previous = current;
current = current->link;
}
throw LinkedListItemNotFoundException();
}
template <typename T>
T& LinkedList<T> :: findItemAtIndex(int index)
{
int i = 0;
Node<T>* current = first;
while (current != NULL)
{
if (i == index)
{
return current->info;
}
current = current->link;
i++;
}
throw LinkedListItemNotFoundException();
}
template <typename T>
T& LinkedList<T> :: operator[](int index)
{
return findItemAtIndex(index);
}
template <typename T>
void LinkedList<T> :: deleteItemAtIndex(int index)
{
T item = findItemAtIndex(index);
deleteNode(item);
}
/************************************
*************************************
*Implementation of linked list
*Elements are stored in Node
*Contains all the function definitins
*of the linked list
*************************************
************************************/
//______________________________
//search an item in the linked list
//if item found then return true otherwise
//return false
template <typename T>
bool LinkedList<T> :: search(const T& item) const{
   // if list is not empty display the items
   //store the address of first in the temporary
   //variable current
   Node<T>* current = first;
   //linear search the entire linked list
   while (current != NULL){
       //item found
       if (current->info ==
item){
           return
true;
       }
       current = current->link;
   }
   //item not found
   return false;
}
//______________________________
template <typename T>
void LinkedList<T> :: deleteNode(const T& item){
   //temporary variables for traversing
   Node<T>* current = first;
   Node<T>* previous = NULL;
   //linear search the entire linked list
   while (current != NULL){
       //item found
       if (current->info ==
item){
           //node to be
deleted is the first node
           if (previous ==
NULL){
          
    first = first->link;
           }
           else{
          
    //node to be deleted is the intermediate
node
          
    previous->link = current->link;
          
    //node to be deleted is the last node
          
    if (previous->link == NULL){
          
        last = previous;
          
    }
           }
           //deallocate
memory
          
free(current);
           //decrease
number of items
           count--;
           //return the
control after successful deletion
           return;
       }
       //if item not found traverse linked
list further
       previous = current;
       current = current->link;
   }
   //excption for item not found in linked list
   throw LinkedListItemNotFoundException();
}
//_____________________________________
//finds an item at a particular index
//return the reference of the item
template <typename T>
T& LinkedList<T> :: findItemAtIndex(int index){
   int i = 0;
   Node<T>* current = first;
   //traverse until the index is found
   while (current != NULL){
       //index found
       if (i == index){
           //return the
item
           return
current->info;
       }
       //item not found yet traverse
further
       current = current->link;
       i++;
   }
   //excption for item not found in linked list
   throw LinkedListItemNotFoundException();
}
//______________________________
//overload [] opearator to access linked list using index
//like l[10]
template <typename T>
T& LinkedList<T> :: operator[](int index){
   //call the already written function
findItemAtIndex(index)
   return findItemAtIndex(index);
}
//__________________________________
//delete an item at the particular index of the linked list
template <typename T>
void LinkedList<T> :: deleteItemAtIndex(int index){
   //get the reference of the item using already written
function
   //findItemAtIndex(index)
   T item = findItemAtIndex(index);
   //deallocate memory
   deleteNode(item);
}
/***********************************************
*end of the function definitions of linked list
************************************************/