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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
#include <iostream>
#include <cstring>
#include <ctime>
#include <chrono>
using namespace std;
template<typename Item>
class LinkedList{
private:
struct Node{
Item item;
Node* prev;
Node* next;
};
Node* first;
Node* last;
int size;
enum {
up = 0,
down = 1,
};
Node& findElement(int pos, int count = up) const;
public:
LinkedList();
LinkedList(Item);
~LinkedList();
LinkedList& Add(const Item&);
LinkedList& Add(const Item&, int pos);
LinkedList& Delete(int pos, int range = 1);
Item& operator[](int i);
};
template<typename Item>
typename LinkedList<Item>::Node& LinkedList<Item>::findElement(int pos, int count) const{
LinkedList<Item>::Node* current;
if (count == up){
current = first;
for (int k = 1; k <= pos; k++)
current = current->next;
}else{
current = last;
for (int k = size - 2; k >= pos; k--)
current = current->prev;
}
return *current;
}
template<typename Item>
LinkedList<Item>& LinkedList<Item>::Add(const Item& item, int pos){
LinkedList<Item>::Node* temp = new LinkedList<Item>::Node;
temp->item = item;
if (pos >= size){
last = temp;
temp->next = nullptr;
}else{
temp->next = &findElement(pos);
(temp->next)->prev = temp;
}
if (pos == 0){
temp->prev = nullptr;
first = temp;
}
else
if (size != 0){
temp->prev = &findElement(pos - 1);
temp->prev->next = temp;
}
size++;
return *this;
}
template<typename Item>
LinkedList<Item>& LinkedList<Item>::Add(const Item& item){
return Add(item, size);
}
template<typename Item>
LinkedList<Item>::LinkedList(){
size = 0;
first = nullptr;
last = nullptr;
}
template<typename Item>
LinkedList<Item>::LinkedList(Item item){
size = 1;
first = last = new LinkedList<Item>::Node;
first->prev = nullptr;
first->next = nullptr;
first->item = item;
}
template<typename Item>
Item& LinkedList<Item>::operator[](int i){
if (i < 0 || i >= size)
std::cout << "Out of bounds of an array";
return findElement(i).item;
}
template<typename Item>
LinkedList<Item>& LinkedList<Item>::Delete(int pos, int range){
if (pos < 0 || pos >= size)
std::cout << "Out of bounds of an array";
int tempSize = size;
for (int i = pos; i < pos + range && i < size; i++, tempSize--)
delete& findElement(i, down);
if (tempSize == 0)
first = last = nullptr;
else{
if (pos == 0)
{
first = &findElement(pos + range, down);
first->prev = nullptr;
}
else
if (pos + range >= size){
last = &findElement(pos - 1);
last->next = nullptr;
}else{
findElement(pos - 1).next = &findElement(pos + range, down);
findElement(pos + range, down).prev = &findElement(pos - 1);
}
}
size = tempSize;
return *this;
}
template<typename Item>
LinkedList<Item>::~LinkedList(){
Delete(0, size);
}
void arr(int size);
void dll(int size);
int main() {
srand(time(NULL));
int size;
while (true){
cout << "Insert the size: ";
cin >> size;
if (!cin or size <= 0) {
cin.clear();
cin.ignore(10000, '\n');
}
else
break;
}
arr(size);
dll(size);
return 0;
}
void arr(int size) {
int ind = 0, count = 0;
int array[size];
for (int i = 0;i < size;i++) {
array[i] = rand() % 10;
}
auto start = chrono::high_resolution_clock::now();
for (int i = 0;i < size;i++) {
if (array[i] == 0) {
ind++;
if (ind == 3) { count++;ind = 0; }
}
else ind = 0;
}
auto end = chrono::high_resolution_clock::now();
chrono::duration<double> duretion = end - start;
cout << "Duration: " << duretion.count() << "s" << endl;
cout << "Number of sequences 000 : " << count << "\n";
}
void dll(int size) {
int ind = 0, count = 0;
LinkedList<int>aye;
for (int i = 0; i < size; i++) {
aye.Add(rand() % 10);
}
auto start = chrono::high_resolution_clock::now();
for (int i = 0; i < size; i++) {
if (aye[i] == 0) {
ind++;
if (ind == 3) { count++; ind = 0; }
}
else ind = 0;
}
auto end = chrono::high_resolution_clock::now();
chrono::duration<double> duretion = end - start;
cout << "Duration: " << duretion.count() << "s" << endl;
cout << "Number of sequences 000 : " << count << "\n";
}
| |