-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.h
More file actions
110 lines (101 loc) · 2.17 KB
/
List.h
File metadata and controls
110 lines (101 loc) · 2.17 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
#ifndef _LIST_H_
#define _LIST_H_
#include "algorithm"
template <class val>
struct node {
val data;
node <val> *next;
};
template <class val>
class list {
private:
node <val> *head;
node <val> *end;
int size;
public:
list();
node <val> *find(val el);
void insert(val el);
void del(val el);
void sort();
void swap(val a, val b);
};
template <class val>
list <val>::list() {
head = NULL;
end = NULL;
size = 0;
}
template <class val>
void list <val>::insert(val el) {
node <val> *temp = new node <val>();
temp -> data = el;
temp -> next = NULL;
if (head == NULL) {
head = temp;
return;
}
end -> next = temp;
end = temp;
size++;
return;
}
template <class val>
void list <val>::del(val el) {
if (head == NULL) {
return;
} else {
node <val> *temp = head;
if (head -> data == el) {
head = head -> next;
delete temp;
size--;
} else {
while (temp -> next != NULL) {
if (temp -> next -> data == el) {
node <val> *c = temp -> next;
temp -> next = temp -> next -> next;
delete c;
size--;
} else {
temp = temp -> next;
}
}
}
}
}
template <class val>
node <val>* list <val>::find(val el) {
node <val> *temp = head;
while (temp != NULL) {
if (temp -> data == el) {
return temp;
} else {
temp = temp -> next;
}
}
return temp;
}
template <class val>
void list <val>::swap(val a, val b) {
node <val> *temp1 = find(a);
node <val> *temp2 = find(b);
if (temp1 == NULL || temp2 == NULL) {
return;
}
std::swap(temp1 -> data, temp2 -> data);
}
template <class val>
void list <val>::sort() {
node <val> *f = head;
while (f != end) {
val min_val = f -> data;
node <val> *temp = f;
while (temp != end) {
min_val = min(min_val, temp -> next -> data);
}
swap(min_val, f -> data);
f = f -> next;
}
}
#endif