-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyLinkedList.cpp
More file actions
132 lines (118 loc) · 3.15 KB
/
Copy pathMyLinkedList.cpp
File metadata and controls
132 lines (118 loc) · 3.15 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//
// Created by Diego Alberto Ortiz Mariscal A01552000, Diego Mojarro A01638460,
// Luis Armando Salazar A01114901 on 11/26/20.
//
#include "MyLinkedList.h"
#include <string>
#include <utility>
using namespace std;
MyLinkedList::MyLinkedList() {
this->head = this->tail = nullptr;
this->size = 0;
}
MyLinkedList::~MyLinkedList() {
MyNodoLL *next;
while (head != nullptr) {
next = head->next;
delete head;
head = next;
}
}
int MyLinkedList::length() const {
return this->size;
}
bool MyLinkedList::isEmpty() const {
return this->size == 0;
}
RegisterEntry *MyLinkedList::getAt(const string &key) {
MyNodoLL *current = this->head;
for (int i = 0; i < this->size; i++) {
if (current->key == key) {
return ¤t->reg;
}
current = current->next;
}
throw invalid_argument("No se encontró " + key + " en la lista");
}
bool MyLinkedList::isRepeated(const string &key) {
MyNodoLL *current = this->head;
for (int i = 0; i < this->size; i++) {
if (current->key == key) {
return true;
}
current = current->next;
}
return false;
}
MyNodoLL *MyLinkedList::getAt(int pos) {
if (pos >= 0) {
MyNodoLL *current = this->head;
for (int i = 0; i < pos; i++) {
current = current->next;
}
return current;
} else {
throw invalid_argument("No se puede insertar en la posición " + to_string(pos) + " en una lista de tamaño " +
to_string(this->size));
}
}
void MyLinkedList::insertFirst(string key, RegisterEntry reg) {
this->head = new MyNodoLL(std::move(key), move(reg), this->head);
if (this->tail == nullptr) {
this->tail = this->head;
}
this->size++;
}
void MyLinkedList::removeFirst() {
if (this->size > 0) {
MyNodoLL *tmp = this->head;
this->head = this->head->next;
this->size--;
if (this->size == 0) {
this->tail = nullptr;
}
delete tmp;
} else {
throw invalid_argument("No se puede borrar el inicio de una lista vacía");
}
}
void MyLinkedList::removeLast() {
if (this->size <= 1) {
removeFirst();
} else {
MyNodoLL *current = this->head;
for (int i = 0; i < this->size - 2; i++) {
current = current->next;
}
this->tail = current;
delete current->next;
this->tail->next = nullptr;
this->size--;
}
}
void MyLinkedList::removeAt(int pos) {
if (pos == 0) {
removeFirst();
} else {
MyNodoLL *current = this->head;
for (int i = 0; i < pos - 1; i++) {
current = current->next;
}
MyNodoLL *tmp = current->next;
current->next = current->next->next;
if (pos == this->size - 1) {
this->tail = current;
}
this->size--;
delete tmp;
}
}
void MyLinkedList::removeAt(const string &key) {
MyNodoLL *current = this->head;
for (int i = 0; i < this->size; i++) {
if (current->key == key) {
removeAt(i);
}
current = current->next;
}
}