Add destructors to the C++ codes.

This commit is contained in:
Yudong Jin
2023-01-14 19:52:11 +08:00
parent 87acfc91ab
commit bb657f9517
19 changed files with 121 additions and 24 deletions

View File

@ -23,6 +23,8 @@ int* extend(int* nums, int size, int enlarge) {
for (int i = 0; i < size; i++) {
res[i] = nums[i];
}
// 释放内存
delete[] nums;
// 返回扩展后的新数组
return res;
}
@ -82,10 +84,7 @@ int main() {
/* 长度扩展 */
int enlarge = 3;
int* res = extend(nums, size, enlarge);
int* temp = nums;
nums = res;
delete[] temp;
nums = extend(nums, size, enlarge);
size += enlarge;
cout << "将数组长度扩展至 8 ,得到 nums = ";
PrintUtil::printArray(nums, size);
@ -107,5 +106,9 @@ int main() {
int index = find(nums, size, 3);
cout << "在 nums 中查找元素 3 ,得到索引 = " << index << endl;
// 释放内存
delete[] arr;
delete[] nums;
return 0;
}

View File

@ -83,5 +83,8 @@ int main() {
int index = find(n0, 2);
cout << "链表中值为 2 的结点的索引 = " << index << endl;
// 释放内存
freeMemoryLinkedList(n0);
return 0;
}

View File

@ -20,6 +20,11 @@ public:
nums = new int[numsCapacity];
}
/* 析构函数 */
~MyList() {
delete[] nums;
}
/* 获取列表长度(即当前元素数量)*/
int size() {
return numsSize;
@ -90,14 +95,14 @@ public:
void extendCapacity() {
// 新建一个长度为 size * extendRatio 的数组,并将原数组拷贝到新数组
int newCapacity = capacity() * extendRatio;
int* extend = new int[newCapacity];
int* tmp = nums;
nums = new int[newCapacity];
// 将原数组中的所有元素复制到新数组
for (int i = 0; i < size(); i++) {
extend[i] = nums[i];
nums[i] = tmp[i];
}
int* temp = nums;
nums = extend;
delete[] temp;
// 释放内存
delete[] tmp;
numsCapacity = newCapacity;
}
@ -160,5 +165,8 @@ int main() {
PrintUtil::printVector(vec);
cout << "容量 = " << list->capacity() << " ,长度 = " << list->size() << endl;
// 释放内存
delete list;
return 0;
}

View File

@ -56,5 +56,9 @@ int main() {
cout << "方法二 res = ";
PrintUtil::printVector(res);
// 释放内存
delete slt1;
delete slt2;
return 0;
}

View File

@ -18,7 +18,7 @@ void constant(int n) {
const int a = 0;
int b = 0;
vector<int> nums(10000);
ListNode* node = new ListNode(0);
ListNode node(0);
// 循环中的变量占用 O(1) 空间
for (int i = 0; i < n; i++) {
int c = 0;
@ -34,9 +34,9 @@ void linear(int n) {
// 长度为 n 的数组占用 O(n) 空间
vector<int> nums(n);
// 长度为 n 的列表占用 O(n) 空间
vector<ListNode*> nodes;
vector<ListNode> nodes;
for (int i = 0; i < n; i++) {
nodes.push_back(new ListNode(i));
nodes.push_back(ListNode(i));
}
// 长度为 n 的哈希表占用 O(n) 空间
unordered_map<int, string> map;
@ -98,5 +98,8 @@ int main() {
TreeNode* root = buildTree(n);
PrintUtil::printTree(root);
// 释放内存
freeMemoryTree(root);
return 0;
}

View File

@ -21,6 +21,10 @@ public:
nums = new int[capacity];
}
~ArrayQueue() {
delete[] nums;
}
/* 获取队列的容量 */
int capacity() {
return cap;
@ -117,5 +121,8 @@ int main() {
PrintUtil::printVector(queue->toVector());
}
// 释放内存
delete queue;
return 0;
}

View File

@ -78,5 +78,8 @@ int main() {
bool empty = stack->empty();
cout << "栈是否为空 = " << empty << endl;
// 释放内存
delete stack;
return 0;
}

View File

@ -19,6 +19,11 @@ public:
queSize = 0;
}
~LinkedListQueue() {
delete front;
delete rear;
}
/* 获取队列的长度 */
int size() {
return queSize;
@ -108,5 +113,8 @@ int main() {
bool empty = queue->empty();
cout << "队列是否为空 = " << empty << endl;
// 释放内存
delete queue;
return 0;
}

View File

@ -18,6 +18,10 @@ public:
stkSize = 0;
}
~LinkedListStack() {
freeMemoryLinkedList(stackTop);
}
/* 获取栈的长度 */
int size() {
return stkSize;
@ -97,5 +101,8 @@ int main() {
bool empty = stack->empty();
cout << "栈是否为空 = " << empty << endl;
// 释放内存
delete stack;
return 0;
}

View File

@ -17,6 +17,10 @@ public:
root = buildTree(nums, 0, nums.size() - 1); // 构建二叉搜索树
}
~BinarySearchTree() {
freeMemoryTree(root);
}
/* 获取二叉树根结点 */
TreeNode* getRoot() {
return root;
@ -152,5 +156,8 @@ int main() {
cout << endl << "删除结点 4 后,二叉树为\n" << endl;
PrintUtil::printTree(bst->getRoot());
// 释放内存
delete bst;
return 0;
}

View File

@ -37,5 +37,8 @@ int main() {
cout << endl << "删除结点 P 后\n" << endl;
PrintUtil::printTree(n1);
// 释放内存
freeMemoryTree(n1);
return 0;
}

View File

@ -48,3 +48,18 @@ ListNode* getListNode(ListNode *head, int val) {
}
return head;
}
/**
* @brief Free the memory allocated to a linked list
*
* @param cur
*/
void freeMemoryLinkedList(ListNode *cur) {
// 释放内存
ListNode *pre;
while (cur != nullptr) {
pre = cur;
cur = cur->next;
delete pre;
}
}

View File

@ -68,3 +68,16 @@ TreeNode *getTreeNode(TreeNode *root, int val) {
TreeNode *right = getTreeNode(root->right, val);
return left != nullptr ? left : right;
}
/**
* @brief Free the memory allocated to a tree
*
* @param root
*/
void freeMemoryTree(TreeNode *root) {
if (root == nullptr) return;
freeMemoryTree(root->left);
freeMemoryTree(root->right);
// 释放内存
delete root;
}

View File

@ -245,6 +245,8 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
for (int i = 0; i < size; i++) {
res[i] = nums[i];
}
// 释放内存
delete[] nums;
// 返回扩展后的新数组
return res;
}

View File

@ -748,6 +748,11 @@ comments: true
nums = new int[numsCapacity];
}
/* 析构函数 */
~MyList() {
delete[] nums;
}
/* 获取列表长度(即当前元素数量)*/
int size() {
return numsSize;
@ -818,14 +823,14 @@ comments: true
void extendCapacity() {
// 新建一个长度为 size * extendRatio 的数组,并将原数组拷贝到新数组
int newCapacity = capacity() * extendRatio;
int* extend = new int[newCapacity];
int* tmp = nums;
nums = new int[newCapacity];
// 将原数组中的所有元素复制到新数组
for (int i = 0; i < size(); i++) {
extend[i] = nums[i];
nums[i] = tmp[i];
}
int* temp = nums;
nums = extend;
delete[] temp;
// 释放内存
delete[] tmp;
numsCapacity = newCapacity;
}
};

View File

@ -507,7 +507,7 @@ $$
const int a = 0;
int b = 0;
vector<int> nums(10000);
ListNode* node = new ListNode(0);
ListNode node(0);
// 循环中的变量占用 O(1) 空间
for (int i = 0; i < n; i++) {
int c = 0;
@ -654,9 +654,9 @@ $$
// 长度为 n 的数组占用 O(n) 空间
vector<int> nums(n);
// 长度为 n 的列表占用 O(n) 空间
vector<ListNode*> nodes;
vector<ListNode> nodes;
for (int i = 0; i < n; i++) {
nodes.push_back(new ListNode(i));
nodes.push_back(ListNode(i));
}
// 长度为 n 的哈希表占用 O(n) 空间
unordered_map<int, string> map;

View File

@ -330,6 +330,10 @@ comments: true
rear = nullptr;
queSize = 0;
}
~LinkedListQueue() {
delete front;
delete rear;
}
/* 获取队列的长度 */
int size() {
return queSize;
@ -784,6 +788,9 @@ comments: true
cap = capacity;
nums = new int[capacity];
}
~ArrayQueue() {
delete[] nums;
}
/* 获取队列的容量 */
int capacity() {
return cap;

View File

@ -324,6 +324,9 @@ comments: true
stackTop = nullptr;
stkSize = 0;
}
~LinkedListStack() {
freeMemoryLinkedList(stackTop);
}
/* 获取栈的长度 */
int size() {
return stkSize;

View File

@ -22,19 +22,15 @@ comments: true
-`cur.val = num` ,说明找到目标结点,跳出循环并返回该结点即可;
=== "Step 1"
![bst_search_1](binary_search_tree.assets/bst_search_1.png)
=== "Step 2"
![bst_search_2](binary_search_tree.assets/bst_search_2.png)
=== "Step 3"
![bst_search_3](binary_search_tree.assets/bst_search_3.png)
=== "Step 4"
![bst_search_4](binary_search_tree.assets/bst_search_4.png)
二叉搜索树的查找操作和二分查找算法如出一辙,也是在每轮排除一半情况。循环次数最多为二叉树的高度,当二叉树平衡时,使用 $O(\log n)$ 时间。