Add C++ codes for the chapter
array and linked list.
This commit is contained in:
@@ -30,7 +30,12 @@ comments: true
|
||||
=== "C++"
|
||||
|
||||
```cpp title=""
|
||||
|
||||
/* 链表结点结构体 */
|
||||
struct ListNode {
|
||||
int val; // 结点值
|
||||
ListNode *next; // 指向下一结点的指针(引用)
|
||||
ListNode(int x) : val(x), next(nullptr) {} // 构造函数
|
||||
};
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -71,7 +76,18 @@ comments: true
|
||||
=== "C++"
|
||||
|
||||
```cpp title=""
|
||||
|
||||
/* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */
|
||||
// 初始化各个结点
|
||||
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);
|
||||
// 构建引用指向
|
||||
n0->next = n1;
|
||||
n1->next = n2;
|
||||
n2->next = n3;
|
||||
n3->next = n4;
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -123,7 +139,22 @@ comments: true
|
||||
=== "C++"
|
||||
|
||||
```cpp title=""
|
||||
|
||||
/* 在链表的结点 n0 之后插入结点 P */
|
||||
void insert(ListNode* n0, ListNode* P) {
|
||||
ListNode* n1 = n0->next;
|
||||
n0->next = P;
|
||||
P->next = n1;
|
||||
}
|
||||
|
||||
/* 删除链表的结点 n0 之后的首个结点 */
|
||||
void remove(ListNode* n0) {
|
||||
if (n0->next == nullptr)
|
||||
return;
|
||||
// n0 -> P -> n1
|
||||
ListNode* P = n0->next;
|
||||
ListNode* n1 = P->next;
|
||||
n0->next = n1;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -166,7 +197,15 @@ comments: true
|
||||
=== "C++"
|
||||
|
||||
```cpp title=""
|
||||
|
||||
/* 访问链表中索引为 index 的结点 */
|
||||
ListNode* access(ListNode* head, int index) {
|
||||
for (int i = 0; i < index; i++) {
|
||||
head = head->next;
|
||||
if (head == nullptr)
|
||||
return nullptr;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -206,7 +245,17 @@ comments: true
|
||||
=== "C++"
|
||||
|
||||
```cpp title=""
|
||||
|
||||
/* 在链表中查找值为 target 的首个结点 */
|
||||
int find(ListNode* head, int target) {
|
||||
int index = 0;
|
||||
while (head != nullptr) {
|
||||
if (head->val == target)
|
||||
return index;
|
||||
head = head->next;
|
||||
index++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -246,7 +295,13 @@ comments: true
|
||||
=== "C++"
|
||||
|
||||
```cpp title=""
|
||||
|
||||
/* 链表结点结构体 */
|
||||
struct ListNode {
|
||||
int val; // 结点值
|
||||
ListNode *next; // 指向后继结点的指针(引用)
|
||||
ListNode *prev; // 指向前驱结点的指针(引用)
|
||||
ListNode(int x) : val(x), next(nullptr) {} // 构造函数
|
||||
};
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
|
Reference in New Issue
Block a user