cpp-algo-cases/chapter_array_and_linkedlist/linked_list.cpp

97 lines
2.1 KiB
C++
Raw Permalink Normal View History

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-22 14:56:29 +08:00
void insert(ListNode *n0, ListNode *P)
{
ListNode *n1 = n0->next; // n0 -> n1
P->next = n1; // P -> n1
n0->next = P; // n0 -> P
2024-02-21 09:31:34 +08:00
}
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-22 14:56:29 +08:00
void remove(ListNode *n0)
{
2024-02-21 09:31:34 +08:00
if (n0->next == nullptr)
2024-02-22 14:56:29 +08:00
return; // n0 ֮<><D6AE>û<EFBFBD>нڵ<D0BD>
2024-02-21 09:31:34 +08:00
// 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-22 14:56:29 +08:00
ListNode *access(ListNode *head, int index)
{
for (int i = 0; i < index; i++)
{
2024-02-21 09:31:34 +08:00
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-22 14:56:29 +08:00
int find(ListNode *head, int target)
{
2024-02-21 09:31:34 +08:00
int index = 0;
2024-02-22 14:56:29 +08:00
while (head != nullptr)
{
if (head->val == target) // <20>ҵ<EFBFBD><D2B5>ڵ<EFBFBD>
return index; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
head = head->next; // <20><><EFBFBD>½ڵ<C2BD>
index++; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-02-21 09:31:34 +08:00
}
2024-02-22 14:56:29 +08:00
return -1; // δ<>ҵ<EFBFBD><D2B5>ڵ<EFBFBD>, <20><><EFBFBD><EFBFBD> -1
2024-02-21 09:31:34 +08:00
}
/* Driver Code */
2024-02-22 14:56:29 +08:00
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-22 14:56:29 +08:00
ListNode *n0 = new ListNode(1); // <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ڵ<EFBFBD>, ֵΪ1
2024-02-21 09:31:34 +08:00
ListNode *n1 = new ListNode(3);
ListNode *n2 = new ListNode(2);
2024-02-22 14:56:29 +08:00
ListNode *n3 = new ListNode(5); // <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ڵ<EFBFBD>, ֵΪ5
2024-02-21 09:31:34 +08:00
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;
}