Skip to content

Commit 8e60d12

Browse files
authored
Add the initial EN translation for C++ code (krahets#1346)
1 parent 9e4017b commit 8e60d12

File tree

111 files changed

+6993
-9
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+6993
-9
lines changed

en/codes/cpp/.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Ignore all
2+
*
3+
# Unignore all with extensions
4+
!*.*
5+
# Unignore all dirs
6+
!*/
7+
8+
*.dSYM/
9+
10+
build/

en/codes/cpp/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
project(hello_algo CXX)
3+
4+
set(CMAKE_CXX_STANDARD 11)
5+
6+
include_directories(./include)
7+
8+
add_subdirectory(chapter_computational_complexity)
9+
add_subdirectory(chapter_array_and_linkedlist)
10+
add_subdirectory(chapter_stack_and_queue)
11+
add_subdirectory(chapter_hashing)
12+
add_subdirectory(chapter_tree)
13+
add_subdirectory(chapter_heap)
14+
add_subdirectory(chapter_graph)
15+
add_subdirectory(chapter_searching)
16+
add_subdirectory(chapter_sorting)
17+
add_subdirectory(chapter_divide_and_conquer)
18+
add_subdirectory(chapter_backtracking)
19+
add_subdirectory(chapter_dynamic_programming)
20+
add_subdirectory(chapter_greedy)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
add_executable(array array.cpp)
2+
add_executable(linked_list linked_list.cpp)
3+
add_executable(list list.cpp)
4+
add_executable(my_list my_list.cpp)
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* File: array.cpp
3+
* Created Time: 2022-11-25
4+
* Author: krahets ([email protected])
5+
*/
6+
7+
#include "../utils/common.hpp"
8+
9+
/* Random access to elements */
10+
int randomAccess(int *nums, int size) {
11+
// Randomly select a number in the range [0, size)
12+
int randomIndex = rand() % size;
13+
// Retrieve and return a random element
14+
int randomNum = nums[randomIndex];
15+
return randomNum;
16+
}
17+
18+
/* Extend array length */
19+
int *extend(int *nums, int size, int enlarge) {
20+
// Initialize an extended length array
21+
int *res = new int[size + enlarge];
22+
// Copy all elements from the original array to the new array
23+
for (int i = 0; i < size; i++) {
24+
res[i] = nums[i];
25+
}
26+
// Free memory
27+
delete[] nums;
28+
// Return the new array after expansion
29+
return res;
30+
}
31+
32+
/* Insert element num at `index` */
33+
void insert(int *nums, int size, int num, int index) {
34+
// Move all elements after `index` one position backward
35+
for (int i = size - 1; i > index; i--) {
36+
nums[i] = nums[i - 1];
37+
}
38+
// Assign num to the element at index
39+
nums[index] = num;
40+
}
41+
42+
/* Remove the element at `index` */
43+
void remove(int *nums, int size, int index) {
44+
// Move all elements after `index` one position forward
45+
for (int i = index; i < size - 1; i++) {
46+
nums[i] = nums[i + 1];
47+
}
48+
}
49+
50+
/* Traverse array */
51+
void traverse(int *nums, int size) {
52+
int count = 0;
53+
// Traverse array by index
54+
for (int i = 0; i < size; i++) {
55+
count += nums[i];
56+
}
57+
}
58+
59+
/* Search for a specified element in the array */
60+
int find(int *nums, int size, int target) {
61+
for (int i = 0; i < size; i++) {
62+
if (nums[i] == target)
63+
return i;
64+
}
65+
return -1;
66+
}
67+
68+
/* Driver Code */
69+
int main() {
70+
/* Initialize an array */
71+
int size = 5;
72+
int *arr = new int[size];
73+
cout << "Array arr = ";
74+
printArray(arr, size);
75+
76+
int *nums = new int[size]{1, 3, 2, 5, 4};
77+
cout << "Array nums = ";
78+
printArray(nums, size);
79+
80+
/* Random access */
81+
int randomNum = randomAccess(nums, size);
82+
cout << "Get a random element from nums = " << randomNum << endl;
83+
84+
/* Length extension */
85+
int enlarge = 3;
86+
nums = extend(nums, size, enlarge);
87+
size += enlarge;
88+
cout << "Extend the array length to 8, resulting in nums = ";
89+
printArray(nums, size);
90+
91+
/* Insert element */
92+
insert(nums, size, 6, 3);
93+
cout << "Insert the number 6 at index 3, resulting in nums = ";
94+
printArray(nums, size);
95+
96+
/* Remove element */
97+
remove(nums, size, 2);
98+
cout << "Remove the element at index 2, resulting in nums = ";
99+
printArray(nums, size);
100+
101+
/* Traverse array */
102+
traverse(nums, size);
103+
104+
/* Search for elements */
105+
int index = find(nums, size, 3);
106+
cout << "Find element 3 in nums, index = " << index << endl;
107+
108+
// Free memory
109+
delete[] arr;
110+
delete[] nums;
111+
112+
return 0;
113+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* File: linked_list.cpp
3+
* Created Time: 2022-11-25
4+
* Author: krahets ([email protected])
5+
*/
6+
7+
#include "../utils/common.hpp"
8+
9+
/* Insert node P after node n0 in the linked list */
10+
void insert(ListNode *n0, ListNode *P) {
11+
ListNode *n1 = n0->next;
12+
P->next = n1;
13+
n0->next = P;
14+
}
15+
16+
/* Remove the first node after node n0 in the linked list */
17+
void remove(ListNode *n0) {
18+
if (n0->next == nullptr)
19+
return;
20+
// n0 -> P -> n1
21+
ListNode *P = n0->next;
22+
ListNode *n1 = P->next;
23+
n0->next = n1;
24+
// Free memory
25+
delete P;
26+
}
27+
28+
/* Access the node at `index` in the linked list */
29+
ListNode *access(ListNode *head, int index) {
30+
for (int i = 0; i < index; i++) {
31+
if (head == nullptr)
32+
return nullptr;
33+
head = head->next;
34+
}
35+
return head;
36+
}
37+
38+
/* Search for the first node with value target in the linked list */
39+
int find(ListNode *head, int target) {
40+
int index = 0;
41+
while (head != nullptr) {
42+
if (head->val == target)
43+
return index;
44+
head = head->next;
45+
index++;
46+
}
47+
return -1;
48+
}
49+
50+
/* Driver Code */
51+
int main() {
52+
/* Initialize linked list */
53+
// Initialize each node
54+
ListNode *n0 = new ListNode(1);
55+
ListNode *n1 = new ListNode(3);
56+
ListNode *n2 = new ListNode(2);
57+
ListNode *n3 = new ListNode(5);
58+
ListNode *n4 = new ListNode(4);
59+
// Build references between nodes
60+
n0->next = n1;
61+
n1->next = n2;
62+
n2->next = n3;
63+
n3->next = n4;
64+
cout << "The initialized linked list is" << endl;
65+
printLinkedList(n0);
66+
67+
/* Insert node */
68+
insert(n0, new ListNode(0));
69+
cout << "Linked list after inserting the node is" << endl;
70+
printLinkedList(n0);
71+
72+
/* Remove node */
73+
remove(n0);
74+
cout << "Linked list after removing the node is" << endl;
75+
printLinkedList(n0);
76+
77+
/* Access node */
78+
ListNode *node = access(n0, 3);
79+
cout << "The value of the node at index 3 in the linked list = " << node->val << endl;
80+
81+
/* Search node */
82+
int index = find(n0, 2);
83+
cout << "The index of the node with value 2 in the linked list = " << index << endl;
84+
85+
// Free memory
86+
freeMemoryLinkedList(n0);
87+
88+
return 0;
89+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* File: list.cpp
3+
* Created Time: 2022-11-25
4+
* Author: krahets ([email protected])
5+
*/
6+
7+
#include "../utils/common.hpp"
8+
9+
/* Driver Code */
10+
int main() {
11+
/* Initialize list */
12+
vector<int> nums = {1, 3, 2, 5, 4};
13+
cout << "List nums = ";
14+
printVector(nums);
15+
16+
/* Access element */
17+
int num = nums[1];
18+
cout << "Access the element at index 1, obtained num = " << num << endl;
19+
20+
/* Update element */
21+
nums[1] = 0;
22+
cout << "Update the element at index 1 to 0, resulting in nums = ";
23+
printVector(nums);
24+
25+
/* Clear list */
26+
nums.clear();
27+
cout << "After clearing the list, nums = ";
28+
printVector(nums);
29+
30+
/* Add element at the end */
31+
nums.push_back(1);
32+
nums.push_back(3);
33+
nums.push_back(2);
34+
nums.push_back(5);
35+
nums.push_back(4);
36+
cout << "After adding elements, nums = ";
37+
printVector(nums);
38+
39+
/* Insert element in the middle */
40+
nums.insert(nums.begin() + 3, 6);
41+
cout << "Insert the number 6 at index 3, resulting in nums = ";
42+
printVector(nums);
43+
44+
/* Remove element */
45+
nums.erase(nums.begin() + 3);
46+
cout << "Remove the element at index 3, resulting in nums = ";
47+
printVector(nums);
48+
49+
/* Traverse the list by index */
50+
int count = 0;
51+
for (int i = 0; i < nums.size(); i++) {
52+
count += nums[i];
53+
}
54+
/* Traverse the list elements */
55+
count = 0;
56+
for (int x : nums) {
57+
count += x;
58+
}
59+
60+
/* Concatenate two lists */
61+
vector<int> nums1 = {6, 8, 7, 10, 9};
62+
nums.insert(nums.end(), nums1.begin(), nums1.end());
63+
cout << "Concatenate list nums1 to nums, resulting in nums = ";
64+
printVector(nums);
65+
66+
/* Sort list */
67+
sort(nums.begin(), nums.end());
68+
cout << "After sorting the list, nums = ";
69+
printVector(nums);
70+
71+
return 0;
72+
}

0 commit comments

Comments
 (0)