-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist.h
138 lines (127 loc) · 2.69 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
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
#ifndef LIST_H
#define LIST_H
#include <iostream>
#include "node.h"
#include "iterator.h"
using namespace std;
template <typename T>
class List {
private:
Node<T>* start;
int nodes;
public:
List() {
start = nullptr;
nodes=0;
}
T front() {
if(start==nullptr) {
cout << "lista vacia"; // Da warning, debería ser un throw
}
else {
return start->data;
}
}
T back() {
if(start==nullptr){
cout<<"lista vacia"; // Da warning, debería ser un throw
}
else {
return start->prev->data;
}
}
void push_front(T value) {
Node<T> *nuevo= new Node<T>();
nuevo->data= value;
if(start== nullptr){
start= nuevo;
start->prev= nuevo;
start->next=nuevo;
}
else{
start->prev->next=nuevo;
nuevo->next=start;
nuevo->prev=start->prev;
start->prev=nuevo;
}
start=nuevo;
nodes++;
}
void push_back(T value) {
Node<T> * nuevo = new Node<T>();
nuevo->data = value;
if (start == nullptr) {
start= nuevo;
start->prev= nuevo;
start->next=nuevo;
}
else {
nuevo->next=start;
nuevo->prev=start->prev;
start->prev->next=nuevo;
start->prev=nuevo;
}
nodes++;
}
void pop_front(){ // No estás borrando el elemento
if(start==nullptr){
cout<<"lista vacia";
}
else if(start->next==start){
start= nullptr;
}
else{
//auto* aux=start;
start->prev->next=start->next;
start->next->prev=start->prev;
start=start->next;
//delete (aux);
}
nodes--;
}
void pop_back(){ // No estás borrando el elemento
if(start==nullptr){
cout<<"lista vacia";
}
else if(start->next==start){
start= nullptr;
}
else{
// auto* aux=start;
start->prev->prev->next=start;
start->prev->prev=start->prev;
// delete (aux);
}nodes--;
}
T get(int position){
auto* aux=start;
for(int i=0;i<position;i++){ // Podrías usar el módulo
aux=aux->next;
}
return(aux->data);
}
void concat(List<T> &other){
start->prev->next = other.start; // Falta
other.start->prev=start->prev;
}
bool empty(){
return(nodes==0);
}
int size(){
return(nodes);
}
void clear(){
while(nodes > 0){
pop_back();
}
nodes=0;
}
Iterator<T> begin() {
return Iterator<T>(start);
}
Iterator<T> end(){
return Iterator<T>(start->prev);
}
~List(){} // No se implementó
};
#endif