Browse Source

添加析构函数

LinTianxiang 10 months ago
parent
commit
177f8f52b7
4 changed files with 37 additions and 10 deletions
  1. 9 0
      list.cpp
  2. 4 4
      list.h
  3. 18 4
      tire.cpp
  4. 6 2
      tire.h

+ 9 - 0
list.cpp

@@ -35,6 +35,15 @@ TEMPLATE(bool) ITERATOR::operator!= (const ITERATOR& it) {
 
 // #pragma region List<Type>
 TEMPLATE(HOLDER) ADT::List() : dummy(&dummy, &dummy) {};
+TEMPLATE(HOLDER) ADT::~List() {
+	Node* curr = head->_next;
+	Node* next = nullptr;
+	for (; curr != &dummy;) {
+		next = curr->_next;
+		delete curr;
+		curr = next;
+	}
+}
 
 TEMPLATE(typename ITERATOR) ADT::begin() { return ADT::iterator(head->_next); }
 

+ 4 - 4
list.h

@@ -9,7 +9,8 @@ public:
 		Node* _prev, * _next;
 		Node() : _prev(nullptr), _next(nullptr) { _value = nullptr; }
 		Node(Node* prev, Node* next) : _prev(prev), _next(next) { _value = nullptr; }
-		Node(Type& value, Node* prev = nullptr, Node* next = nullptr) : _prev(prev), _next(next) { _value = new Type(value); }
+		Node(Type& value, Node* prev = nullptr, Node* next = nullptr) : _prev(prev), _next(next) { _value = &value; }
+		~Node() { _prev = _next = nullptr, _value = nullptr; }
 		Type& operator*() { return *_value; }
 	};
 
@@ -24,9 +25,7 @@ public:
 		bool operator== (const iterator& it);
 		bool operator!= (const iterator& it);
 		Type& operator*() { return **_ptr; }
-		Node* operator&() {
-			return _ptr;
-		}
+		Node* operator&() { return _ptr; }
 	};
 
 private:
@@ -35,6 +34,7 @@ private:
 	Node* tail = &dummy;
 public:
 	List();
+	~List();
 public:
 	iterator& operator[] (int index);
 	List& operator+= (Type& value);

+ 18 - 4
tire.cpp

@@ -13,8 +13,14 @@ TEMPLATE(HOLDER) NODE::Node() {
 
 }
 
-TEMPLATE(HOLDER) NODE::Node(Type& value) {
-	_value = typename Container::Node(value);
+TEMPLATE(HOLDER) NODE::~Node() {
+	for (int i = 0; i < TIRE_NODE_NUMBER; ++i) {
+		if (_next[i] != nullptr) {
+			delete _next[i];
+			_next[i] = nullptr;
+		}
+	}
+	_value = nullptr;
 }
 // #pragma endregion
 
@@ -23,11 +29,19 @@ TEMPLATE(HOLDER) ADT::Tire() {
 
 }
 
+TEMPLATE(HOLDER) ADT::~Tire() {
+	for (int i = 0; i < TIRE_NODE_NUMBER; ++i) {
+		delete _root._next[i];
+		_root._next[i] = nullptr;
+	}
+	_root._value = nullptr;
+}
+
 TEMPLATE(typename ITERATOR) ADT::find(const std::string& key) {
 	Node* curr = &_root;
 	Node* next = nullptr;
 	for (const char& ch : key) {
-		next = curr->_next[(const int)ch];
+		next = curr->_next[INDEX(ch)];
 		if (next == nullptr) {
 			return end();
 		}
@@ -44,7 +58,7 @@ TEMPLATE(typename ITERATOR) ADT::insert(const std::string& key, Type& value) {
 	Node* curr = &_root;
 	Node* next = nullptr;
 	for (const char& ch : key) {
-		next = curr->_next[(const int)ch];
+		next = curr->_next[INDEX(ch)];
 		if (next != nullptr) {
 			curr = next;
 			continue;

+ 6 - 2
tire.h

@@ -5,15 +5,18 @@
 #include "list.cpp"
 #include <string>
 
+#define TIRE_NODE_NUMBER 128
+#define INDEX(x) ((const int)(x))
+
 template<typename Type,
 	typename Container = List<Type>>
 	class Tire {
 	public:
 		struct Node {
 			Node();
-			Node(Type& value);
+			~Node();
 			Node* operator[](const int ch);
-			Node* _next[256] { nullptr };
+			Node* _next[TIRE_NODE_NUMBER] { nullptr };
 			typename Container::Node* _value = nullptr;
 		};
 
@@ -24,6 +27,7 @@ template<typename Type,
 		Container _container;
 	public:
 		Tire();
+		~Tire();
 		Type& operator[](const std::string& key);
 		iterator find(const std::string& key);
 		iterator insert(const std::string& key, Type& value);