| 1 |
cycrow |
1 |
#ifndef EXT_SIMPLE_LIST_INCLUDED
|
|
|
2 |
#define EXT_SIMPLE_LIST_INCLUDED
|
|
|
3 |
namespace ext
|
|
|
4 |
{
|
|
|
5 |
|
|
|
6 |
template <class Ty>
|
|
|
7 |
class simple_list
|
|
|
8 |
{
|
|
|
9 |
public:
|
|
|
10 |
typedef Ty value_type;
|
|
|
11 |
typedef Ty& reference;
|
|
|
12 |
typedef const Ty& const_reference;
|
|
|
13 |
typedef size_t size_type;
|
|
|
14 |
|
|
|
15 |
private:
|
|
|
16 |
struct node
|
|
|
17 |
{
|
|
|
18 |
node *next;
|
|
|
19 |
value_type val;
|
|
|
20 |
};
|
|
|
21 |
|
|
|
22 |
node *m_head;
|
|
|
23 |
node *m_last;
|
|
|
24 |
size_type m_size;
|
|
|
25 |
|
|
|
26 |
public:
|
|
|
27 |
class const_iterator
|
|
|
28 |
{
|
|
|
29 |
protected:
|
|
|
30 |
node *m_node;
|
|
|
31 |
|
|
|
32 |
public:
|
|
|
33 |
const_iterator() { }
|
|
|
34 |
const_iterator(node *n) { m_node=n; }
|
|
|
35 |
const_iterator& operator ++() { m_node=m_node->next; return *this; }
|
|
|
36 |
const_iterator operator ++(int) { node *old=m_node; m_node=m_node->next; return const_iterator(old); }
|
|
|
37 |
|
|
|
38 |
const_reference operator *() const { return m_node->val; }
|
|
|
39 |
const_reference operator ->() const { return m_node->val; }
|
|
|
40 |
|
|
|
41 |
bool operator ==(const_iterator& right) const { return m_node==right.m_node; }
|
|
|
42 |
bool operator !=(const_iterator& right) const { return m_node!=right.m_node; }
|
|
|
43 |
};
|
|
|
44 |
|
|
|
45 |
class iterator : public const_iterator
|
|
|
46 |
{
|
|
|
47 |
public:
|
|
|
48 |
iterator() { }
|
|
|
49 |
iterator(node *n) : const_iterator(n) { }
|
|
|
50 |
|
|
|
51 |
iterator& operator ++() { m_node=m_node->next; return *this; }
|
|
|
52 |
iterator operator ++(int) { node *old=m_node; m_node=m_node->next; return iterator(old); }
|
|
|
53 |
|
|
|
54 |
reference operator *() { return m_node->val; }
|
|
|
55 |
reference operator ->() { return m_node->val; }
|
|
|
56 |
};
|
|
|
57 |
|
|
|
58 |
public:
|
|
|
59 |
simple_list() { m_head=new node(); m_head->next=m_head; clear(); }
|
|
|
60 |
simple_list(const simple_list& other)
|
|
|
61 |
{
|
|
|
62 |
m_head=new node(); m_head->next=m_head; clear();
|
|
|
63 |
for(const_iterator &it=other.begin(); it!=other.end(); ++it){
|
|
|
64 |
push_back(*it);
|
|
|
65 |
}
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
~simple_list() { clear(); delete m_head; }
|
|
|
69 |
|
|
|
70 |
void clear()
|
|
|
71 |
{
|
|
|
72 |
node *n, *old;
|
|
|
73 |
for(old=0, n=m_head->next; n!=m_head; old=n, n=n->next){
|
|
|
74 |
delete old;
|
|
|
75 |
}
|
|
|
76 |
delete old;
|
|
|
77 |
|
|
|
78 |
m_head->next=m_head;
|
|
|
79 |
m_last=m_head;
|
|
|
80 |
m_size=0;
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
iterator begin() { return iterator(m_head->next); }
|
|
|
84 |
iterator end() { return iterator(m_head); }
|
|
|
85 |
|
|
|
86 |
const_iterator begin() const { return const_iterator(m_head->next); }
|
|
|
87 |
const_iterator end() const { return const_iterator(m_head); }
|
|
|
88 |
|
|
|
89 |
size_type size() const { return m_size; }
|
|
|
90 |
|
|
|
91 |
iterator push_back(const_reference val)
|
|
|
92 |
{
|
|
|
93 |
node *n=new node();
|
|
|
94 |
n->val=val;
|
|
|
95 |
m_last->next=n;
|
|
|
96 |
n->next=m_head;
|
|
|
97 |
m_last=n;
|
|
|
98 |
m_size++;
|
|
|
99 |
return iterator(n);
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
reference front() { return *begin(); }
|
|
|
103 |
reference back() { return m_last->val; }
|
|
|
104 |
|
|
|
105 |
const_reference front() const { return *begin(); }
|
|
|
106 |
const_reference back() const { return m_last->val; }
|
|
|
107 |
};
|
|
|
108 |
|
|
|
109 |
};
|
|
|
110 |
|
|
|
111 |
#endif // !defined(EXT_SIMPLE_LIST_INCLUDED)
|