I have re-implemented Singly Linked List based on previous review on this website. I have added iterators and remove mistakes. I did not use template classes because I have not learned about them yet.
SinglyList.hpp
#ifndef SINGLYLIST_HPP
#define SINGLYLIST_HPP
class Iterator;
class SinglyList
{
private:
struct Node { int data; Node *next; };
Node *head;
public:
SinglyList(); //constructor
~SinglyList(); //distructor
SinglyList(const SinglyList& rhs); //copy constructor
SinglyList& operator=(const SinglyList& rhs); //assignment overload
bool empty(); //check if list is empty
int& front(); //return refrence to first element
void pop_back(); //pop last element
void pop_front(); //pop first element
unsigned int size(); //return size of list
void remove(int value); //remove passed value from list
bool search(int value); //return true if passed value found and vice versa
void push_back(int value); //add an element at the end of list
void push_front(int value); //add an element at the start of list
Iterator begin() const; //begin iterator
Iterator end() const; //end iterator
friend class Iterator; //This class is friend of Iterator class
};
class Iterator
{
private:
SinglyList::Node *curr;
public:
Iterator(); //constructor
Iterator(SinglyList::Node *rhs); //parameterized constructor
int& operator *() const; //reference operator overload
Iterator operator++ (int); //post increment operator overload
bool operator!=(const Iterator& rhs); //not equal operator overload
Iterator& operator=(const Iterator& rhs); //assignment overload
friend class SinglyList; //This class is friend of SinglyList class
};
#endif // SINGLYLIST_HPP
SinglyList.cpp
#include "SinglyList.hpp"
SinglyList::SinglyList()
{
head = nullptr;
}
SinglyList::SinglyList(const SinglyList& rhs)
{
Node *temp = rhs.head, *newer = new Node;
head = newer;
while (temp != nullptr)
{
newer->data = temp->data;
temp = temp->next;
if (temp != nullptr)
{
newer->next = new Node;
newer = newer->next;
}
else
newer->next = nullptr;
}
}
SinglyList& SinglyList::operator=(const SinglyList& rhs)
{
if(head != nullptr)
{
Node *prev = nullptr;
while(head != nullptr)
{
prev = head;
head = head->next;
delete prev;
}
}
Node *temp = rhs.head, *newer = new Node;
head = newer;
while (temp != nullptr)
{
newer->data = temp->data;
temp = temp->next;
if (temp != nullptr)
{
newer->next = new Node;
newer = newer->next;
}
else
newer->next = nullptr;
}
return *this;
}
SinglyList::~SinglyList()
{
Node* prev = nullptr;
while (head != nullptr)
{
prev = head;
head = head->next;
delete prev;
}
}
bool SinglyList::empty()
{
return head == nullptr;
}
int& SinglyList::front()
{
return head->data;
}
unsigned int SinglyList::size()
{
Node* temp = head;
unsigned int count = 0;
while (temp != nullptr)
{
temp = temp->next;
count++;
}
return count;
}
void SinglyList::push_front(int value)
{
Node* newer = new Node;
newer->data = value;
if (empty())
{
head = newer;
newer->next = nullptr;
}
else
{
newer->next = head;
head = newer;
}
}
void SinglyList::push_back(int value)
{
Node* newer = new Node;
newer->data = value;
if (empty())
{
newer->next = nullptr;
head = newer;
}
else
{
Node* temp = head;
while (temp->next != nullptr)
{
temp = temp->next;
}
newer->next = nullptr;
temp->next = newer;
}
}
void SinglyList::remove(int value)
{
if(!empty())
{
Node *temp = head, *prev = nullptr;
if (temp->data == value)
head = temp->next;
else
{
while (temp->next != nullptr && temp->data != value)
{
prev = temp;
temp = temp->next;
}
if (temp->data == value)
prev->next = temp->next;
}
delete temp;
}
}
bool SinglyList::search(int value)
{
if (empty())
return false;
else
{
bool check = false;
Node* temp = head;
while (temp != nullptr)
{
if (temp->data == value)
{
check = true;
break;
}
else
temp = temp->next;
}
return check;
}
}
void SinglyList::pop_front()
{
Node* temp = head;
head = temp->next;
delete temp;
}
void SinglyList::pop_back()
{
if(!empty())
{
Node *temp = head, *prev = nullptr;
if (head->next == nullptr)
head = temp->next;
else
{
while (temp->next != nullptr)
{
prev = temp;
temp = temp->next;
}
prev->next = nullptr;
}
delete temp;
}
}
Iterator SinglyList::begin() const
{
return Iterator(head);
}
Iterator SinglyList::end() const
{
Node *temp = head;
while(temp != nullptr)
{
temp=temp->next;
}
return Iterator(temp);
}
Iterator::Iterator()
{
curr = nullptr;
}
Iterator::Iterator(SinglyList::Node *rhs)
{
curr = rhs;
}
Iterator& Iterator::operator=(const Iterator& rhs)
{
curr = rhs.curr;
return *this;
}
bool Iterator::operator !=(const Iterator& rhs)
{
return curr != rhs.curr;
}
int& Iterator::operator *() const
{
return curr->data;
}
Iterator Iterator::operator ++(int)
{
curr = curr->next;
return *this;
}
main.cpp
#include "SinglyList.hpp"
#include <iostream>
int main()
{
SinglyList l1;
l1.push_back(11);
l1.push_front(10);
l1.push_front(9);
l1.push_front(8);
Iterator it;
for(it = l1.begin(); it != l1.end(); it++)
{
std::cout << *it << " ";
}
std::cout << '\n';
}