2024-02-21 09:31:34 +08:00
|
|
|
|
/**
|
|
|
|
|
* File: linked_list.cpp
|
|
|
|
|
* Created Time: 2022-11-25
|
|
|
|
|
* Author: krahets (krahets@163.com)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "../utils/common.hpp"
|
|
|
|
|
|
2024-02-21 10:10:16 +08:00
|
|
|
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľڵ<C4BD> n0 ֮<><D6AE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD> P */
|
2024-02-21 09:31:34 +08:00
|
|
|
|
void insert(ListNode *n0, ListNode *P) {
|
|
|
|
|
ListNode *n1 = n0->next;
|
|
|
|
|
P->next = n1;
|
|
|
|
|
n0->next = P;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-21 10:10:16 +08:00
|
|
|
|
/* ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľڵ<C4BD> n0 ֮<><D6AE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><D7B8>ڵ<EFBFBD> */
|
2024-02-21 09:31:34 +08:00
|
|
|
|
void remove(ListNode *n0) {
|
|
|
|
|
if (n0->next == nullptr)
|
|
|
|
|
return;
|
|
|
|
|
// n0 -> P -> n1
|
|
|
|
|
ListNode *P = n0->next;
|
|
|
|
|
ListNode *n1 = P->next;
|
|
|
|
|
n0->next = n1;
|
2024-02-21 10:10:16 +08:00
|
|
|
|
// <20>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
|
2024-02-21 09:31:34 +08:00
|
|
|
|
delete P;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-21 10:10:16 +08:00
|
|
|
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ index <20>Ľڵ<C4BD> */
|
2024-02-21 09:31:34 +08:00
|
|
|
|
ListNode *access(ListNode *head, int index) {
|
|
|
|
|
for (int i = 0; i < index; i++) {
|
|
|
|
|
if (head == nullptr)
|
|
|
|
|
return nullptr;
|
|
|
|
|
head = head->next;
|
|
|
|
|
}
|
|
|
|
|
return head;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-21 10:10:16 +08:00
|
|
|
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>в<EFBFBD><D0B2><EFBFBD>ֵΪ target <20><><EFBFBD><EFBFBD><D7B8>ڵ<EFBFBD> */
|
2024-02-21 09:31:34 +08:00
|
|
|
|
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() {
|
2024-02-21 10:10:16 +08:00
|
|
|
|
/* <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|
|
|
|
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>
|
2024-02-21 09:31:34 +08:00
|
|
|
|
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);
|
2024-02-21 10:10:16 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>֮<EFBFBD><D6AE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2024-02-21 09:31:34 +08:00
|
|
|
|
n0->next = n1;
|
|
|
|
|
n1->next = n2;
|
|
|
|
|
n2->next = n3;
|
|
|
|
|
n3->next = n4;
|
2024-02-21 10:10:16 +08:00
|
|
|
|
cout << "<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ" << endl;
|
2024-02-21 09:31:34 +08:00
|
|
|
|
printLinkedList(n0);
|
|
|
|
|
|
2024-02-21 10:10:16 +08:00
|
|
|
|
/* <20><><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD> */
|
2024-02-21 09:31:34 +08:00
|
|
|
|
insert(n0, new ListNode(0));
|
2024-02-21 10:10:16 +08:00
|
|
|
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ" << endl;
|
2024-02-21 09:31:34 +08:00
|
|
|
|
printLinkedList(n0);
|
|
|
|
|
|
2024-02-21 10:10:16 +08:00
|
|
|
|
/* ɾ<><C9BE><EFBFBD>ڵ<EFBFBD> */
|
2024-02-21 09:31:34 +08:00
|
|
|
|
remove(n0);
|
2024-02-21 10:10:16 +08:00
|
|
|
|
cout << "ɾ<EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ" << endl;
|
2024-02-21 09:31:34 +08:00
|
|
|
|
printLinkedList(n0);
|
|
|
|
|
|
2024-02-21 10:10:16 +08:00
|
|
|
|
/* <20><><EFBFBD>ʽڵ<CABD> */
|
2024-02-21 09:31:34 +08:00
|
|
|
|
ListNode *node = access(n0, 3);
|
2024-02-21 10:10:16 +08:00
|
|
|
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 3 <20><><EFBFBD>Ľڵ<C4BD><DAB5><EFBFBD>ֵ = " << node->val << endl;
|
2024-02-21 09:31:34 +08:00
|
|
|
|
|
2024-02-21 10:10:16 +08:00
|
|
|
|
/* <20><><EFBFBD>ҽڵ<D2BD> */
|
2024-02-21 09:31:34 +08:00
|
|
|
|
int index = find(n0, 2);
|
2024-02-21 10:10:16 +08:00
|
|
|
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ 2 <20>Ľڵ<C4BD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> = " << index << endl;
|
2024-02-21 09:31:34 +08:00
|
|
|
|
|
2024-02-21 10:10:16 +08:00
|
|
|
|
// <20>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
|
2024-02-21 09:31:34 +08:00
|
|
|
|
freeMemoryLinkedList(n0);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|