Restruct list iterator after erase - c++

I've created a custom Node, Iterator and List.
I would like to do:
List<int> list;
// push_back objects here...
List<int>::Iterator it = list.begin();
list.erase(it);
++it;
list.erase(it); // error here
So I need to reconstruct the iterator after erasing an element. How do I do that?
Node.h
#pragma once
namespace Util
{
template<typename T>
class Node
{
public:
template<typename T> friend class Iterator;
template<typename T> friend class List;
private:
Node();
Node(T);
~Node();
/* unlink(): takes out this node
and links next and prev to each other
prev <- this -> next
prev <- -> next
this <- this -> this
*/
void unlink();
private:
T value;
Node<T>* next;
Node<T>* prev;
};
template<typename T>
Node<T>::Node() : next(this), prev(this)
{
// ...
}
template<typename T>
Node<T>::Node(T t) : value(t), next(this), prev(this)
{
// ...
}
template<typename T>
Node<T>::~Node()
{
unlink();
}
template<typename T>
void Node<T>::unlink()
{
next->prev = prev;
prev->next = next;
next = this;
prev = this;
}
}
Iterator.h
#pragma once
#include "Node.h"
namespace Util
{
template<typename T>
class Iterator
{
public:
template<typename T> friend class List;
Iterator& operator++();
T& operator*() const;
bool operator==(const Iterator& rhs) const;
bool operator!=(const Iterator& rhs) const;
private:
Iterator(Node<T>* n);
Node<T>* node;
};
template<typename T>
Iterator<T>::Iterator(Node<T>* n) : node(n)
{
// ...
}
template<typename T>
Iterator<T>& Iterator<T>::operator++()
{
node = node->next;
return *this;
}
template<typename T>
T& Iterator<T>::operator*() const
{
return node->value;
}
template<typename T>
bool Iterator<T>::operator==(const Iterator& rhs) const
{
return node == rhs.node;
}
template<typename T>
bool Iterator<T>::operator!=(const Iterator& rhs) const
{
return node != rhs.node;
}
}
List.h
#pragma once
#include "Iterator.h"
namespace Util
{
template<typename T>
class List
{
public:
typedef Iterator<T> Iterator;
typedef Node<T> Node;
List();
~List();
// Capacity
bool empty() const;
int size() const;
// Modifiers
void push_back(const T&);
void push_front(const T&);
void pop_back();
void pop_front();
Iterator erase(Iterator it);
// Element access
Iterator begin() const;
Iterator end() const;
private:
Node* head;
int list_size;
};
template<typename T>
List<T>::List() : list_size(0)
{
head = new Node();
}
template<typename T>
List<T>::~List()
{
while (!empty()) pop_back();
delete head;
}
template<typename T>
bool List<T>::empty() const
{
return head->next == head;
}
template<typename T>
int List<T>::size() const
{
return list_size;
}
template<typename T>
void List<T>::push_back(const T& t)
{
Node* n = new Node(t);
n->next = head;
n->prev = head->prev;
head->prev->next = n;
head->prev = n;
++list_size;
}
template<typename T>
void List<T>::push_front(const T& t)
{
Node* n = new Node(t);
n->prev = head;
n->next = head->next;
head->next->prev = n;
head->next = n;
++list_size;
}
template<typename T>
void List<T>::pop_back()
{
if (head->prev == head) return;
delete head->prev;
--list_size;
}
template<typename T>
void List<T>::pop_front()
{
if (head->next == head) return;
delete head->next;
--list_size;
}
template<typename T>
typename List<T>::Iterator List<T>::erase(Iterator it)
{
if (it.node == head) return it;
Node* n = it.node->next;
delete it.node;
--list_size;
return Iterator(n);
}
template<typename T>
typename List<T>::Iterator List<T>::begin() const
{
return Iterator(head->next);
}
template<typename T>
typename List<T>::Iterator List<T>::end() const
{
return Iterator(head);
}
}
Edit: after updating the code based on the comments I received. Going through the list and erasing, only erases every other element; since the loop is incrementing the iterator and erase makes the iterator's current object iterator's next object. Is this how it is in the standard as well?
for (Util::List<int>::Iterator it = list.begin(); it != list.end(); ++it)
{
it = list.erase(it);
}
Old list: 0 1 2 3 4 5 6 7 8 9
After erasing: 1 3 5 7 9
Please do not complain about rule of five and things unrelated to the question. I haven't gotten to every part yet. My justification of making an edit over posting a new question is that the edit is still about how to reconstruct the iterator correctly after erase.

As some people already mentioned, erase typically returns a new, valid iterator, if that is possible/feasible for the container.
You can define the semantics as you would like, but typically you would erase the element by pointing prev->next to next and next->prev to prev, then deallocate the node, and return an iterator to next. You could also return an iterator to prev, but that results in several counter-intuitive behaviors, such as having to advance the iterator if you are doing an operation on some sort of range. Returning an iterator to next, just lets you delete ranges with while(foo(it)) it = c.erase(it);
In that case you of course don't increment the iterator anymore, unless you wan t to skip every other element.

Related

How to make my doubly Linked list printBackward() Function Print all numbers on C++?

I studied about Doubly Linked list and got stuck on how to make my "PrintBackward" Function print all number
The input is 1 2 3
The Output I want is 3 2 1
But my Output now is resulted on 3 2 only
Here's my code. Can anyone tell me what causes this problem and a hint for Solution?
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
class Node{
public:
int data;
Node* next;
Node* prev;
};
void printList(Node* n)
{
while (n != NULL) {
cout << n->data << " ";
n = n->next;
}
}
void printBackward(Node* n)
{
while (n != NULL) {
cout << n->data << " ";
n = n->prev;
}
}
void push(Node** head_ref,Node** tail_ref, int new_data)
{
/* 1. allocate node */
Node* new_node = new Node();
/* 2. put in the data */
new_node->data = new_data;
/* 3. Make next of new node as head */
new_node->next = (*head_ref);
new_node->prev = *tail_ref;
//update first node to point to newnode
if(*head_ref != NULL){
(*head_ref)->prev = new_node;
}
/* 4. move the head to point to the new node */
(*head_ref) = new_node;
/*5. update tail for the first node*/
if(*tail_ref == NULL){
*tail_ref = new_node;
}
}
void appendTail(Node** head_ref,Node** tail_ref , int new_data)
{
// 1. allocate node
Node* new_node = new Node();
// 2. Put in the data
new_node->data = new_data;
// 3. This new node is going to be
// the last node, so make next of
// it as NULL
new_node->next = NULL;
new_node->prev = *tail_ref;
// 4. If the Linked List is empty,
// then make the new node as head
if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}
// 6. Change the next of last node
if(*tail_ref != NULL){
(*tail_ref)->next = new_node;
}
*tail_ref = new_node;
return;
}
int main(int argc, char** argv) {
Node* head = NULL;
Node* tail = NULL;
appendTail(&head, &tail, 1);
appendTail(&head, &tail, 2);
appendTail(&head, &tail, 3);
printBackward(tail);
return 0;
}
Now I tried using AppendTail to add the numbers to make a list of 1 2 3
I want to make it print out in backward like 3 2 1
Maybe I forget to link one list? The list won't print out the one with NULL on the head or something? I tried find the solution everywhere and now I'm totally strucked.
You have several issues in your code. But more important are the problems with the design.
Strictly spoken, you do not have a linked list. There is no object "list" or similar that could be instantiated. You have only "nodes" that you try link with external functions.
Look at the interface of std::list. Then you interstand what I mean.
Then, in your "appendTail", you do not handle the pointer correctly. Especially, if you add the fist node. There you just set the head pointer and not the tail pointer. That is wrong. And all following tries to add something to the list, will fail.
If you have 1 element in the list. Then head and tail pointer must point to the same element.
In your case the links are broken.
After adding 1,2 and 3 the pointers will look like:
You see, you need to redesign your push and appenTail functions. Or better, the complete design . . .
And then refactor your code.
By the way. Standard implementations of a doubly linked list use a node as a sentinel. "next" and "previous" pointers in this node are used as head and tail pointers. This makes navigation much more simply. There is no need to differentiate the special cases of handling the start and tail node.
And travisering is usually done with iterators.
You may study the following example of a doubly link list
#include <iterator>
#include <initializer_list>
#include <algorithm>
#include <iostream>
#include <type_traits>
#include <vector>
// ------------------------------------------------------------------------------------------------
// This would be in a header file -----------------------------------------------------------------
// Type trait helper to identify iterators --------------------------------------------------------
template<typename T, typename = void>
struct is_iterator { static constexpr bool value = false; };
template<typename T>
struct is_iterator<T, typename std::enable_if<!std::is_same<typename std::iterator_traits<T>::value_type, void>::value>::type> {
static constexpr bool value = true;
};
// The List class ---------------------------------------------------------------------------------
template <typename T>
class List {
// Sub class for a Node -----------
struct Node {
T data{};
Node* next{};
Node* previous{};
Node() {}
Node(Node* const n, Node* const p) : next(n), previous(p) {}
Node(Node* const n, Node* const p, const T& d) : next(n), previous(p), data(d) {}
};
// Private list data and functions --------
Node* head{};
size_t numberOfElements{};
void init() { head = new Node(); head->next = head; head->previous = head; numberOfElements = 0; }
public:
struct iterator; // Forward declaration
// Constructor --------------------
List() { init(); }
explicit List(const size_t count) { init(); insert(begin(), count); }
explicit List(const size_t count, const T& value) { init(); insert(begin(), count, value); };
template <typename Iter>
List(const Iter& first, const Iter& last) { init(); insert(begin(), first, last); }
List(const List& other) { init(), insert(begin(), other.begin(), other.end()); };
List(List&& other) : head(other.head), numberOfElements(other.numberOfElements) { other.init(); }
List(const std::initializer_list<T>& il) { init(); insert(begin(), il.begin(), il.end()); }
template <int N> List(T(&other)[N]) { init(); insert(begin(), std::begin(other), std::end(other)); }
template <int N> List(const T(&other)[N]) { init(); insert(begin(), std::begin(other), std::end(other)); }
// Assignment ---------------------
List& operator =(const List& other) { clear(); insert(begin(), other.begin(), other.end()); return *this; }
List& operator =(List&& other) { clear(); head = other.head; numberOfElements = other.numberOfElements; other.init(); return *this; }
List& operator =(const std::initializer_list<T>& il) { clear(); insert(begin(), il.begin(), il.end()); return *this; }
template <int N> List& operator =(const T(&other)[N]) { clear(); insert(begin(), std::begin(other), std::end(other)); return *this; }
template <int N> List& operator =(T(&other)[N]) { clear(); insert(begin(), std::begin(other), std::end(other)); return *this; }
template <typename Iter> void assign(const Iter& first, const Iter& last) { clear(); insert(begin(), first, last); }
template <int N> void assign(const T(&other)[N]) { clear(); insert(begin(), std::begin(other), std::end(other)); return *this; }
template <int N> void assign(T(&other)[N]) { clear(); insert(begin(), std::begin(other), std::end(other)); return *this; }
void assign(const size_t count, const T& value) { clear(); insert(begin(), count, value); }
void assign(const std::initializer_list<T>& il) { clear(); insert(begin(), il.begin(), il.end()); }
// Destructor ---------------------
~List() { clear(); delete head; }
// Element Access -----------------
T& front() { return *begin(); }
T& back() { return *(--end()); }
// Iterators ----------------------
iterator begin() const { return iterator(head->next, head); }
iterator end() const { return iterator(head, head); }
// Capacity -----------------------
size_t size() const { return numberOfElements; }
bool empty() { return size() == 0; }
// Modifiers ----------------------
void clear();
iterator insert(const iterator& insertBeforePosition, const T& value);
iterator insert(const iterator& insertBeforePosition);
template <class Iter, std::enable_if_t<is_iterator<Iter>::value, bool> = true>
iterator insert(const iterator& insertBeforePosition, const Iter& first, const Iter& last);
iterator insert(const iterator& insertBeforePosition, const size_t& count, const T& value);
iterator insert(const iterator& insertBeforePosition, const std::initializer_list<T>& il);
iterator erase(const iterator& posToDelete);
iterator erase(const iterator& first, const iterator& last);
void pop_front() { erase(begin()); };
void push_front(const T& d) { insert(begin(), d); }
void pop_back() { erase(--end()); };
void push_back(const T& d) { insert(end(), d); }
void resize(size_t count, const T& value);
void resize(size_t count);
void swap(List& other) { std::swap(head, other.head); std::swap(numberOfElements, other.numberOfElements); }
// Operations --------------------
void reverse();
// Non standard inefficient functions --------------------------
T& operator[](const size_t index) const { return begin()[index]; }
// ------------------------------------------------------------------------
// Define iterator capability ---------------------------------------------
struct iterator {
// Definitions ----------------
using iterator_category = std::bidirectional_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = T;
using pointer = T*;
using reference = T&;
// Data -----------------------
Node* iter{};
Node* head{};
// Constructor ----------------
iterator(Node* const node, Node* const h) : iter(node), head(h) {};
iterator() {};
// Dereferencing --------------
reference operator*() const { return iter->data; }
reference operator->() const { return &**this; }
// Arithmetic operations ------
iterator operator++() { iter = iter->next; return *this; }
iterator operator--() { iter = iter->previous; return *this; }
iterator operator++(int) { iterator tmp = *this; ++* this; return tmp; }
iterator operator--(int) { iterator tmp = *this; --* this; return tmp; }
iterator operator +(const difference_type& n) const {
iterator temp{ *this }; difference_type k{ n }; if (k > 0) while (k--)++temp; else while (k++)--temp; return temp;
}
iterator operator +=(const difference_type& n) {
difference_type k{ n }; if (k > 0) while (k--)++* this; else while (k++)--* this; return *this;
};
iterator operator -(const difference_type& n) const {
iterator temp{ *this }; difference_type k{ n }; if (k > 0) while (k--)--temp; else while (k++)++temp; return temp;
}
iterator operator -=(const difference_type& n) {
difference_type k{ n }; if (k > 0) while (k--)--* this; else while (k++)++* this; return *this;
};
// Comparison ----------------- (typical space ship implementation)
bool operator ==(const iterator& other) const { return iter == other.iter; };
bool operator !=(const iterator& other) const { return iter != other.iter; };
bool operator < (const iterator& other) const { return other.iter - iter < 0; };
bool operator <= (const iterator& other) const { return other.iter - iter <= 0; };
bool operator > (const iterator& other) const { return other.iter - iter > 0; };
bool operator >= (const iterator& other) const { return other.iter - iter >= 0; };
// Special non standard functions -----------------
difference_type operator-(const iterator& other) const;
reference operator[] (const size_t index);
};
};
// ------------------------------------------------------------------------------------------------
// Implementation of list functions. This would normally go into a TCC file -----------------------
// List class functions ---------------
template <typename T>
void List<T>::clear() {
for (Node* nextNode{}, * currentNode(head->next); currentNode != head; currentNode = nextNode) {
nextNode = currentNode->next;
delete currentNode;
}
delete head;
init();
}
template <typename T>
typename List<T>::iterator List<T>::insert(const List<T>::iterator& insertBeforePosition, const T& value)
{
Node* nodeInsertBeforePosition = insertBeforePosition.iter;
Node* newNode = new Node(nodeInsertBeforePosition, nodeInsertBeforePosition->previous, value);
nodeInsertBeforePosition->previous = newNode;
(newNode->previous)->next = newNode;
++numberOfElements;
return iterator(newNode, head);
}
template <typename T>
typename List<T>::iterator List<T>::insert(const List<T>::iterator& insertBeforePosition)
{
Node* nodeInsertBeforePosition = insertBeforePosition.iter;
Node* newNode = new Node(nodeInsertBeforePosition, nodeInsertBeforePosition->previous);
nodeInsertBeforePosition->previous = newNode;
(newNode->previous)->next = newNode;
++numberOfElements;
return iterator(newNode, head);
}
template <typename T>
template <class Iter, std::enable_if_t<is_iterator<Iter>::value, bool>>
typename List<T>::iterator List<T>::insert(const List<T>::iterator& insertBeforePosition, const Iter& first, const Iter& last) {
iterator result(insertBeforePosition.iter, head);
if (first != last) {
result = insert(insertBeforePosition, *first);
Iter i(first);
for (++i; i != last; ++i)
insert(insertBeforePosition, *i);
}
return result;
}
template <typename T>
typename List<T>::iterator List<T>::insert(const List<T>::iterator& insertBeforePosition, const size_t& count, const T& value) {
iterator result(insertBeforePosition.iter, head);
if (count != 0u) {
result = insert(insertBeforePosition, value);
for (size_t i{ 1u }; i < count; ++i)
insert(insertBeforePosition, value);
}
return result;
}
template <typename T>
typename List<T>::iterator List<T>::insert(const List<T>::iterator& insertBeforePosition, const std::initializer_list<T>& il) {
return insert(insertBeforePosition, il.begin(), il.end());
}
template <typename T>
typename List<T>::iterator List<T>::erase(const List<T>::iterator& posToDelete) {
iterator result = posToDelete;
++result;
Node* nodeToDelete = posToDelete.iter;
if (nodeToDelete != head) {
nodeToDelete->previous->next = nodeToDelete->next;
nodeToDelete->next->previous = nodeToDelete->previous;
delete nodeToDelete;
--numberOfElements;
}
return result;
}
template <typename T>
typename List<T>::iterator List<T>::erase(const List<T>::iterator& first, const List<T>::iterator& last) {
iterator result{ end() };
if (first == begin() && last == end())
clear();
else {
while (first != last)
first = erase(first);
result = last;
}
return result;
}
template <typename T>
void List<T>::resize(size_t count) {
if (numberOfElements < count)
for (size_t i{ numberOfElements }; i < count; ++i)
insert(end());
else
while (count--)
pop_back();
}
template <typename T>
void List<T>::resize(size_t count, const T& value) {
if (numberOfElements < count)
for (size_t i{ numberOfElements }; i < count; ++i)
insert(end(), value);
else
while (count--)
pop_back();
}
template <typename T>
void List<T>::reverse() {
const Node* oldHead = head;
for (Node* nptr = head; ; nptr = nptr->previous) {
std::swap(nptr->next, nptr->previous);
if (nptr->previous == oldHead) // Previous was the original next
break;
}
}
// ------------------------------------
// Iterator functions -----------------
template <typename T>
typename List<T>::iterator::difference_type List<T>::iterator::operator-(const iterator& other) const {
difference_type result{};
Node* nptr = head;
int indexThis{ -1 }, indexOther{ -1 }, index{};
do {
nptr = nptr->next;
if (nptr == iter)
indexThis = index;
if (nptr == other.iter)
indexOther = index;
++index;
} while (nptr != head);
if (indexThis >= 0 and indexOther >= 0)
result = indexThis - indexOther;
return result;
}
template <typename T>
typename List<T>::iterator::reference List<T>::iterator::operator[] (const size_t index) {
Node* nptr = head->next;
for (size_t i{}; i < index and nptr != head; ++i, nptr = nptr->next)
;
return nptr->data;
}
int main() {
List<int> list{ 1,2,3 };
for (const int i : list)
std::cout << i << '\n';
List<int>::iterator iter = list.begin();
std::cout << '\n' << iter[1] << '\n';
}

Segmentation fault (core dumped) eror in c++ (iterator overloading)

I'm tring to implement the doubly linked list and also iterators and i only want to add a single node and test my print function which have iterators in it.but somehow ( i don't know really ) i'm facing the Segmentation fault (core dumped) eror and it's my first time facing this eror and i read about it and i understand the eror but i can't find where i do the mistake!
doublylinkedlist.h
#ifndef DoublyLinkedList_H
#define DoublyLinkedList_H
#include <iostream>
#ifndef DoublyLinkedList_H
#define DoublyLinkedList_H
#include <iostream>
template <class T>
class DoublyLinkedList;
// List Node class
template <class T>
class DoublyLinkedListNode
{
friend class DoublyLinkedList<T>;
friend class DoublyLinkedList<T>::Iterator_Base;
friend class DoublyLinkedList<T>::Iterator;
friend class DoublyLinkedList<T>::Reverse_Iterator;
private:
T data; // data
DoublyLinkedListNode<T> *llink; // left link
DoublyLinkedListNode<T> *rlink; // right link
};
// List class
template <class T>
class DoublyLinkedList
{
public:
typedef DoublyLinkedListNode<T> NodeType;
// iterator
class Iterator_Base
{
public:
Iterator_Base(NodeType *startNode = 0) { current = startNode; }
~Iterator_Base() {}
Iterator_Base(const Iterator_Base &source) { current = source.current; }
// dereferencing operators
T &operator*() const { return current->data; }
T *operator->() const { return &current->data; }
// get the node pointer directly
NodeType *GetNodePointer() { return current; }
// equality testing
bool operator!=(const Iterator_Base it) const
{
return current != it.current;
}
bool operator==(const Iterator_Base it) const
{
return current == it.current;
}
protected:
NodeType *current;
};
class Iterator : public Iterator_Base
{
public:
Iterator(NodeType *startNode = 0) : Iterator_Base(startNode) {}
Iterator &operator++(); // ToDo
Iterator operator++(int); // ToDo
Iterator &operator--(); // ToDo
Iterator operator--(int); // ToDo
};
class Reverse_Iterator : public Iterator_Base
{
public:
Reverse_Iterator(NodeType *startNode = 0) : Iterator_Base(startNode) {}
Reverse_Iterator &operator++(); // ToDo
Reverse_Iterator operator++(int); // ToDo
Reverse_Iterator &operator--(); // ToDo
Reverse_Iterator operator--(int); // ToDo
};
// Default constructor - ToDo
DoublyLinkedList();
// Copy constructor - ToDo
DoublyLinkedList(const DoublyLinkedList &source);
// Destructor - ToDo
~DoublyLinkedList();
// Assignment operator - ToDo
DoublyLinkedList &operator=(const DoublyLinkedList &source);
// Get leftmost node iterator
Iterator Begin() const
{
Iterator it(first);
return ++it;
};
// Get end iterator
Iterator End() const
{
Iterator it(first);
return it;
}
// Get rightmost iterator
Reverse_Iterator rBegin() const
{
Reverse_Iterator it(first);
return ++it;
};
// Get end iterator
Reverse_Iterator rEnd() const
{
Reverse_Iterator it(first);
return it;
}
// Insert a new node to the list as the first node
// Return the pointer of new node - ToDo
Iterator InsertFirst(const T &data);
// Insert a new node to the list as the last node
// Return the pointer of new node - ToDo
Iterator InsertLast(const T &data);
// Insert a new node to the list after pos
// Return the pointer of new node - ToDo
Iterator InsertNext(Iterator pos, const T &data);
// Insert a new node to the list before pos
// Return the pointer of new node - ToDo
Iterator InsertPrev(Iterator pos, const T &data);
// Total number of nodes in the list - ToDo
int Size() const;
// Delete node at pos in the list - ToDo
bool DeleteNode(Iterator pos);
// Delete first node - ToDo
bool DeleteFirst();
// Delete last node - ToDo
bool DeleteLast();
// Print list
void Print(bool reverse = false);
private:
// first pointer of the list
NodeType *first;
};
#ifndef DoublyLinkedList_TXX
#include "doublylinkedlist.txx"
#endif
#endif
doublylinkedlist.txx
#include "doublylinkedlist.h"
#include <iostream>
#include <math.h>
template <class T>
typename DoublyLinkedList<T>::Iterator &
DoublyLinkedList<T>::Iterator::operator++()
{
this->current = this->current->rlink;
return *this;
}
template <class T>
typename DoublyLinkedList<T>::Iterator
DoublyLinkedList<T>::Iterator::operator++(int)
{
DoublyLinkedList<T>::Iterator temp = *this;
++(*this);
return temp;
}
template <class T>
typename DoublyLinkedList<T>::Iterator &
DoublyLinkedList<T>::Iterator::operator--()
{
this->current = this->current->llink;
return *this;
}
template <class T>
typename DoublyLinkedList<T>::Iterator
DoublyLinkedList<T>::Iterator::operator--(int)
{
DoublyLinkedList<T>::Iterator temp = *this;
--(*this);
return temp;
}
/*
* Reverse Iterator
*/
template <class T>
typename DoublyLinkedList<T>::Reverse_Iterator &
DoublyLinkedList<T>::Reverse_Iterator::operator++()
{
this->current = this->current->llink;
return *this;
}
template <class T>
typename DoublyLinkedList<T>::Reverse_Iterator
DoublyLinkedList<T>::Reverse_Iterator::operator++(int)
{
DoublyLinkedList<T>::Reverse_Iterator temp = *this;
++(*this);
return temp;
}
template <class T>
typename DoublyLinkedList<T>::Reverse_Iterator &
DoublyLinkedList<T>::Reverse_Iterator::operator--()
{
this->current = this->current->rlink;
return *this;
}
template <class T>
typename DoublyLinkedList<T>::Reverse_Iterator
DoublyLinkedList<T>::Reverse_Iterator::operator--(int)
{
DoublyLinkedList<T>::Reverse_Iterator temp = *this;
--(*this);
return temp;
}
/*
* Doubly linked list
*/
// Default constructor
template <class T>
DoublyLinkedList<T>::DoublyLinkedList()
{
this->first = NULL;
}
// Copy constructor
template <class T>
DoublyLinkedList<T>::DoublyLinkedList(const DoublyLinkedList &source)
{
// ToDo
}
// Destructor
template <class T>
DoublyLinkedList<T>::~DoublyLinkedList()
{
// ToDo
}
/*
// Assignment operator
template <class T>
DoublyLinkedList<T> &
DoublyLinkedList<T>::operator=(const DoublyLinkedList<T> &source)
{
// ToDo
}
// Total number of nodes in the list
template <class T>
int DoublyLinkedList<T>::Size() const
{
// ToDo
}
*/
// Insert a new node to the list as the first node
// Return the pointer of new node
template <class T>
typename DoublyLinkedList<T>::Iterator
// typename Iterator
DoublyLinkedList<T>::InsertFirst(const T &data)
{
if (first != NULL)
{
printf("worked");
}
else
{
DoublyLinkedListNode<T> *newNode = new DoublyLinkedListNode<T>();
newNode->data = data;
newNode->rlink = NULL;
newNode->llink = NULL;
this->first = newNode;
}
}
/*
// Insert a new node to the list as the last node
// Return the pointer of new node
template <class T>
typename DoublyLinkedList<T>::Iterator
DoublyLinkedList<T>::InsertLast(const T &data)
{
// ToDo
}
/*
// Insert a new node to the list after pos
// Return the pointer of new node
template <class T>
typename DoublyLinkedList<T>::Iterator
DoublyLinkedList<T>::InsertNext(DoublyLinkedList<T>::Iterator pos, const T &data)
{
// ToDo
}
// Insert a new node to the list before pos
// Return the pointer of new node
template <class T>
typename DoublyLinkedList<T>::Iterator
DoublyLinkedList<T>::InsertPrev(Iterator, const T &data)
{
// ToDo
}
// Delete node at pos in the list
template <class T>
bool DoublyLinkedList<T>::DeleteNode(Iterator pos)
{
// ToDo
}
// Delete first node
template <class T>
bool DoublyLinkedList<T>::DeleteFirst()
{
// ToDo
}
// Delete last node
template <class T>
bool DoublyLinkedList<T>::DeleteLast()
{
// ToDo
}
*/
template <class T>
void DoublyLinkedList<T>::Print(bool reverse)
{
if (reverse)
{
for (Reverse_Iterator rit = rBegin(); rit != rEnd(); rit++)
{
std::cout << *rit << ", ";
}
}
else
{
for (Iterator it = Begin(); it != End(); it++)
{
std::cout << *it << ", ";
}
}
std::cout << std::endl;
}
i'm only using this in my main.cpp to test InsertFirst() and Print()
DoublyLinkedList<int> list;
list.InsertFirst(10);
list.Print();
PS
the goal of this code is to test the iterators for my print fucntion so thats why i comment other fucntions.

Can we reverse a doubly linked list by traversing it till the end and setting last pointer to head?

Instead of actually reversing the link between nodes, I have traversed the list till the very end and set the last node address as head. I prepared a separate function for printing it in specific order which works well except that its returning garbage value for the last node.
Here's the code -
void reverselist(Node * &head){
Node *temp =head;
Node *prevtemp = NULL;
while(temp !=NULL){
prevtemp = temp;
temp = temp->next;
}
head = prevtemp;
}
void printreverse(Node * &head){
Node *temp = head;
while(temp!=NULL){
cout << temp->data << " ";
temp = temp->prev;
}
}
int main() {
Node * node1= new Node(54);
Node *head = node1;
Node *tail =node1;
insertathead(head, tail, 10);
printlist(head);
insertattail(tail, 23);
printlist(head);
insertatanyposition(head, tail,43,2);
printlist(head);
reverselist(head);
printreverse(head);
return 0;
}
The answer is no. This does not work.
I also do not see a double linked list. Looks more like you wanted to implement a singly linked list.
There is no linked list at all somewhere in the code. Maybe you mix up "Node" and "Linked List". The "Node" is usually a part of the linked list. So it is contained in the Linked List class. And the whole functionality of the "Node" is usually not exposed to the outside world.
Then, you would add iterator functionality to the linked list class.
And last but not least, there is a wrapper class for reverse iterators. And with that, you can easily implement what you want. Please read here
But for that you need to create a --operator for your iterator. This is difficult for singly linked list and simple for double linked list.
C++ has already what you need. You can simply use it.
But, if we want to practice a little bit, then check some list implementations in the net and try to implement step by step. You can find a somehow OK starting point in the code example below.
Please note: Reverse can be done in several ways. Either you reverse the list by swapping values. That may be inefficient with complex data (but OK for simple data). std::reverese from the algorithm library is available for that.
Or, the pointer in the nodes can be swapped. With that data would not be touched at all. For this purpose, the list in the standard library has a special reversefunction. I also added such a function in the below example code.
And third, you can create a revers_iterator with the help of some build in C++ functions. Please look in the example code at the bottom on how to do that.
Anyway: Please read a book about data structures. This thing which consists of real paper :-). Then you will understand, that there is no naive implementation with a head and a tail pointer. For that a sentinel, a special noe is used. This makes life extremely simple for inserting and erasing values. No comparison with head and teail is needed.
Please find an example with Double Linked list and reverse iterator in the function main at the bottom.
#include <iostream>
#include <iterator>
#include <vector>
#include <type_traits>
#include <initializer_list>
#include <algorithm>
// ------------------------------------------------------------------------------------------------
// This would be in a header file -----------------------------------------------------------------
// Type trait helper to identify iterators --------------------------------------------------------
template<typename T, typename = void>
struct is_iterator { static constexpr bool value = false; };
template<typename T>
struct is_iterator<T, typename std::enable_if<!std::is_same<typename std::iterator_traits<T>::value_type, void>::value>::type> {
static constexpr bool value = true;
};
// The List class ---------------------------------------------------------------------------------
template <typename T>
class List {
// Sub class for a Node -----------
struct Node {
Node* next{};
Node* previous{};
T data{};
Node(Node* const n, Node* const p, const T& d) : next(n), previous(p), data(d) {}
Node(Node* const n, Node* const p) : next(n), previous(p) {}
Node() {}
};
// Private list data and functions --------
size_t numberOfElements{};
Node* head{};
void init() { head = new Node(); head->next = head; head->previous = head; numberOfElements = 0; }
public:
struct iterator; // Forward declaration
// Constructor --------------------
List() { init(); }
explicit List(const size_t count, const T& value) { init(); insert(begin(), count, value); };
explicit List(const size_t count) { init(); insert(begin(), count); }
template <typename Iter>
List(const Iter& first, const Iter& last) { init(); insert(begin(),first, last); }
List(const List& other) { init(), insert(begin(), other.begin(), other.end()); };
List(List&& other) : head(other.head), numberOfElements(other.numberOfElements) { other.init(); }
List(const std::initializer_list<T>& il) { init(); insert(begin(), il.begin(), il.end()); }
// Assignment ---------------------
List& operator =(const List& other) { clear(); insert(begin(), other.begin(), other.end()); return *this; }
List& operator =(List&& other) { clear(); head = other.head; numberOfElements = other.numberOfElements; other.init(); return *this; }
List& operator =(const std::initializer_list<T>& il) { clear(); insert(begin(),il.begin(),il.end()); return *this; }
void assign(const size_t count, const T& value) { clear(); insert(begin(), count, value); }
template <typename Iter>
void assign(const Iter& first, const Iter& last) { clear(); insert(begin(), first, last);}
void assign(const std::initializer_list<T>& il) { clear(); insert(begin(), il.begin(), il.end()); }
// Destructor ---------------------
~List() { clear(); }
// Element Access -----------------
T& front() { return *begin(); }
T& back() { return *(--end()); }
// Iterators ----------------------
iterator begin() const { return iterator(head->next, head); }
iterator end() const { return iterator(head, head); }
// Capacity -----------------------
size_t size() const { return numberOfElements; }
bool empty() { return size() == 0; }
// Modifiers ----------------------
void clear();
iterator insert(const iterator& insertBeforePosition, const T& value);
iterator insert(const iterator& insertBeforePosition);
template <class Iter, std::enable_if_t<is_iterator<Iter>::value, bool> = true>
iterator insert(const iterator& insertBeforePosition, const Iter& first, const Iter& last);
iterator insert(const iterator& insertBeforePosition, const size_t& count, const T& value);
iterator insert(const iterator& insertBeforePosition, const std::initializer_list<T>& il);
iterator erase(const iterator& posToDelete);
iterator erase(const iterator& first, const iterator& last);
void push_back(const T& d) { insert(end(), d); }
void pop_back() { erase(--end()); };
void push_front(const T& d) { insert(begin(), d); }
void pop_front() { erase(begin()); };
void resize(size_t count);
void resize(size_t count, const T& value);
void swap(List& other) { std::swap(head, other.head); std::swap(numberOfElements, other.numberOfElements); }
// Operations --------------------
void reverse();
// Non standard inefficient functions --------------------------
T& operator[](const size_t index) const { return begin()[index]; }
// ------------------------------------------------------------------------
// Define iterator capability ---------------------------------------------
struct iterator {
// Definitions ----------------
using iterator_category = std::bidirectional_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = T;
using pointer = T*;
using reference = T&;
// Data -----------------------
Node* iter{};
Node* head{};
// Constructor ----------------
iterator(Node*const node, Node* const h) : iter(node), head(h) {};
iterator() {};
// Dereferencing --------------
reference operator*() const { return iter->data; }
reference operator->() const { return &**this; }
// Arithmetic operations ------
iterator operator++() { iter = iter->next; return *this; }
iterator operator++(int) { iterator tmp = *this; ++* this; return tmp; }
iterator operator--() { iter = iter->previous; return *this; }
iterator operator--(int) { iterator tmp = *this; --* this; return tmp; }
iterator operator +(const difference_type& n) const {
iterator temp{ *this }; difference_type k{ n }; if (k > 0) while (k--)++temp; else while (k++)--temp; return temp;
}
iterator operator +=(const difference_type& n) {
difference_type k{ n }; if (k > 0) while (k--)++* this; else while (k++)--* this; return *this;
};
iterator operator -(const difference_type& n) const {
iterator temp{ *this }; difference_type k{ n }; if (k > 0) while (k--)--temp; else while (k++)++temp; return temp;
}
iterator operator -=(const difference_type& n) {
difference_type k{ n }; if (k > 0) while (k--)--* this; else while (k++)++* this; return *this;
};
// Comparison -----------------
bool operator ==(const iterator& other) const { return iter == other.iter; };
bool operator !=(const iterator& other) const { return iter != other.iter; };
bool operator < (const iterator& other) const { return other.iter - iter < 0; };
bool operator <= (const iterator& other) const { return other.iter - iter <= 0; };
bool operator > (const iterator& other) const { return other.iter - iter > 0; };
bool operator >= (const iterator& other) const { return other.iter - iter >= 0; };
// Special non standard functions -----------------
difference_type operator-(const iterator& other) const;
reference operator[] (const size_t index);
};
};
// ------------------------------------------------------------------------------------------------
// Implementation of list functions. This would normally go into a TCC file -----------------------
// List class functions ---------------
template <typename T>
void List<T>::clear() {
for (Node* nextNode{}, * currentNode(head->next); currentNode != head; currentNode = nextNode) {
nextNode = currentNode->next;
delete currentNode;
}
init();
}
template <typename T>
typename List<T>::iterator List<T>::insert(const List<T>::iterator& insertBeforePosition, const T& value)
{
Node* nodeInsertBeforePosition = insertBeforePosition.iter;
Node* newNode = new Node(nodeInsertBeforePosition, nodeInsertBeforePosition->previous, value);
nodeInsertBeforePosition->previous = newNode;
(newNode->previous)->next = newNode;
++numberOfElements;
return iterator(newNode, head);
}
template <typename T>
typename List<T>::iterator List<T>::insert(const List<T>::iterator& insertBeforePosition)
{
Node* nodeInsertBeforePosition = insertBeforePosition.iter;
Node* newNode = new Node(nodeInsertBeforePosition, nodeInsertBeforePosition->previous);
nodeInsertBeforePosition->previous = newNode;
(newNode->previous)->next = newNode;
++numberOfElements;
return iterator(newNode, head);
}
template <typename T>
template <class Iter, std::enable_if_t<is_iterator<Iter>::value, bool>>
typename List<T>::iterator List<T>::insert(const List<T>::iterator& insertBeforePosition, const Iter& first, const Iter& last) {
iterator result(insertBeforePosition.iter, head);
if (first != last) {
result = insert(insertBeforePosition, *first);
Iter i(first);
for (++i; i != last; ++i)
insert(insertBeforePosition, *i);
}
return result;
}
template <typename T>
typename List<T>::iterator List<T>::insert(const List<T>::iterator& insertBeforePosition, const size_t& count, const T& value) {
iterator result(insertBeforePosition.iter, head);
if (count != 0u) {
result = insert(insertBeforePosition, value);
for (size_t i{ 1u }; i < count; ++i)
insert(insertBeforePosition, value);
}
return result;
}
template <typename T>
typename List<T>::iterator List<T>::insert(const List<T>::iterator& insertBeforePosition, const std::initializer_list<T>& il) {
return insert(insertBeforePosition, il.begin(), il.end());
}
template <typename T>
typename List<T>::iterator List<T>::erase(const List<T>::iterator& posToDelete) {
iterator result = posToDelete;
++result;
Node* nodeToDelete = posToDelete.iter;
if (nodeToDelete != head) {
nodeToDelete->previous->next = nodeToDelete->next;
nodeToDelete->next->previous = nodeToDelete->previous;
delete nodeToDelete;
--numberOfElements;
}
return result;
}
template <typename T>
typename List<T>::iterator List<T>::erase(const List<T>::iterator& first, const List<T>::iterator& last) {
iterator result{ end() };
if (first == begin() && last == end())
clear();
else {
while (first != last)
first = erase(first);
result = last;
}
return result;
}
template <typename T>
void List<T>::resize(size_t count) {
if (numberOfElements < count)
for (size_t i{ numberOfElements }; i < count; ++i)
insert(end());
else
while (count--)
pop_back();
}
template <typename T>
void List<T>::resize(size_t count, const T& value) {
if (numberOfElements < count)
for (size_t i{ numberOfElements }; i < count; ++i)
insert(end(),value);
else
while (count--)
pop_back();
}
template <typename T>
void List<T>::reverse() {
const Node* oldHead = head;
for (Node* nptr = head; ; nptr = nptr->previous) {
std::swap(nptr->next, nptr->previous);
if (nptr->previous == oldHead) // Previous was the original next
break;
}
}
// ------------------------------------
// Iterator functions -----------------
template <typename T>
typename List<T>::iterator::difference_type List<T>::iterator::operator-(const iterator& other) const {
difference_type result{};
Node* nptr = head;
int indexThis{ -1 }, indexOther{ -1 }, index{};
do {
nptr = nptr->next;
if (nptr == iter)
indexThis = index;
if (nptr == other.iter)
indexOther = index;
++index;
} while (nptr != head);
if (indexThis >= 0 and indexOther >= 0)
result = indexThis - indexOther;
return result;
}
template <typename T>
typename List<T>::iterator::reference List<T>::iterator::operator[] (const size_t index) {
Node* nptr = head->next;
for (size_t i{}; i < index and nptr != head; ++i, nptr = nptr->next)
;
return nptr->data;
}
// ------------------------------------------------------------------------------------------------
// This would be in a cpp file --------------------------------------------------------------------
int main() {
List<int> list{ 1,2,3,4,5 };
std::cout << "Original List\n";
for (int i : list) std::cout << i << ' '; std::cout << '\n';
std::cout << "\nInternal reverse function. Kust swap ointers in Node\n";
list.reverse();
for (int i : list) std::cout << i << ' '; std::cout << '\n';
std::cout << "\nReverse function from algorithm library. Reverse values with copy\n";
std::reverse(list.begin(), list.end());
for (int i : list) std::cout << i << ' '; std::cout << '\n';
// Use reverse iterators
std::cout << "\nBuild and use revers iterators\n";
std::reverse_iterator<List<int>::iterator> riter = std::make_reverse_iterator(list.end());
std::reverse_iterator<List<int>::iterator> riterEnd = std::make_reverse_iterator(list.begin());
for (; riter != riterEnd; ++riter)
std::cout << *riter << ' '; std::cout << '\n';
return 0;
}
Please note. The above is a very simplified implementation. Not very efficient and not using allocaters.
Attention. This code is only partially tested. It may not even compile be instantiating templates . . .

Template LinkedList class with nested class

I'm trying to implement a linked list with a template class LList and Nested Iterator and Node classes. Here's the code:
template <typename T1>
class LList
{
public:
class Iterator
{
public:
Iterator();
T1 get() const;
void next();
void previous();
bool equals(Iterator iter) const;
private:
Node* position;
LList* container;
};
LList();
~LList();
void pushBack(T1 data);
Iterator begin();
Iterator end();
void insert (Iterator iter, T1 data);
Iterator erase(Iterator Iter);
private:
class Node
{
public:
Node(T1 data);
private:
T1 data;
Node* ptr_next;
Node* ptr_prev;
};
Node* ptr_first;
Node* ptr_last;
};
template <typename T1>
LList<T1>::Node::Node(T1 data)
{
this->data = data;
ptr_next = 0;
ptr_prev =0;
}
template <typename T1>
LList<T1>::Iterator::Iterator()
{
position = 0;
container = 0;
}
template <typename T1>
T1 LList<T1>::Iterator::get() const
{
return position->data;
}
template <typename T1>
void LList<T1>::Iterator::next()
{
if(position == container->ptr_last)
{
position = container->ptr_first;
}
else
{
position = position->ptr_next;
}
}
template <typename T1>
void LList<T1>::Iterator::previous()
{
if(!position)
{
position = container->ptr_last;
}
else
{
position = position->ptr_prev;
}
}
template <typename T1>
bool LList<T1>::Iterator::equals(Iterator iter) const
{
return position == iter.position;
}
template <typename T1>
LList<T1>::LList()
{
ptr_first = 0;
ptr_last = 0;
}
template <typename T1>
LList<T1>::~LList()
{
while (ptr_first)
{
Node* tmp = ptr_first;
ptr_first = ptr_first->ptr_next;
delete tmp;
tmp = 0;
}
}
template <typename T1>
void LList<T1>::pushBack(T1 data)
{
Node* new_node = new Node(data);
if(ptr_first==0)
{
ptr_first = new_node;
ptr_last = new_node;
}
else
{
ptr_last->ptr_next = new_node;
new_node->ptr_prev = ptr_last;
ptr_last = new_node;
}
}
template <typename T1>
Iterator LList<T1>::begin()
{
Iterator iter;
iter.positon = ptr_first;
iter.container = this;
return iter;
}
template <typename T1>
Iterator LList<T2>::end()
{
Iterator iter;
iter.position = ptr_last;
iter.container = this;
return iter;
}
template <typename T1>
void LList<T1>::insert(Iterator iter, T1 data)
{
if (iter.position == 0)
{
pushBack(data);
return;
}
Node* before;
Node* after;
after = iter.position;
before = iter.position->ptr_prev;
Node* new_node = new Node(data);
after->ptr_prev = new_node;
if (before == 0) ptr_first = new_node;
else before->ptr_next = new_node;
new_node->ptr_prev = before;
new_node->ptr_next = after;
}
template <typename T1>
Iterator LList<T1>::erase(Iterator iter)
{
Node* after = iter.position->ptr_next;
Node* before = iter.position->ptr_prev;
Node* remove = iter.position;
if (remove == ptr_first) ptr_first = after;
else before->ptr_next = after;
if (remove == ptr_last) ptr_last = before;
else after->ptr_prev = before;
delete remove;
remove = 0;
}
I've seen how do it without nested classes but I need to do it with a nested class.
Any help on why it doesn't compile will help :) Thanks.
Well, Iterator is the name of a nested class, so when you use it in the definition of a member function of your LList class template as the return type, you have to fully qualify it (and add the typename disambiguator to tell the compiler that what follows the :: shall be parsed as the name of a type).
For instance:
template <typename T1>
typename LList<T1>::Iterator LList<T1>::erase(Iterator iter)
// ^^^^^^^^^^^^^^^^^^^^
There are several instances of this error, so you will have to fix all of them.
You are also referring to class Node before its definition appears inside LList. Therefore, you should have a forward declaration for it:
template <typename T1>
class LList
{
class Node;
// ^^^^^^^^^^^
// Forward declaration for Node
public:
// ...
class Iterator
{
// ...
Node* position; // <== Now this is OK because of the forward declaration
// ...
};
There are different error :
The first is that you use Node into the Iterator class, but you need to declare Node before it :
template <typename T1>
class LList
{
private:
class Node
{
public:
Node(T1 data);
private:
T1 data;
Node* ptr_next;
Node* ptr_prev;
};
Node* ptr_first;
Node* ptr_last;
public:
LList();
~LList();
void pushBack(T1 data);
class Iterator
{
public:
Iterator();
T1 get() const;
void next();
void previous();
bool equals(Iterator iter) const;
private:
Node* position;
LList* container;
};
Iterator begin();
Iterator end();
void insert (Iterator iter, T1 data);
Iterator erase(Iterator Iter);
};
That is the first thing.
The second is that Iterator is a nested type, so you need to be more specific when you return un object of this type :
template <typename T1>
typename LList<T1>::Iterator LList<T1>::begin()
And the last error is :
template <typename T1>
Iterator LList<T2>::end()
Here is the correct lign :
template <typename T1>
typename LList<T1>::Iterator LList<T1>::end()
// ^^^^^^^^^^^^^^^^^ ^^

c++ sort function of List

My sort function looks like this:
template <typename Compare, typename T>
void List<T>::sort(const Compare& comparer){
...
}
But I received next error:
template definition of non-template
void List<T>::sort(const Compare&)'
invalid use of undefined typeclass
List'
What does it mean?
This is the complete code of the list:
template <typename T> class Node;
template <typename T> class Iterator;
template <typename T>
class List{
private:
Node<T> * first;
Node<T> * last;
int size;
friend class Predicate ;
friend class Compare;
public:
typedef Iterator<T> iterator;
List(){
first = NULL;
last = NULL;
size = 0;
}
List(const List<T> & l);
void pushBack(T element);
void insert( const T& element, iterator i);
iterator remove(iterator i);
iterator find(const Predicate& predicate);
void sort(const Compare& comparer);
int getSize() const;
iterator begin();
iterator end();
~List();
};
template <class T>
List<T>::List(const List & l) {
first = 0;
last = 0;
size = 0;
for (Node<T> * current = l.first; current != 0; current = current -> next){
pushBack(current -> data);
}
}
template <typename T>
void List<T>::pushBack(T element){
Node<T>* newnode = new Node<T>(element);
if (newnode->prev == NULL) {
first = newnode;
last = newnode;
}else{
newnode->prev = last;
last->next = newnode;
last = newnode;
}
}
template <typename T>
void List<T>::insert( const T& element, iterator i){
if (i.position == NULL){
pushBack(element);
++size;
return;
}
Node<T>* after = i.position;
Node<T>* before = after->prev;
Node<T>* newnode = new Node<T>(element);
newnode->prev = before;
newnode->next = after;
after->prev = newnode;
if (before == NULL) {
first = newnode;
}
else{
before->next = newnode;
}
++size;
}
template <typename T>
typename List<T>::iterator List<T>::remove(iterator iter){
if(iter.position != NULL){
Node<T>* remove = iter.position;
Node<T>* before = remove->prev;
Node<T>* after = remove->next;
if (remove == first){
first = after;
} else{
before->next = after;
}
if (remove == last){
last = before;
}else{
after->prev = before;
}
iter.position = after;
--size;
delete remove;
return iter;
}else{
throw ElementNotFound();
}
}
template <typename T>
typename List<T>::iterator List<T>::begin(){
//iterator iter;
//iter.position = first;
//iter.last = last;
return iterator(first);
}
template <typename T>
typename List<T>::iterator List<T>::end(){
return iterator (last);
}
template <typename Predicate, typename T>
List<T>::iterator List<T>::find(const Predicate& predicate){
iterator iter;
for( iter = begin(); iter != end(); iter = next()){
if( predicate(iter.getElement())){
return iter;
}
}
return end();
}
template <typename Compare, typename T>
void List<T>::sort(const Compare& comparer){
Iterator<T> iter;
for( iter = begin(); iter != end(); iter = iter.next() ){
if( comparer( iter.getElement() , (iter+1).getElement()) != true){
}
}
}
template <typename T>
int List<T>::getSize() const{
return size;
}
template <class T>
List <T>::~List() {
Node <T> * firstNode = first;
while (firstNode != 0) {
Node <T> * nextNode = firstNode->next;
delete firstNode;
firstNode = nextNode;
}
}
template <typename T> class Node {
private:
T data;
Node* next;
Node* prev;
friend class List<T>;
friend class Iterator<T>;
public:
Node(T element){
data = element;
prev = NULL;
next = NULL;
}
~Node(){}
};
template <typename T> class Iterator{
private:
Node<T>* position;
Node<T>* last;
friend class List<T>;
public:
Iterator(){
position = NULL;
last = NULL;
}
Iterator(Node<T> * source): position(source) { }
T& getElement()const;
bool operator==(Iterator b) const;
bool operator!=(Iterator b) const;
T & operator*();
Iterator & operator++();
Iterator & operator++(int);
~Iterator(){}
};
template <class T>
T& Iterator<T>::getElement() const{
if(position != NULL){
return position->data;
}
else{
throw ElementNotFound();
}
}
template <typename T>
bool Iterator<T>::operator==(Iterator b) const{
return position == b.position;
}
template <typename T>
bool Iterator<T>::operator!=(Iterator b) const{
return position != b.position;
}
template <typename T>
T & Iterator<T>::operator*() {
return position->data;
}
template <class T>
Iterator<T> & Iterator<T>::operator++() {
position = position->next;
return *this;
}
template <class T>
Iterator<T> & Iterator<T>::operator++(int){
position = position->next;
return *this;
}
#endif /* LISTGENERIC_H_ */
Either you declare sort as a template function with one template argument (Compare) inside the class. Then your definition has to look as follows:
template <typename T>
template <typename Compare>
void List<T>::sort(const Compare& comparer) {
…
}
And you also need to remove the now redundant friend class Compare declaration from your List class.
Or you keep Compare and sort as-is. In this case, simply don’t have sort as a template, and omit the Compare template argument:
template <typename T>
void List<T>::sort(const Compare& comparer) {
…
}
Of course this only works if you have defined (not just declared!) the class Compare before this function.
But this second solution would be highly unorthodox and pretty useless since the compare argument doesn’t make much sense: there is only one Compare class and the user cannot change it. Normally, this should be a template argument (first solution).