List.h
#pragma once
#define _CRT_SECURuE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#include<cassert>
template<class T>
struct ListNode
{
ListNode(const T& data)
:_data(data)
, _prev(NULL)
, _next(NULL)
{}
ListNode<T>* _prev;
ListNode<T>* _next;
T _data;
};
template<class T, class Ref, class Ptr>
struct _List_Iterator
{
typedef _List_Iterator<T, Ref, Ptr> self;
typedef ListNode<T> Node;
_List_Iterator(Node* node)
:_node(node)
{}
Ref operator*()
{
return _node->_data;
}
Ptr operator->()
{
return &(_node->_data);
}
self& operator++()
{
_node = _node->_next;
return *this;
}
self operator++(int)
{
Node* tmp = _node;
_node = _node->_next;
return self(tmp);
}
self& operator--()
{
_node = _node->_prev;
return *this;
}
self operator--(int)
{
Node* tmp = _node;
_node = _node->_prev;
return self(tmp);
}
bool operator==(const self& s) const
{
return _node == s._node;
}
bool operator!=(const self& s)const
{
return _node != s._node;
}
Node* _node;
};
template<class T>
class List
{
public:
typedef ListNode<T> Node;
typedef _List_Iterator<T, T&, T*> Iterator;
typedef _List_Iterator<T, const T&,const T*> Const_Iterator;
Node* GetNode(const T& x)
{
return new Node(x);
}
List()
{
_head = new Node(T());
_head->_next = _head;
_head->_prev = _head;
}
Iterator Begin()
{
return Iterator(_head->_next);
}
Const_Iterator Begin()const
{
return Const_Iterator(_head->_next);
}
Iterator End()
{
return Iterator(_head);
}
Const_Iterator End()const
{
return Const_Iterator(_head);
}
void PushBack(const T& x)
{
Insert(_head,x);
}
void PopBack()
{
Erase(Iterator(_head->_prev));
}
void PushFront(const T& x)
{
Insert(_head->_next, x);
}
void PopFront()
{
Erase(Iterator(_head->_next));
}
void Insert(Iterator pos, const T& x)
{
assert(pos._node != NULL);
Node* prev = pos._node->_prev;
Node* cur = GetNode(x);
prev->_next = cur;
cur->_prev = prev;
cur->_next = pos._node;
pos._node->_prev = cur;
}
Iterator Erase(Iterator& pos)
{
assert(pos._node && pos != End());
Node* prev = pos._node->_prev;
Node* next = pos._node->_next;
prev->_next = next;
next->_prev = prev;
delete pos._node;
pos._node = NULL;
return next;
}
Iterator Find(const T& x)
{
Iterator it = Begin();
while (it != End())
{
if (*it == x)
{
return it;
}
it++;
}
return End();
}
void Clear()
{
Iterator it = Begin();
while (it != End())
{
Node* del = it._node;
it++;
delete del;
}
_head->_next = _head;
_head->_prev = _head;
}
~List()
{
Clear();
delete _head;
_head = NULL;
}
protected:
Node* _head;
};
list.cpp
#include"List.h"
void PrintList(List<int>& l)
{
List<int >::Iterator it = l.Begin();
while (it != l.End())
{
cout << *it << " ";
it++;
}
cout << endl;
}
void Print(List<int>& l)
{
List<int>::Iterator it = l.Begin();
while (it != l.End())
{
if (*it % 2 == 0)
{
it = l.Erase(it);
}
else
{
it++;
}
}
}
void Test()
{
List<int> l1;
l1.PushBack(1);
l1.PushBack(2);
l1.PushBack(3);
l1.PushBack(4);
l1.PushBack(5);
PrintList(l1);
l1.PopBack();
l1.PopBack();
l1.PopBack();
l1.PopBack();
PrintList(l1);
}
void TestList()
{
List<int > l;
l.PushFront(1);
l.PushFront(2);
l.PushFront(3);
PrintList(l);
Print(l);
PrintList(l);
}
int main()
{
TestList();
return 0;
}