jezv 1 gadu atpakaļ
vecāks
revīzija
f1b90fd2ee
3 mainītis faili ar 87 papildinājumiem un 8 dzēšanām
  1. 57 3
      List.h
  2. BIN
      main
  3. 30 5
      main.cpp

+ 57 - 3
List.h

@@ -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;
     }
 };

BIN
main


+ 30 - 5
main.cpp

@@ -1,12 +1,37 @@
 #include "List.h"
 #include <iostream>
+#include <time.h>
 
 using namespace std;
 
-int main() {
+
+void iosif(int count, int step)
+{
+    clock_t start = clock();
     List<int> l;
-    for(int i = 0; i < 10; i++) l.add(i);
-    cout << l << endl;
-    l.pop();
-    cout << l << endl;
+    for(int i = 0; i < count; i++) {
+        l.add(i);
+    }
+
+    int i = 0;
+    while(l.lenght > 1) {
+        l.pop(i);
+        i += step - 1;
+        i %= l.lenght;
+    }
+
+    clock_t end = clock();
+    double sec = (double)(end - start) / CLOCKS_PER_SEC;
+    cout << "time: " << sec << " answer: " << l[0] << endl;
+}
+
+
+int main() {
+    iosif(1000, 3);
+    iosif(5000, 3);
+    iosif(10000, 3);
+    iosif(50000, 3);
+    iosif(100000, 3);
+    iosif(500000, 3);
+    iosif(1000000, 3);
 }