template <class type>
class node
{
public:
type info;
node *link;
};
template <class type>
class Linkedlist
{
private:
node<type> *head, *tail;
int counter;
public:
Linkedlist()
{
head = tail = NULL;
counter = 0;
}
bool isEmpty()
{
return (head==NULL);
}
type getLast()
{
if (isEmpty()) {
cout << "Linked list is empty !!!" << endl; assert(!isEmpty());
}
return tail->info;
}
void Append(type e)
{
node<type> *newnode = new node<type>;
newnode->info = e;
newnode->link = NULL;// (*newnode).link=NULL
if (isEmpty())
{
head = tail = newnode;
}
else
{
tail->link = newnode;
tail = newnode;
}
++counter;
}
void print()
{
if (isEmpty()) { cout << "Linked list is empty !!!" << endl; return; }
node<type> *current = head;
while (current != NULL)
{
cout << current->info << ",";
current = current->link;
}
cout << endl;
}
void Destroy()
{
node<type> *temp;
while (head != NULL)
{
temp = head;
head = head->link;
delete temp;
}
tail = NULL;
counter = 0;
}
bool search(type e)
{
bool found = false;
node<type> * current = head;
while (current != NULL && !found)
{
if (current->data == e) found = true;
else
current = current->link;
}
if (!found) returnfalse;
elsereturntrue;
}
~LinkedList()
{
Destroy();
}
};
Extend the class Linkedlist by adding the following methods:
Average method .This method should return the average of the elements in the linked-list. (5 points)
DeleteAtIndex method .This method should delete a node at a particular index. Hint: Suppose the index of the linked-list starts with 1 (5 points)
Delete method. This method should delete all occurrence of an element (value) from a linked-list. (6 points)
For the provided implementation, there are some issues. A copy constructor and operator= are required (or the compiler generated ones deleted as they do a shallow copy rather than the required deep copy). nullptr should be used instead of NULL.