-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathC++DListReview.cpp
More file actions
187 lines (182 loc) · 2.92 KB
/
C++DListReview.cpp
File metadata and controls
187 lines (182 loc) · 2.92 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
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
180
181
182
183
184
185
186
187
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
#include<assert.h>
using namespace std;
//C++双向链表复习 带头节点和尾结点
template<class T>
struct DListNode
{
T _data;
DListNode<T>* _next;
DListNode<T>* _prev;
DListNode(const T& data)
:_data(data)
, _next(NULL)
, _prev(NULL)
{
cout << "构造结点" << endl;
}
};
template<class T>
class DList
{
typedef DListNode<T> Node;
private:
Node* _head;
Node* _tail;
public:
DList()
:_head(new Node(T()))
,_tail(_head)
{
_head->_next = _head;
_head->_prev = _head;
cout << "构造链表头结点" << endl;
}
DList(const DList<T>& l1)
{
_head = new Node(T());
_tail = _head;
if (l1._head->_next == l1._head)
{
return;
}
Node* cur = (l1._head)->_next;
while (cur != l1._head)
{
PushBack(cur->_data);
cur = cur->_next;
}
}
//赋值运算符重载
DList<T>& operator=(const DList<T>& l1)
{
if (this != &l1)
{
if (l1._head->_next == l1._head)
{
return *this;
}
if (_head->_next != _head) //当原来的对象不为空的时候,释放原来的空间
{
Destory();
}
Node* cur = l1._head->_next;
while (cur != l1._head)
{
PushBack(cur->_data);
cur = cur->_next;
}
}
return *this;
}
//尾插
void PushBack(const T& data)
{
Node* newnode = new Node(data);
_tail->_next = newnode;
newnode->_prev = _tail;
newnode->_next = _head;
_head->_prev = newnode;
_tail = newnode;
}
//头插
void PushFront(const T& data)
{
Node* newnode = new Node(data);
Node* tmp = _head->_next;
_head->_next = newnode;
newnode->_prev = _head;
newnode->_next = tmp;
tmp->_prev = newnode;
}
void PopBack()
{}
void PopFront()
{}
Node* Find(const T& data)
{
if (_head->_next == _head)
return NULL;
else
{
Node* cur = _head->_next;
while (cur != _head)
{
if (cur->_data == data)
return cur;
cur = cur->_next;
}
return NULL;
}
}
//实现随机位置的插入和删除
void Insert(const T& data,Node* pos)
{}
void Erase(Node* pos)
{}
bool Empty()
{
if (_head->_next == _head)
return true;
else
return false;
}
//不销毁头结点的destory
void Destory()
{
Node* cur = _head->_next;
while (cur != _head)
{
Node* tmp = cur;
cur = cur->_next;
delete tmp;
}
_head->_next = _head; //记住自己刚开始是怎么构造的,后面还需要回复最初的状态
_head->_prev = _head;
_tail = _head;
}
~DList()
{
Destory();
delete _head;
_head = _tail = NULL;
}
void Print()
{
Node* cur = _head->_next;
while (cur != _head)
{
cout << cur->_data << " ";
cur = cur->_next;
}
cout << endl;
}
};
//----------------test-----------------------------
void test()
{
DList<int> d1;
d1.PushBack(1);
d1.PushBack(2);
d1.PushBack(3);
d1.PushBack(4);
d1.PushBack(5);
d1.PushBack(6);
d1.Print();
DList<int> d2(d1);
d2.Print();
DList<int> d3;
d3.PushBack(7);
d3.PushBack(8);
d3.PushBack(9);
d3.PushBack(10);
d1 = d3;
d1.Print();
}
int main()
{
test();
return 0;
}