-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMy_forward_list.h
159 lines (138 loc) · 3.71 KB
/
My_forward_list.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#pragma once
#include "MyForwardIterator.h"
#include "error_types.h"
//Чтобы определения шаблонов увидели реализацию
template <class Type>
class MyForwardIterator;
template <class Type>
struct My_list_element
{
public:
My_list_element(Type const& el, My_list_element<Type> *n): m_element(el), m_next(n)
{
}
Type m_element;
My_list_element<Type>* m_next;
};
template <class Type>
class My_forward_list
{
public:
My_forward_list():m_number(0), m_first(NULL), m_last(NULL)
{
}
My_forward_list(My_forward_list<Type> const& orig):m_number(0), m_first(NULL), m_last(NULL) //throw(My_forward_list_errors)
{
if(orig.is_empty())
return;
MyForwardIterator<Type> i=orig.get_beginning();
while(i!=orig.get_ending())
{
add_in_ending(*i);
i++;
}
add_in_ending(*i);
}
~My_forward_list()
{
clear_list();
}
MyForwardIterator<Type> get_beginning() const
{
return MyForwardIterator<Type>(m_first);
}
MyForwardIterator<Type> get_ending() const
{
return MyForwardIterator<Type>(m_last);
}
void add_in_beginning(Type const& element) //throw(My_forward_list_errors)
//try
{
if(m_number==0)
m_first=m_last=new My_list_element<Type>(element, NULL);
else
m_first=new My_list_element<Type>(element, m_first);
m_number++;
}
/*catch(std::bad_alloc& ba)
{
throw CANT_ALLOCATE_MEMORY;
}*/
void add_in_ending(Type const& element) //throw(My_forward_list_errors)
//try
{
if(m_number==0)
m_first=m_last=new My_list_element<Type>(element, NULL);
else
{
m_last->m_next=new My_list_element<Type>(element, NULL);
m_last=m_last->m_next;
}
m_number++;
}
/*catch(std::bad_alloc& ba)
{
throw CANT_ALLOCATE_MEMORY;
}*/
void delete_first() throw(My_forward_list_errors)
{
if(m_number==0)
throw ACCESS_TO_ELEMNT_FROM_EMPTY_LIST;
else if(m_number==1)
{
delete m_first;
m_first=m_last=NULL;
}
else
{
My_list_element<Type> *temp=m_first->m_next;
delete m_first;
m_first=temp;
}
m_number--;
}
void delete_last() throw(My_forward_list_errors)
{
if(m_number==0)
throw ACCESS_TO_ELEMNT_FROM_EMPTY_LIST;
else if(m_number==1)
{
delete m_first;
m_first=m_last=NULL;
m_number=0;
}
else
{
//ищем предпоследний
My_list_element<Type>* a=m_first;
while(a->m_next != m_last)//пока следующий за текущим не послендий
a=a->m_next;
delete m_last;
m_last=a;
m_last->m_next=NULL;
m_number--;
}
}
void clear_list()
{
My_list_element<Type> *temp;
while(m_first!=NULL)//Пока есть что удалять
{
temp=m_first;
m_first=m_first->m_next;
delete temp;
}
m_number=0;
}
bool is_empty() const
{
return m_number==0;
}
int get_element_number() const
{
return m_number;
}
private:
int m_number;
My_list_element<Type> *m_first, *m_last;
};