-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlists_template.h
99 lines (82 loc) · 1.83 KB
/
lists_template.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
#include <iostream>
template <typename Type> class list;
template <typename Type> class list_node_n
{
private:
Type m_data;
list_node_n *m_next;
public:
list_node_n(const Type & data):m_data(data){}
friend class list<Type>;
};
template <typename Type> class list
{
private:
list_node_n<Type> *list_l;
int count;
public:
list();
~list();
bool add_head(list_node_n<Type> *new_node);
bool add_tail(list_node_n<Type> *new_node);
bool remove_head();
bool remove_tail();
bool remove_key(list_node_n<Type> *new_node);
int show_list();
int purge_list();
};
template<typename Type> list<Type>::list()
{
}
template<typename Type> list<Type>::~list()
{
}
template<typename Type> bool list<Type>::add_head(list_node_n<Type> *new_node)
{
new_node->m_next = list_l;
list_l = new_node;
return true;
}
template<typename Type> bool list<Type>::add_tail(list_node_n<Type> *new_node)
{
new_node->m_next = NULL;
list_node_n<Type> *current = list_l;
while(current->m_next!=NULL)
{
current = current->m_next;
}
return true;
}
template<typename Type> int list<Type>::show_list()
{
list_node_n<Type> *current=NULL;
current = list_l;
if (current==NULL)
{
std::cout<<"list is empty!"<<"\n";
return -1;
}
while (current!=NULL)
{
std::cout<<"element: "<<current->m_data<<"\n";
current=current->m_next;
}
return 1;
}
template<typename Type> int list<Type>::purge_list()
{
list_node_n<Type> *current=NULL;
current = list_l;
if (current==NULL)
{
return -1;
}
while (current!=NULL)
{
current = list_l->m_next;
delete list_l;
list_l = current;
}
list_l = NULL;
return 1;
}