-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyList.h
179 lines (169 loc) · 3.73 KB
/
myList.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#pragma once
#include <iostream>
template <class ElemType>
class Node
{
public:
ElemType data;
Node<ElemType>* next;
Node<ElemType>* prev;
Node(ElemType item)
{ // init node object
data = item;
prev = NULL;
next = NULL;
}
};
// ʵÏÖÒ»¸öË«ÏòÁ´±í
template <class ElemType>
class List
{
private:
Node<ElemType>* header; // head node
Node<ElemType>* trailer; // tail node
int size = 0; // size of List
public:
List()
{
header->next = trailer;
header->prev = NULL;
trailer->prev = header;
trailer->next = NULL;
}
~List() { clear(); }
bool empty() { return size == 0; } // if empty, return true;
int getSize() { return this->size; }
void clear(); // remove all the nodes
Node<ElemType>* getNode(int pos);
Node<ElemType>* first()
{
if (!size)
return NULL;
else
return header->next;
}
Node<ElemType>* last()
{
if (!size)
return NULL;
else
return trailer->prev;
}
void insertAsFirst(ElemType const& item);
void insertAsLast(ElemType const& item);
void insertAsPred(int pos, ElemType const& item);
void insertAsSucc(int pos, ElemType const& item);
void diaplay(); // display all the elements;
ElemType remove(Node<ElemType>* p);
Node<ElemType>* find(ElemType const& item);
};
template <class ElemType>
void List<ElemType>::insertAsFirst(const ElemType& item)
{
Node<ElemType> s(item); // init a new node
s.next = header->next;
s.prev = header;
header->next->prev = s;
header->next = s;
size++;
}
template <class ElemType>
void List<ElemType>::insertAsLast(const ElemType& item)
{
Node<ElemType> s(item); // init a new node
s.next = trailer;
s.prev = trailer->prev;
trailer->prev->next = s;
trailer->prev = s;
size++;
}
template <class ElemType>
Node<ElemType>* List<ElemType>::getNode(int pos)
{
if (pos > size)
return NULL;
Node<ElemType>* tmp = header;
for (int i = 0; i < pos; i++)
tmp = tmp->next;
return tmp;
}
template <class ElemType>
void List<ElemType>::insertAsSucc(int pos, const ElemType& item)
{
if (pos > size)
{
cout << "Out of rebound" << endl;
return;
}
Node<ElemType>* p = getNode(pos);
Node<ElemType> s(item);
// insert s succeed p;
s->next = p->next;
s->prev = p;
p->next->prev = s;
p->next = s;
size++;
}
template <class ElemType>
void List<ElemType>::insertAsPred(int pos, const ElemType& item)
{
if (pos > size)
{
cout << "Out of rebound" << endl;
return;
}
Node<ElemType>* p = getNode(pos);
Node<ElemType> s(item);
// insert s before p;
s->prev = p->prev;
s->next = p;
p->prev->next = s;
p->prev = s;
size++;
}
template <class ElemType>
ElemType List<ElemType>::remove(Node<ElemType>* p)
{
// p must point to a node in the list
// remove p and return
ElemType data = p->data;
p->prev->next = p->next;
p->next->prev = p->prev;
delete p;
size--;
return data;
}
template <class ElemType>
void List<ElemType>::diaplay()
{
Node<ElemType> p = header;
while (p->next != NULL)
{
cout << p->next->data;
p = p->next;
}
}
template <class ElemType>
void List<ElemType>::clear()
{
Node<ElemType> p = first();
while (!size)
{
remove(p);
p = first();
}
}
template <class ElemType>
Node<ElemType>* List<ElemType>::find(ElemType const& item)
{
if (empty())
return NULL;
Node<ElemType>* p = last();
for (int i = size; i--; i > 0)
{
if (p->data == item)
return p;
p = p->prev;
}
return NULL;
}