|
@@ -5,8 +5,8 @@ template <typename T>
|
|
|
struct Node
|
|
|
{
|
|
|
T value;
|
|
|
- Node* next = nullptr;
|
|
|
- Node* prev = nullptr;
|
|
|
+ Node<T>* next = nullptr;
|
|
|
+ Node<T>* prev = nullptr;
|
|
|
};
|
|
|
|
|
|
template <typename T>
|
|
@@ -16,11 +16,36 @@ class List
|
|
|
Node<T>* start = nullptr;
|
|
|
Node<T>* end = nullptr;
|
|
|
|
|
|
+ Node<T>* getNode(int index)
|
|
|
+ {
|
|
|
+ if (index >= lenght)
|
|
|
+ throw "index > lenght";
|
|
|
+ Node<T>* currentNode = start;
|
|
|
+ for(int i = 0; i < index; i++) {
|
|
|
+ currentNode = currentNode->next;
|
|
|
+ }
|
|
|
+ return currentNode;
|
|
|
+ }
|
|
|
+
|
|
|
public:
|
|
|
+ int lenght;
|
|
|
+
|
|
|
List()
|
|
|
{
|
|
|
Node<T>* start = nullptr;
|
|
|
Node<T>* end = nullptr;
|
|
|
+ lenght = 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ ~List()
|
|
|
+ {
|
|
|
+ Node<T>* currentNode = start;
|
|
|
+ while (currentNode->next!=nullptr)
|
|
|
+ {
|
|
|
+ currentNode = currentNode->next;
|
|
|
+ delete currentNode->prev;
|
|
|
+ }
|
|
|
+ delete currentNode;
|
|
|
}
|
|
|
|
|
|
void add(T value)
|
|
@@ -35,12 +60,42 @@ class List
|
|
|
node->prev = end;
|
|
|
end->next = node;
|
|
|
end = node;
|
|
|
+ lenght++;
|
|
|
}
|
|
|
|
|
|
void pop()
|
|
|
{
|
|
|
end = end->prev;
|
|
|
end->next = nullptr;
|
|
|
+ lenght--;
|
|
|
+ }
|
|
|
+
|
|
|
+ void pop(Node<T>* node)
|
|
|
+ {
|
|
|
+ // Node<T>* prev = node->prev;
|
|
|
+ node->next->prev = node->prev;
|
|
|
+ node->prev->next = node->next;
|
|
|
+ lenght--;
|
|
|
+ }
|
|
|
+
|
|
|
+ void pop(int index)
|
|
|
+ {
|
|
|
+ if (index==0) {
|
|
|
+ start = getNode(index+1);
|
|
|
+ lenght--;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (index>=lenght-1) {
|
|
|
+ pop();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Node<T>* node = getNode(index);
|
|
|
+ pop(node);
|
|
|
+ }
|
|
|
+
|
|
|
+ T operator[](int index)
|
|
|
+ {
|
|
|
+ return getNode(index)->value;
|
|
|
}
|
|
|
|
|
|
friend std::ostream &operator <<(std::ostream &os, List &c)
|
|
@@ -53,7 +108,6 @@ class List
|
|
|
currentNode = currentNode->next;
|
|
|
}
|
|
|
os << currentNode->value << " )";
|
|
|
- delete currentNode;
|
|
|
return os;
|
|
|
}
|
|
|
};
|