mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
fix: clang-tidy fixes applied
This commit is contained in:
parent
102937d1d0
commit
5ef67bfe44
@ -2,11 +2,15 @@
|
|||||||
* @file
|
* @file
|
||||||
* @brief Given a linked list L[0,....,n] of n numbers, find the middle node.
|
* @brief Given a linked list L[0,....,n] of n numbers, find the middle node.
|
||||||
*
|
*
|
||||||
* @details The technique utilized in this implementation is the ["Floyd's tortoise and hare"](https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_tortoise_and_hare) approach.
|
* @details The technique utilized in this implementation is the ["Floyd's
|
||||||
* This technique uses two pointers that iterate through the list at different 'speeds' in order to solve problems.
|
* tortoise and
|
||||||
* In this implementation, for every iteration the slow pointer advances one node while the fast pointer advances two nodes.
|
* hare"](https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_tortoise_and_hare)
|
||||||
* The result of this is that since the fast pointer moves twice as fast as the slow pointer, when the fast pointer reaches the end of the list
|
* approach. This technique uses two pointers that iterate through the list at
|
||||||
* the slow pointer will be pointing to the middle node of the list.
|
* different 'speeds' in order to solve problems. In this implementation, for
|
||||||
|
* every iteration the slow pointer advances one node while the fast pointer
|
||||||
|
* advances two nodes. The result of this is that since the fast pointer moves
|
||||||
|
* twice as fast as the slow pointer, when the fast pointer reaches the end of
|
||||||
|
* the list the slow pointer will be pointing to the middle node of the list.
|
||||||
*
|
*
|
||||||
* Here are some example lists you can use to see how the algorithm works
|
* Here are some example lists you can use to see how the algorithm works
|
||||||
* A = [1,2,3,4,5]
|
* A = [1,2,3,4,5]
|
||||||
@ -17,19 +21,24 @@
|
|||||||
* @author [Benjamin Weiss](https://github.com/weiss-ben)
|
* @author [Benjamin Weiss](https://github.com/weiss-ben)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <iostream> /// for IO operations
|
|
||||||
#include <cassert> /// for assert
|
#include <cassert> /// for assert
|
||||||
|
#include <iostream> /// for IO operations
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Definition for singly-linked list.
|
* Definition for singly-linked list.
|
||||||
*/
|
*/
|
||||||
struct ListNode {
|
struct ListNode {
|
||||||
int val; ///< the value stored in the node
|
int val; ///< the value stored in the node
|
||||||
ListNode *next; ///< pointer to the next node
|
ListNode* next; ///< pointer to the next node
|
||||||
ListNode() : val(0), next(nullptr) {} ///< default constructor
|
ListNode() : val(0), next(nullptr) {} ///< default constructor
|
||||||
ListNode(int x) : val(x), next(nullptr) {} ///< constructor with value for node->val provided
|
ListNode(int x)
|
||||||
ListNode(int x, ListNode *next) : val(x), next(next) {} ///< constructor with values provided for node->val and node->next
|
: val(x),
|
||||||
};
|
next(nullptr) {} ///< constructor with value for node->val provided
|
||||||
|
ListNode(int x, ListNode* next)
|
||||||
|
: val(x),
|
||||||
|
next(next) {
|
||||||
|
} ///< constructor with values provided for node->val and node->next
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @namespace search
|
* @namespace search
|
||||||
@ -38,29 +47,29 @@ struct ListNode {
|
|||||||
namespace search {
|
namespace search {
|
||||||
/**
|
/**
|
||||||
* @namespace median_search
|
* @namespace median_search
|
||||||
* @brief Functions for the Median Search algorithm implementation. Wkipedia link to algorithm: https://en.wikipedia.org/wiki/Median_search
|
* @brief Functions for the Median Search algorithm implementation. Wkipedia
|
||||||
|
* link to algorithm: https://en.wikipedia.org/wiki/Median_search
|
||||||
*/
|
*/
|
||||||
namespace median_search2 {
|
namespace median_search2 {
|
||||||
/**
|
/**
|
||||||
* This function searches for the median of a linked list.
|
* This function searches for the median of a linked list.
|
||||||
* @param head The head of the linked list.
|
* @param head The head of the linked list.
|
||||||
* @returns Median node of the linked list.
|
* @returns Median node of the linked list.
|
||||||
*/
|
*/
|
||||||
ListNode* middleNode(ListNode* head) {
|
ListNode* middleNode(ListNode* head) {
|
||||||
if(!head)
|
if (!head)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
//Fast and slow pointers
|
// Fast and slow pointers
|
||||||
ListNode* fastptr;
|
ListNode* fastptr;
|
||||||
ListNode* slowptr = fastptr = head;
|
ListNode* slowptr = fastptr = head;
|
||||||
|
|
||||||
// fast jumps 2 while slow jumps 1
|
// fast jumps 2 while slow jumps 1
|
||||||
while(fastptr->next && fastptr->next->next) {
|
while (fastptr->next && fastptr->next->next) {
|
||||||
slowptr = slowptr->next;
|
slowptr = slowptr->next;
|
||||||
fastptr = fastptr->next->next;
|
fastptr = fastptr->next->next;
|
||||||
}
|
}
|
||||||
return (fastptr->next) ? slowptr->next : slowptr;
|
return (fastptr->next) ? slowptr->next : slowptr;
|
||||||
|
|
||||||
}
|
}
|
||||||
} // namespace median_search2
|
} // namespace median_search2
|
||||||
} // namespace search
|
} // namespace search
|
||||||
@ -74,9 +83,8 @@ static void test() {
|
|||||||
head->val = 1;
|
head->val = 1;
|
||||||
|
|
||||||
ListNode* temp1 = head;
|
ListNode* temp1 = head;
|
||||||
for(int i = 1; i < 6; ++i)
|
for (int i = 1; i < 6; ++i) {
|
||||||
{
|
auto temp2 = new ListNode;
|
||||||
ListNode* temp2 = new ListNode;
|
|
||||||
temp2->val = i;
|
temp2->val = i;
|
||||||
|
|
||||||
temp1->next = temp2;
|
temp1->next = temp2;
|
||||||
@ -88,8 +96,7 @@ static void test() {
|
|||||||
std::cout << "test case:1 passed\n";
|
std::cout << "test case:1 passed\n";
|
||||||
|
|
||||||
// Clean up
|
// Clean up
|
||||||
while(head)
|
while (head) {
|
||||||
{
|
|
||||||
ListNode* t = head->next;
|
ListNode* t = head->next;
|
||||||
delete head;
|
delete head;
|
||||||
}
|
}
|
||||||
@ -98,8 +105,7 @@ static void test() {
|
|||||||
head->val = 1;
|
head->val = 1;
|
||||||
|
|
||||||
temp1 = head;
|
temp1 = head;
|
||||||
for(int i = 1; i < 7; ++i)
|
for (int i = 1; i < 7; ++i) {
|
||||||
{
|
|
||||||
ListNode* temp2 = new ListNode;
|
ListNode* temp2 = new ListNode;
|
||||||
temp2->val = i;
|
temp2->val = i;
|
||||||
|
|
||||||
@ -112,10 +118,10 @@ static void test() {
|
|||||||
std::cout << "test case:1 passed\n";
|
std::cout << "test case:1 passed\n";
|
||||||
|
|
||||||
// Clean up
|
// Clean up
|
||||||
while(head)
|
while (head) {
|
||||||
{
|
ListNode* t = head;
|
||||||
ListNode* t = head->next;
|
head = head->next;
|
||||||
delete head;
|
delete t;
|
||||||
}
|
}
|
||||||
std::cout << "test case:2 passed\n";
|
std::cout << "test case:2 passed\n";
|
||||||
std::cout << "--All tests passed--\n";
|
std::cout << "--All tests passed--\n";
|
||||||
@ -125,8 +131,7 @@ static void test() {
|
|||||||
* @brief Main function
|
* @brief Main function
|
||||||
* @returns 0 on exit
|
* @returns 0 on exit
|
||||||
*/
|
*/
|
||||||
int main()
|
int main() {
|
||||||
{
|
|
||||||
test(); // run self-test implementations
|
test(); // run self-test implementations
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user