# pragma once
# include <iostream>
# include <string>
# include <vector>
using namespace std; class Iterator ;
class ForwardIterator ;
class ReverseIterator ;
struct Node
{ Node ( int n) : name ( n) { } int name; Node* next = nullptr ; Node* prev = nullptr ;
} ;
class MyList
{
public : inline int getCount ( ) { return m_count; } inline Node* head ( ) { return m_head; } inline Node* tail ( ) { return m_tail; } Node* insert ( Node* node, int name) ; Node* pushFront ( int name) ; Node* pushBack ( int name) ; Iterator* getIterator ( bool isReverse) ; private : Node* m_head = nullptr ; Node* m_tail = nullptr ; int m_count = 0 ;
} ;
Node* MyList :: insert ( Node* node, int name)
{ Node* newNode = nullptr ; if ( node == m_head) { newNode = pushFront ( name) ; } else if ( node == m_tail) { newNode = pushBack ( name) ; } else { newNode = new Node ( name) ; newNode-> next = node; newNode-> prev = node-> prev; node-> prev-> next = newNode; node-> prev = newNode; m_count++ ; } return newNode;
} Node* MyList :: pushFront ( int name)
{ Node* newNode = new Node ( name) ; if ( m_count == 0 ) { m_head = m_tail = newNode; } else { newNode-> next = m_head; m_head-> prev = newNode; m_head = newNode; } m_count++ ; return newNode;
} Node* MyList :: pushBack ( int name)
{ Node* newNode = new Node ( name) ; if ( m_count == 0 ) { m_head = m_tail = newNode; } else { newNode-> prev = m_tail; m_tail-> next = newNode; m_tail = newNode; } m_count++ ; return newNode;
}
class Iterator
{
public : Iterator ( MyList* list) : m_list ( list) { } Node* current ( ) { return m_current; } virtual Node* first ( ) = 0 ; virtual Node* next ( ) = 0 ; virtual bool isDone ( ) = 0 ; protected : MyList* m_list = nullptr ; Node* m_current = nullptr ;
} ;
class ForwardIterator : public Iterator
{
public : using Iterator:: Iterator; Node* first ( ) override { m_current = m_list-> head ( ) ; return m_current; } Node* next ( ) override { m_current = m_current-> next; return m_current; } bool isDone ( ) override { return m_current == nullptr ; }
} ;
class ReverseIterator : public Iterator
{
public : using Iterator:: Iterator; Node* first ( ) override { m_current = m_list-> tail ( ) ; return m_current; } Node* next ( ) override { m_current = m_current-> prev; return m_current; } bool isDone ( ) override { return m_current == nullptr ; }
} ; Iterator* MyList :: getIterator ( bool isReverse)
{ Iterator* it = nullptr ; if ( isReverse) { it = new ReverseIterator ( this ) ; } else { it = new ForwardIterator ( this ) ; } return it;
}
int main ( )
{ vector< int > nameList = { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; MyList ls; for ( auto & itr : nameList) { ls. pushBack ( itr) ; } Iterator* it = ls. getIterator ( true ) ; for ( auto begin = it-> first ( ) ; ! it-> isDone ( ) ; it-> next ( ) ) { cout << it-> current ( ) -> name << endl; } cout << "==================================================" << endl; Iterator* ir = ls. getIterator ( false ) ; for ( auto begin = ir-> first ( ) ; ! ir-> isDone ( ) ; ir-> next ( ) ) { cout << ir-> current ( ) -> name << endl; } return 0 ;
}