-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlist.h
More file actions
139 lines (119 loc) · 2.45 KB
/
list.h
File metadata and controls
139 lines (119 loc) · 2.45 KB
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
#ifndef _SL_LIST__H
#define _SL_LIST__H
/***************************************************************************
*
* Project libmjpeg
*
* Copyright (C) 2014-2015, Changer, <dingjinchengyx@163.com>.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************/
#include "slconfig.h"
template <class T>
class list
{
public:
class node {
public:
node(const T& e):prev(0),next(0),element(e){}
node():prev(0),next(0){}
virtual ~node(){}
public:
node* prev;
node* next;
T element;
};
list():m_size(0){
head = new node();
tail = new node();
head->prev = NULL;
tail->next = NULL;
head->next = tail;
tail->prev = head;
}
~list() {
clear();
delete head;
delete tail;
}
u32 getSize() const {
return m_size;
}
void clear() {
node* p = head->next;
while (p != tail)
{
head->next = p->next;
delete p;
p = head->next;
--m_size;
}
tail->prev = head;
}
bool empty() {
return m_size == 0;
}
void push_back(const T& element) {
node *p_node = new node(element);
tail->prev->next = p_node;
p_node->prev = tail->prev;
tail->prev = p_node;
p_node->next = tail;
m_size++;
}
void push_front(const T& element) {
node *p_node = new node(element);
p_node->next = head->next;
head->next->prev = p_node;
head->next = p_node;
p_node->prev = head;
m_size++;
}
void insert(const T& element, s32 i) {
if (i > m_size)
return;
node *p_node = new node(element);
node* p = head;
for (int index = 0; index < i; index++)
p = p->next;
p_node->next = p->next;
p->next->prev = p_node;
p_node->prev = p;
p->next = p_node;
m_size++;
}
node* begin() const{
return head->next;
}
node* end() const {
return tail;
}
void erase(s32 i) {
if (i >=(s32)m_size)
return;
node* p = head->next;
for (int index = 0; index < i;index++)
p = p->next;
erase(p);
}
void erase(node* p) {
p->prev->next = p->next;
p->next->prev = p->prev;
delete p;
m_size--;
}
private:
node* head;
node* tail;
u32 m_size;
};
#endif