-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList.h
93 lines (65 loc) · 1.36 KB
/
List.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
#ifndef MAIN_LIST_H
#define MAIN_LIST_H
#include <iostream>
#include "Node.h"
template <typename T>
class List {
public:
List() {};
void push(T data) {
if (this->head == nullptr) {
this->head = new Node<T>(data);
this->end = this->head;
} else {
Node<T> *current = this->end;
current->pNext = new Node<T>(data);
this->end = current->pNext;
}
this->size++;
};
T getElement(int index) {
Node<T> *current = this->head;
int counter = 0;
while (current != nullptr) {
if (counter == index) {
return current->value;
}
current = current->pNext;
counter++;
}
}
int getSize() {
return this->size;
};
void sort() {
Node<T> *ptr1;
Node<T> *lptr = nullptr;
if (this->size == 0) {
return;
}
int swapped;
do {
swapped = 0;
ptr1 = this->head;
while (ptr1->pNext != lptr) {
float firstRatio = ptr1->value.lettersRatio(), secondRatio = ptr1->pNext->value.lettersRatio();
if (ptr1->value > ptr1->pNext->value) {
swap(ptr1, ptr1->pNext);
swapped = 1;
}
ptr1 = ptr1->pNext;
}
lptr = ptr1;
} while (swapped);
}
void swap(Node<T> *a, Node<T> *b) {
T temp = a->value;
a->value = b->value;
b->value = temp;
}
private:
int size = 0;
Node<T> *head = nullptr;
Node<T> *end = nullptr;
};
#endif //MAIN_LIST_H