Add the initial EN translation for C++ code (#1346)

This commit is contained in:
Yudong Jin
2024-05-06 13:31:46 +08:00
committed by GitHub
parent 9e4017b3fb
commit 8e60d12151
111 changed files with 6993 additions and 9 deletions
@@ -0,0 +1,4 @@
add_executable(array array.cpp)
add_executable(linked_list linked_list.cpp)
add_executable(list list.cpp)
add_executable(my_list my_list.cpp)
@@ -0,0 +1,113 @@
/**
* File: array.cpp
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* Random access to elements */
int randomAccess(int *nums, int size) {
// Randomly select a number in the range [0, size)
int randomIndex = rand() % size;
// Retrieve and return a random element
int randomNum = nums[randomIndex];
return randomNum;
}
/* Extend array length */
int *extend(int *nums, int size, int enlarge) {
// Initialize an extended length array
int *res = new int[size + enlarge];
// Copy all elements from the original array to the new array
for (int i = 0; i < size; i++) {
res[i] = nums[i];
}
// Free memory
delete[] nums;
// Return the new array after expansion
return res;
}
/* Insert element num at `index` */
void insert(int *nums, int size, int num, int index) {
// Move all elements after `index` one position backward
for (int i = size - 1; i > index; i--) {
nums[i] = nums[i - 1];
}
// Assign num to the element at index
nums[index] = num;
}
/* Remove the element at `index` */
void remove(int *nums, int size, int index) {
// Move all elements after `index` one position forward
for (int i = index; i < size - 1; i++) {
nums[i] = nums[i + 1];
}
}
/* Traverse array */
void traverse(int *nums, int size) {
int count = 0;
// Traverse array by index
for (int i = 0; i < size; i++) {
count += nums[i];
}
}
/* Search for a specified element in the array */
int find(int *nums, int size, int target) {
for (int i = 0; i < size; i++) {
if (nums[i] == target)
return i;
}
return -1;
}
/* Driver Code */
int main() {
/* Initialize an array */
int size = 5;
int *arr = new int[size];
cout << "Array arr = ";
printArray(arr, size);
int *nums = new int[size]{1, 3, 2, 5, 4};
cout << "Array nums = ";
printArray(nums, size);
/* Random access */
int randomNum = randomAccess(nums, size);
cout << "Get a random element from nums = " << randomNum << endl;
/* Length extension */
int enlarge = 3;
nums = extend(nums, size, enlarge);
size += enlarge;
cout << "Extend the array length to 8, resulting in nums = ";
printArray(nums, size);
/* Insert element */
insert(nums, size, 6, 3);
cout << "Insert the number 6 at index 3, resulting in nums = ";
printArray(nums, size);
/* Remove element */
remove(nums, size, 2);
cout << "Remove the element at index 2, resulting in nums = ";
printArray(nums, size);
/* Traverse array */
traverse(nums, size);
/* Search for elements */
int index = find(nums, size, 3);
cout << "Find element 3 in nums, index = " << index << endl;
// Free memory
delete[] arr;
delete[] nums;
return 0;
}
@@ -0,0 +1,89 @@
/**
* File: linked_list.cpp
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* Insert node P after node n0 in the linked list */
void insert(ListNode *n0, ListNode *P) {
ListNode *n1 = n0->next;
P->next = n1;
n0->next = P;
}
/* Remove the first node after node n0 in the linked list */
void remove(ListNode *n0) {
if (n0->next == nullptr)
return;
// n0 -> P -> n1
ListNode *P = n0->next;
ListNode *n1 = P->next;
n0->next = n1;
// Free memory
delete P;
}
/* Access the node at `index` in the linked list */
ListNode *access(ListNode *head, int index) {
for (int i = 0; i < index; i++) {
if (head == nullptr)
return nullptr;
head = head->next;
}
return head;
}
/* Search for the first node with value target in the linked list */
int find(ListNode *head, int target) {
int index = 0;
while (head != nullptr) {
if (head->val == target)
return index;
head = head->next;
index++;
}
return -1;
}
/* Driver Code */
int main() {
/* Initialize linked list */
// Initialize each node
ListNode *n0 = new ListNode(1);
ListNode *n1 = new ListNode(3);
ListNode *n2 = new ListNode(2);
ListNode *n3 = new ListNode(5);
ListNode *n4 = new ListNode(4);
// Build references between nodes
n0->next = n1;
n1->next = n2;
n2->next = n3;
n3->next = n4;
cout << "The initialized linked list is" << endl;
printLinkedList(n0);
/* Insert node */
insert(n0, new ListNode(0));
cout << "Linked list after inserting the node is" << endl;
printLinkedList(n0);
/* Remove node */
remove(n0);
cout << "Linked list after removing the node is" << endl;
printLinkedList(n0);
/* Access node */
ListNode *node = access(n0, 3);
cout << "The value of the node at index 3 in the linked list = " << node->val << endl;
/* Search node */
int index = find(n0, 2);
cout << "The index of the node with value 2 in the linked list = " << index << endl;
// Free memory
freeMemoryLinkedList(n0);
return 0;
}
@@ -0,0 +1,72 @@
/**
* File: list.cpp
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* Driver Code */
int main() {
/* Initialize list */
vector<int> nums = {1, 3, 2, 5, 4};
cout << "List nums = ";
printVector(nums);
/* Access element */
int num = nums[1];
cout << "Access the element at index 1, obtained num = " << num << endl;
/* Update element */
nums[1] = 0;
cout << "Update the element at index 1 to 0, resulting in nums = ";
printVector(nums);
/* Clear list */
nums.clear();
cout << "After clearing the list, nums = ";
printVector(nums);
/* Add element at the end */
nums.push_back(1);
nums.push_back(3);
nums.push_back(2);
nums.push_back(5);
nums.push_back(4);
cout << "After adding elements, nums = ";
printVector(nums);
/* Insert element in the middle */
nums.insert(nums.begin() + 3, 6);
cout << "Insert the number 6 at index 3, resulting in nums = ";
printVector(nums);
/* Remove element */
nums.erase(nums.begin() + 3);
cout << "Remove the element at index 3, resulting in nums = ";
printVector(nums);
/* Traverse the list by index */
int count = 0;
for (int i = 0; i < nums.size(); i++) {
count += nums[i];
}
/* Traverse the list elements */
count = 0;
for (int x : nums) {
count += x;
}
/* Concatenate two lists */
vector<int> nums1 = {6, 8, 7, 10, 9};
nums.insert(nums.end(), nums1.begin(), nums1.end());
cout << "Concatenate list nums1 to nums, resulting in nums = ";
printVector(nums);
/* Sort list */
sort(nums.begin(), nums.end());
cout << "After sorting the list, nums = ";
printVector(nums);
return 0;
}
@@ -0,0 +1,171 @@
/**
* File: my_list.cpp
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* List class */
class MyList {
private:
int *arr; // Array (stores list elements)
int arrCapacity = 10; // List capacity
int arrSize = 0; // List length (current number of elements)
int extendRatio = 2; // Multiple for each list expansion
public:
/* Constructor */
MyList() {
arr = new int[arrCapacity];
}
/* Destructor */
~MyList() {
delete[] arr;
}
/* Get list length (current number of elements)*/
int size() {
return arrSize;
}
/* Get list capacity */
int capacity() {
return arrCapacity;
}
/* Access element */
int get(int index) {
// If the index is out of bounds, throw an exception, as below
if (index < 0 || index >= size())
throw out_of_range("Index out of bounds");
return arr[index];
}
/* Update element */
void set(int index, int num) {
if (index < 0 || index >= size())
throw out_of_range("Index out of bounds");
arr[index] = num;
}
/* Add element at the end */
void add(int num) {
// When the number of elements exceeds capacity, trigger the expansion mechanism
if (size() == capacity())
extendCapacity();
arr[size()] = num;
// Update the number of elements
arrSize++;
}
/* Insert element in the middle */
void insert(int index, int num) {
if (index < 0 || index >= size())
throw out_of_range("Index out of bounds");
// When the number of elements exceeds capacity, trigger the expansion mechanism
if (size() == capacity())
extendCapacity();
// Move all elements after `index` one position backward
for (int j = size() - 1; j >= index; j--) {
arr[j + 1] = arr[j];
}
arr[index] = num;
// Update the number of elements
arrSize++;
}
/* Remove element */
int remove(int index) {
if (index < 0 || index >= size())
throw out_of_range("Index out of bounds");
int num = arr[index];
// Move all elements after `index` one position forward
for (int j = index; j < size() - 1; j++) {
arr[j] = arr[j + 1];
}
// Update the number of elements
arrSize--;
// Return the removed element
return num;
}
/* Extend list */
void extendCapacity() {
// Create a new array with a length multiple of the original array by extendRatio
int newCapacity = capacity() * extendRatio;
int *tmp = arr;
arr = new int[newCapacity];
// Copy all elements from the original array to the new array
for (int i = 0; i < size(); i++) {
arr[i] = tmp[i];
}
// Free memory
delete[] tmp;
arrCapacity = newCapacity;
}
/* Convert the list to a Vector for printing */
vector<int> toVector() {
// Only convert elements within valid length range
vector<int> vec(size());
for (int i = 0; i < size(); i++) {
vec[i] = arr[i];
}
return vec;
}
};
/* Driver Code */
int main() {
/* Initialize list */
MyList *nums = new MyList();
/* Add element at the end */
nums->add(1);
nums->add(3);
nums->add(2);
nums->add(5);
nums->add(4);
cout << "List nums = ";
vector<int> vec = nums->toVector();
printVector(vec);
cout << "Capacity = " << nums->capacity() << ", length = " << nums->size() << endl;
/* Insert element in the middle */
nums->insert(3, 6);
cout << "Insert the number 6 at index 3, resulting in nums = ";
vec = nums->toVector();
printVector(vec);
/* Remove element */
nums->remove(3);
cout << "Remove the element at index 3, resulting in nums = ";
vec = nums->toVector();
printVector(vec);
/* Access element */
int num = nums->get(1);
cout << "Access the element at index 1, obtained num = " << num << endl;
/* Update element */
nums->set(1, 0);
cout << "Update the element at index 1 to 0, resulting in nums = ";
vec = nums->toVector();
printVector(vec);
/* Test expansion mechanism */
for (int i = 0; i < 10; i++) {
// At i = 5, the list length will exceed the list capacity, triggering the expansion mechanism at this time
nums->add(i);
}
cout << "After extending, list nums = ";
vec = nums->toVector();
printVector(vec);
cout << "Capacity = " << nums->capacity() << ", length = " << nums->size() << endl;
// Free memory
delete nums;
return 0;
}