Skip to content

Commit 05db76f

Browse files
committed
Changed all classes structure, move implementation into .cpp files and created Makefiles; Write Tree->BinTree convert function.
1 parent 72564ae commit 05db76f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+2647
-2869
lines changed

List/CircList/Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
INCLUDE := -I.
2+
DEPEND := main.cpp circlist.h circlist.cpp
3+
4+
all: main
5+
6+
main: $(DEPEND)
7+
g++ -Wall -std=c++11 -g $(INCLUDE) main.cpp -o main
8+
9+
run: main
10+
main
11+
12+
gdb: main
13+
gdb -q main.exe
14+
15+
clean:
16+
del main.exe

List/circlist.h renamed to List/CircList/circlist.cpp

Lines changed: 42 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,4 @@
1-
#ifndef CIRCLIST_H
2-
#define CIRCLIST_H
3-
4-
#include <iostream>
5-
6-
using namespace std;
7-
8-
template<typename DataType>
9-
class CircList
10-
{
11-
friend ostream & operator <<(ostream& out, const CircList<DataType>& list) // tested
12-
{
13-
out << endl;
14-
CircList<DataType>::iterator it = list.begin();
15-
for(int i = 0; i < list.size(); i++, it++)
16-
{
17-
out << (*it) << endl;
18-
}
19-
20-
return out;
21-
}
22-
23-
private:
24-
class Node
25-
{
26-
public:
27-
DataType data;
28-
Node *link = NULL;
29-
30-
Node(){}
31-
Node(DataType element): data(element){}
32-
Node(Node *ptr): link(ptr){}
33-
Node(DataType element, Node *ptr): data(element), link(ptr){}
34-
~Node(){link = NULL;}
35-
}*first = NULL, *rear = NULL;
36-
int length = 0;
37-
38-
private:
39-
Node* creat_node()const;
40-
Node* creat_node(DataType element)const;
41-
Node* creat_node(DataType element, Node* ptr)const;
42-
Node* creat_node(Node* node)const;
43-
void memory_error(Node* p)const;
44-
45-
public:
46-
class iterator
47-
{
48-
public:
49-
Node* ptr = NULL;
50-
51-
public:
52-
iterator(){}
53-
iterator(Node* p): ptr(p){}
54-
iterator(const iterator& it): ptr(it.ptr){}
55-
~iterator(){ptr = NULL;}
56-
iterator& operator =(const iterator& it){ptr = it.ptr; return *this;}
57-
bool operator ==(const iterator& it){return (ptr == it.ptr);}
58-
bool operator !=(const iterator& it){return (ptr != it.ptr);}
59-
DataType& operator *(){return ptr->data;}
60-
iterator& operator ++(){ptr = ptr->link; return *this;}
61-
iterator operator ++(int){iterator it_temp = *this; ptr = ptr->link; return it_temp;}
62-
};
63-
64-
class const_iterator
65-
{
66-
public:
67-
Node* ptr = NULL;
68-
69-
public:
70-
const_iterator(){}
71-
const_iterator(Node* p): ptr(p){}
72-
const_iterator(const iterator& it): ptr(it.ptr){}
73-
const_iterator(const const_iterator& it): ptr(it.ptr){}
74-
~const_iterator(){ptr = NULL;}
75-
const_iterator& operator =(const iterator& it){ptr = it.ptr;return *this;}
76-
const_iterator& operator =(const const_iterator& it){ptr = it.ptr;return *this;}
77-
bool operator ==(const iterator& it){return (ptr == it.ptr);}
78-
bool operator ==(const const_iterator& it){return (ptr == it.ptr);}
79-
bool operator !=(const iterator& it){return (ptr != it.ptr);}
80-
bool operator !=(const const_iterator& it){return (ptr != it.ptr);}
81-
DataType operator *(){return ptr->data;}
82-
const_iterator& operator ++(){ptr = ptr->link; return *this;}
83-
const_iterator operator ++(int){const_iterator it_temp = *this; ptr = ptr->link; return it_temp;}
84-
};
85-
86-
public:
87-
CircList<DataType>(){}; // tested
88-
CircList<DataType>(int n); // tested
89-
CircList<DataType>(int n, DataType element); // tested
90-
CircList<DataType>(const CircList<DataType>& list); // tested
91-
~CircList<DataType>(); // tested
92-
void clear(); // tested
93-
CircList<DataType>& operator =(const CircList<DataType>& list); // tested
94-
int size()const; // tested
95-
bool empty()const; // tested
96-
DataType& operator [](int i); // tested
97-
DataType operator [](int i)const; // tested
98-
int locate(DataType element)const;
99-
void insert(int n, DataType element); // tested
100-
bool erase(int n); // tested
101-
void push_back(DataType element); // tested
102-
void push_front(DataType element); // tested
103-
DataType pop_back(); // tested
104-
DataType pop_front(); // tested
105-
bool swap(int i, int j); // tested
106-
CircList<DataType> cat(const CircList<DataType>& list); // tested
107-
iterator begin()const{return iterator(first);}
108-
iterator last()const{return iterator(rear);}
109-
};
1+
#ifdef CIRCLIST_H
1102

1113
template<typename DataType>
1124
void CircList<DataType>::memory_error(CircList<DataType>::Node* p)const
@@ -120,7 +12,7 @@ void CircList<DataType>::memory_error(CircList<DataType>::Node* p)const
12012
}
12113

12214
template<typename DataType>
123-
typename CircList<DataType>::Node* CircList<DataType>::creat_node()const
15+
typename CircList<DataType>::Node* CircList<DataType>::new_Node()const
12416
{
12517
CircList<DataType>::Node* p = new CircList<DataType>::Node;
12618
memory_error(p);
@@ -129,7 +21,7 @@ typename CircList<DataType>::Node* CircList<DataType>::creat_node()const
12921
}
13022

13123
template<typename DataType>
132-
typename CircList<DataType>::Node* CircList<DataType>::creat_node(DataType element)const
24+
typename CircList<DataType>::Node* CircList<DataType>::new_Node(DataType element)const
13325
{
13426
CircList<DataType>::Node* p = new CircList<DataType>::Node(element);
13527
memory_error(p);
@@ -138,7 +30,7 @@ typename CircList<DataType>::Node* CircList<DataType>::creat_node(DataType eleme
13830
}
13931

14032
template<typename DataType>
141-
typename CircList<DataType>::Node* CircList<DataType>::creat_node(DataType element, CircList<DataType>::Node* ptr)const
33+
typename CircList<DataType>::Node* CircList<DataType>::new_Node(DataType element, CircList<DataType>::Node* ptr)const
14234
{
14335
CircList<DataType>::Node* p = new CircList<DataType>::Node(element, ptr);
14436
memory_error(p);
@@ -147,7 +39,7 @@ typename CircList<DataType>::Node* CircList<DataType>::creat_node(DataType eleme
14739
}
14840

14941
template<typename DataType>
150-
typename CircList<DataType>::Node* CircList<DataType>::creat_node(CircList<DataType>::Node *node)const
42+
typename CircList<DataType>::Node* CircList<DataType>::new_Node(CircList<DataType>::Node *node)const
15143
{
15244
CircList<DataType>::Node* p = new CircList<DataType>::Node(node->data);
15345
memory_error(p);
@@ -169,11 +61,11 @@ CircList<DataType>::CircList(int n)
16961
return;
17062
}
17163

172-
first = creat_node();
64+
first = new_Node();
17365
CircList<DataType>::Node* p = first;
17466
for(int i = 1; i < n; i++)
17567
{
176-
p->link = creat_node();
68+
p->link = new_Node();
17769
p = p->link;
17870
}
17971
p->link = first;
@@ -195,11 +87,11 @@ CircList<DataType>::CircList(int n, DataType element)
19587
return;
19688
}
19789

198-
first = creat_node(element);
90+
first = new_Node(element);
19991
CircList<DataType>::Node* p = first;
20092
for(int i = 1; i < n; i++)
20193
{
202-
p->link = creat_node(element);
94+
p->link = new_Node(element);
20395
p = p->link;
20496
}
20597
p->link = first;
@@ -215,12 +107,12 @@ CircList<DataType>::CircList(const CircList<DataType>& list)
215107
return;
216108
}
217109

218-
first = creat_node(list.first);
110+
first = new_Node(list.first);
219111
CircList<DataType>::Node *p = first;
220112
CircList<DataType>::Node *q = list.first;
221113
for(int i = 1; i < list.length; i++)
222114
{
223-
p->link = creat_node(q->link);
115+
p->link = new_Node(q->link);
224116
p = p->link;
225117
q = q->link;
226118
}
@@ -273,12 +165,12 @@ CircList<DataType>& CircList<DataType>::operator =(const CircList<DataType>& lis
273165
return *this;
274166
}
275167

276-
first = creat_node(list.first);
168+
first = new_Node(list.first);
277169
CircList<DataType>::Node *p = first;
278170
CircList<DataType>::Node *q = list.first;
279171
for(int i = 1; i < list.length; i++)
280172
{
281-
p->link = creat_node(q->link);
173+
p->link = new_Node(q->link);
282174
p = p->link;
283175
q = q->link;
284176
}
@@ -364,7 +256,7 @@ void CircList<DataType>::insert(int n, DataType element)
364256
{
365257
if(length == 0)
366258
{
367-
first = creat_node(element);
259+
first = new_Node(element);
368260
first->link = first;
369261
rear = first;
370262
length = 1;
@@ -379,19 +271,19 @@ void CircList<DataType>::insert(int n, DataType element)
379271

380272
if(n == 0)
381273
{
382-
rear->link = creat_node(element, first);
274+
rear->link = new_Node(element, first);
383275
first = rear->link;
384276
}
385277
else if(ni == 0)
386278
{
387-
rear->link = creat_node(element, first);
279+
rear->link = new_Node(element, first);
388280
rear = rear->link;
389281
}
390282
else
391283
{
392284
CircList<DataType>::Node *p = first;
393285
for(int i = 0; i < ni-1; i++, p = p->link){}
394-
p->link = creat_node(element, p->link);
286+
p->link = new_Node(element, p->link);
395287
}
396288
length++;
397289
}
@@ -442,14 +334,14 @@ void CircList<DataType>::push_back(DataType element)
442334
{
443335
if(length == 0)
444336
{
445-
first = creat_node(element);
337+
first = new_Node(element);
446338
first->link = first;
447339
rear = first;
448340
length = 1;
449341
return;
450342
}
451343

452-
rear->link = creat_node(element, first);
344+
rear->link = new_Node(element, first);
453345
rear = rear->link;
454346
length++;
455347
}
@@ -459,14 +351,14 @@ void CircList<DataType>::push_front(DataType element)
459351
{
460352
if(length == 0)
461353
{
462-
first = creat_node(element);
354+
first = new_Node(element);
463355
first->link = first;
464356
rear = first;
465357
length = 1;
466358
return;
467359
}
468360

469-
rear->link = creat_node(element, first);
361+
rear->link = new_Node(element, first);
470362
first = rear->link;
471363
length++;
472364
}
@@ -629,7 +521,7 @@ CircList<DataType> CircList<DataType>::cat(const CircList<DataType>& list)
629521
CircList<DataType>::Node *p = list.first;
630522
for(int i = 0; i < list.length; i++)
631523
{
632-
rear->link = creat_node(p);
524+
rear->link = new_Node(p);
633525
rear = rear->link;
634526
p = p->link;
635527
}
@@ -638,4 +530,24 @@ CircList<DataType> CircList<DataType>::cat(const CircList<DataType>& list)
638530
return *this;
639531
}
640532

533+
template<class DataType>
534+
ostream & operator <<(ostream& out, const CircList<DataType>& list)
535+
{
536+
if(list.empty())
537+
{
538+
return out;
539+
}
540+
541+
int n = list.size();
542+
typename CircList<DataType>::iterator it = list.begin();
543+
out << (*it);
544+
it++;
545+
for(int i = 1; i < n; i++, it++)
546+
{
547+
out << ", " << (*it);
548+
}
549+
550+
return out;
551+
}
552+
641553
#endif

0 commit comments

Comments
 (0)