My implementation is only for study purposes, I agree that some of the features may be counter-intuitive.
I guess the best method is to use a list in the STL library.
Any improvements will be greatly appreciated.
The following code compiles and works, If possible I would like the code reviewed in C++98, If not the closest to him which is C++03;
#include <iostream>
using namespace std;
template <class T>
struct node {
node(T data) : data(data), next(NULL) {}
T data;
node<T> *next;
};
template <class T>
class linkedlist {
template<class U>
friend ostream &operator<<(ostream &os, const linkedlist<U> &rhs);
private:
node<T> *head;
public:
linkedlist() : head(NULL) {};
~linkedlist();
linkedlist(const linkedlist &rhs);
linkedlist &operator=(const linkedlist &rhs);
void insert(const T data);
bool search(const T data) const;
void remove(const T data);
bool isEmpty() const;
bool operator==(const linkedlist<T> &rhs) const;
bool operator!=(const linkedlist<T> &rhs) const;
bool operator<(const linkedlist<T> &rhs) const;
bool operator>(const linkedlist<T> &rhs) const;
};
template <class T>
bool linkedlist<T>::operator!=(const linkedlist<T> &rhs) const
{
return !(*this == rhs);
}
template<class T>
bool linkedlist<T>::operator<(const linkedlist<T>& rhs) const
{
node<T> *lhsTemp = head;
node<T> *rhsTemp = rhs.head;
while (lhsTemp != NULL && rhsTemp != NULL)
{
if (lhsTemp->data > rhsTemp->data)
return false;
lhsTemp = lhsTemp->next;
rhsTemp = rhsTemp->next;
}
return true;
}
template<class T>
bool linkedlist<T>::operator>(const linkedlist<T>& rhs) const
{
return rhs < *this;
}
template <class T>
bool linkedlist<T>::operator==(const linkedlist<T> &rhs) const
{
node<T> *lhsTemp = head;
node<T> *rhsTemp = rhs.head;
while (lhsTemp != NULL || rhsTemp != NULL)
{
if (lhsTemp != NULL && rhsTemp == NULL || lhsTemp == NULL && rhsTemp != NULL)
return false;
else if (lhsTemp->data != rhsTemp->data)
return false;
lhsTemp = lhsTemp->next;
rhsTemp = rhsTemp->next;
}
return true;
}
template<class T>
linkedlist<T>::~linkedlist()
{
while (head != NULL)
{
node<T> *tmp = head;
head = head->next;
delete tmp;
}
delete head;
}
template<class T>
linkedlist<T>::linkedlist(const linkedlist & rhs) : head(NULL)
{
*this = rhs;
}
template <class T>
linkedlist<T> & linkedlist<T>::operator=(const linkedlist<T> &rhs)
{
if (this != &rhs)
{
node<T> *temp;
while (head != NULL)
{
temp = head;
head = head->next;
delete temp;
}
if (rhs.head != NULL)
head = new node<T>(rhs.head->data);
node<T> *tmpHead = head;
for (node<T> *tmp = rhs.head->next; tmp != NULL; tmp = tmp->next)
{
tmpHead->next = new node<T>(tmp->data);
tmpHead = tmpHead->next;
}
}
return *this;
}
template<class T>
void linkedlist<T>::insert(const T data)
{
if (head == NULL)
{
head = new node<T>(data);
}
else
{
node<T> *temp = head;
for (temp = head; temp->next != NULL; temp = temp->next);
temp->next = new node<T>(data);
}
}
template<class T>
bool linkedlist<T>::search(const T data) const
{
if (head == NULL)
return false;
for (node<T> *tmp = head; tmp != NULL; tmp = tmp->next)
if (tmp->data == data)
return true;
return true;
}
template<class T>
void linkedlist<T>::remove(const T data)
{
bool removed = false;
node<T> *curr = head;
node<T> *prev = head;
for (; curr != NULL && removed == false; curr = curr->next)
{
if (head->data == data)
{
node<T> *tmp = head;
head = head->next;
delete tmp;
removed = true;
}
else if (curr->data == data)
{
node<T> *tmp = curr;
prev->next = curr->next;
delete tmp;
removed = true;
}
prev = curr;
}
}
template<class T>
bool linkedlist<T>::isEmpty() const
{
return head == NULL;
}
template<class T>
ostream & operator<<(ostream & os, const linkedlist<T>& rhs)
{
for (node<T> *temp = rhs.head; temp != NULL; temp = temp->next)
{
os << temp->data;
if (temp->next != NULL)
os << ", ";
}
return os;
}
using
declarations. Please post complete code. Also, does this actually compile?return true
in avoid
function should give you at least a warning. You tried all functions at least once to instantiate the template, right? Also, all of this is in a single header file, isn't it? \$\endgroup\$T
. Minimal example to show failing code:int main() { linkedlist<int> int_list; int_list.search(10); }
. \$\endgroup\$