Bug fix
[c11tester.git] / stl-model.h
index 489939b739c97878a7a264b4af3d0cc08caf3e3a..d787a2d7951d1cb0589f219a2678a7a4060f7ccd 100644 (file)
@@ -59,15 +59,21 @@ public:
        void pop_front() {
                mllnode<_Tp> *tmp = head;
                head = head->next;
-               head->prev = NULL;
+               if (head == NULL)
+                       tail = NULL;
+               else
+                       head->prev = NULL;
                delete tmp;
                _size--;
        }
 
        void pop_back() {
                mllnode<_Tp> *tmp = tail;
-               tail = tail->next;
-               tail->next = NULL;
+               tail = tail->prev;
+               if (tail == NULL)
+                       head = NULL;
+               else
+                       tail->next = NULL;
                delete tmp;
                _size--;
        }
@@ -120,7 +126,7 @@ public:
                if (tail == node) {
                        tail = node->prev;
                } else {
-                       tail->next->prev = node->prev;
+                       node->next->prev = node->prev;
                }
                mllnode<_Tp> *next = node->next;
                delete node;
@@ -208,18 +214,51 @@ public:
                _size++;
        }
 
+       sllnode<_Tp>* add_front(_Tp val) {
+               sllnode<_Tp> * tmp = new sllnode<_Tp>();
+               tmp->prev = NULL;
+               tmp->next = head;
+               tmp->val = val;
+               if (head == NULL)
+                       tail = tmp;
+               else
+                       head->prev = tmp;
+               head = tmp;
+               _size++;
+               return tmp;
+       }
+
+       sllnode<_Tp> * add_back(_Tp val) {
+               sllnode<_Tp> * tmp = new sllnode<_Tp>();
+               tmp->prev = tail;
+               tmp->next = NULL;
+               tmp->val = val;
+               if (tail == NULL)
+                       head = tmp;
+               else tail->next = tmp;
+               tail = tmp;
+               _size++;
+               return tmp;
+       }
+
        void pop_front() {
                sllnode<_Tp> *tmp = head;
                head = head->next;
-               head->prev = NULL;
+               if (head == NULL)
+                       tail = NULL;
+               else
+                       head->prev = NULL;
                delete tmp;
                _size--;
        }
 
        void pop_back() {
                sllnode<_Tp> *tmp = tail;
-               tail = tail->next;
-               tail->next = NULL;
+               tail = tail->prev;
+               if (tail == NULL)
+                       head = NULL;
+               else
+                       tail->next = NULL;
                delete tmp;
                _size--;
        }
@@ -234,7 +273,7 @@ public:
                _size=0;
        }
 
-       void insertAfter(sllnode<_Tp> * node, _Tp val) {
+       sllnode<_Tp> * insertAfter(sllnode<_Tp> * node, _Tp val) {
                sllnode<_Tp> *tmp = new sllnode<_Tp>();
                tmp->val = val;
                tmp->prev = node;
@@ -246,6 +285,7 @@ public:
                        tmp->next->prev = tmp;
                }
                _size++;
+               return tmp;
        }
 
        void insertBefore(sllnode<_Tp> * node, _Tp val) {
@@ -272,7 +312,7 @@ public:
                if (tail == node) {
                        tail = node->prev;
                } else {
-                       tail->next->prev = node->prev;
+                       node->next->prev = node->prev;
                }
 
                sllnode<_Tp> *next = node->next;
@@ -334,7 +374,7 @@ public:
        }
 
        type back() const {
-               return array[size - 1];
+               return array[_size - 1];
        }
 
        void resize(uint psize) {
@@ -358,11 +398,11 @@ public:
                array[_size++] = item;
        }
 
-       type operator[](uint index) const {
+       type operator[](int index) const {
                return array[index];
        }
 
-       type & operator[](uint index) {
+       type & operator[](int index) {
                return array[index];
        }
 
@@ -463,11 +503,11 @@ public:
                array[_size++] = item;
        }
 
-       type & operator[](uint index) {
+       type operator[](int index) const {
                return array[index];
        }
 
-       type operator[](uint index) const {
+       type & operator[](int index) {
                return array[index];
        }