Files
hello-algo/ru/codes/cpp/chapter_searching/linear_search.cpp
Yudong Jin 71a773df64 fix(ru): align proofreading corrections with the Simplified Chinese original (#2004)
* docs(ru): correct proofreading issues in prose and code examples

* docs(ru): align proofreading edits with Simplified Chinese source

* fix(ru): repair generated Zig statements and complete comment translations
2026-10-27 18:19:11 +08:00

50 lines
1.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* File: linear_search.cpp
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* Линейный поиск (массив) */
int linearSearchArray(vector<int> &nums, int target) {
// Обход массива
for (int i = 0; i < nums.size(); i++) {
// Целевой элемент найден, вернуть его индекс
if (nums[i] == target)
return i;
}
// Целевой элемент не найден, вернуть -1
return -1;
}
/* Линейный поиск (связный список) */
ListNode *linearSearchLinkedList(ListNode *head, int target) {
// Обойти связный список
while (head != nullptr) {
// Целевой узел найден, возвращается этот узел
if (head->val == target)
return head;
head = head->next;
}
// Целевой узел не найден, вернуть nullptr
return nullptr;
}
/* Driver Code */
int main() {
int target = 3;
/* Выполнить линейный поиск в массиве */
vector<int> nums = {1, 5, 3, 2, 4, 7, 5, 9, 10, 8};
int index = linearSearchArray(nums, target);
cout << "Индекс целевого элемента 3 = " << index << endl;
/* Выполнить линейный поиск в связном списке */
ListNode *head = vecToLinkedList(nums);
ListNode *node = linearSearchLinkedList(head, target);
cout << "Объект узла со значением 3 = " << node << endl;
return 0;
}