Kaynağa Gözat

c++ ploxoi ti yap

jezv 1 yıl önce
ebeveyn
işleme
630664ca43
4 değiştirilmiş dosya ile 84 ekleme ve 26 silme
  1. 28 0
      .vscode/tasks.json
  2. 40 23
      CircleList.h
  3. BIN
      main
  4. 16 3
      main.cpp

+ 28 - 0
.vscode/tasks.json

@@ -0,0 +1,28 @@
+{
+    "tasks": [
+        {
+            "type": "cppbuild",
+            "label": "C/C++: g++ build active file",
+            "command": "/usr/bin/g++",
+            "args": [
+                "-fdiagnostics-color=always",
+                "-g",
+                "${file}",
+                "-o",
+                "${fileDirname}/${fileBasenameNoExtension}"
+            ],
+            "options": {
+                "cwd": "${fileDirname}"
+            },
+            "problemMatcher": [
+                "$gcc"
+            ],
+            "group": {
+                "kind": "build",
+                "isDefault": true
+            },
+            "detail": "Task generated by Debugger."
+        }
+    ],
+    "version": "2.0.0"
+}

+ 40 - 23
CircleList.h

@@ -2,43 +2,60 @@
 #include <iostream>
 
 template <typename T>
-class CircleList: public List
+class CircleList: public List<T>
 {
     public:
-    Node<T>* get_start() {
-        return start;
-    }
+        Node<T>* get_start() {
+            return this->start;
+        }
+
+        CircleList() {}
 
-    void add(T value) {
-        Node<T>* node = new Node<T>;
-        node->value = value;
-        if(start==nullptr) {
-            start = node;
-            end = node;
+        void add (T value) override{
+            Node<T>* node = new Node<T>;
+            node->value = value;
+            if(this->start==nullptr) {
+                this->start = node;
+                this->end = node;
+                return;
+            }
+            node->prev = this->end;
+            node->next = this->start;
+            this->end->next = node;
+            this->start->prev = node;
+            this->end = node;
+            this->lenght++;
+        }
+
+        void add (T value, int index) override{
+            static_cast<List<T>*>(this)->add(value, index);
             return;
         }
-        node->prev = end;
-        node->next = start;
-        end = node;
-        lenght++;
-    }
 
-    void pop() {
-        end->prev->next = start;
-        end = end->prev;
-        lenght--;
-    }
+        void pop(Node<T>* n) {
+            this->pop(n);
+        }
+
+        void pop() {
+            this->end->prev->next = this->start;
+            this->end = this->end->prev;
+            this->lenght--;
+        }
+
+        void pop(int index) {
+            this->pop(index);
+        }
 
-    friend std::ostream &operator <<(std::ostream &os, CircleList &c)
+        friend std::ostream &operator <<(std::ostream &os, CircleList &c)
     {
         os << "( ";
         Node<T>* currentNode = c.start;
-        while (currentNode->next!=nullptr)
+        for(int i = 0; i < c.lenght; i++)
         {
             os << currentNode->value << " = ";
             currentNode = currentNode->next;
         }
-        os << currentNode->value << " )";
+        os << currentNode->value << "... )";
         return os;
     }
 


+ 16 - 3
main.cpp

@@ -4,7 +4,20 @@ using namespace std;
 
 int main() {
     CircleList<int> l;
-    for(int i = 0; i < 10; i++) l.add(i);
-    cout << l;
-    return 0;
+    for(int i = 0; i < 10; i++) {
+        l.add(i);
+    }
+    cout << l << endl;
+    l.add(3, 3);
+    cout << l << endl;
+    l.add(5);
+    cout << l << endl;
+    l.pop();
+    cout << l << endl;
+    l.pop(2);
+    Node<int>* n = l.get_start();
+    for(int i = 0; i < 20; i++) {
+        cout << n << endl;
+        n = n->next;
+    }
 }