mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
fix: clang-tidy warnings
This commit is contained in:
parent
633cb2c05d
commit
f7695035f5
@ -18,9 +18,9 @@
|
|||||||
* completion of an iteration.
|
* completion of an iteration.
|
||||||
* Graphic Explanation:https://bit.ly/3nbVrFe
|
* Graphic Explanation:https://bit.ly/3nbVrFe
|
||||||
*/
|
*/
|
||||||
#include <iostream> /// for I/O operations
|
#include <cassert> /// for assert
|
||||||
#include <memory> /// for dynamic memory
|
#include <iostream> /// for I/O operations
|
||||||
#include <cassert> /// for assert
|
#include <memory> /// for dynamic memory
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @namespace data_structures
|
* @namespace data_structures
|
||||||
@ -35,11 +35,10 @@ namespace linked_list {
|
|||||||
/**
|
/**
|
||||||
* A Node class containing a value and pointer to another link
|
* A Node class containing a value and pointer to another link
|
||||||
*/
|
*/
|
||||||
class Node
|
class Node {
|
||||||
{
|
public:
|
||||||
public:
|
int val; /// value of the current link
|
||||||
int val; /// value of the current link
|
Node *next; /// pointer to the next value on the list
|
||||||
Node *next; /// pointer to the next value on the list
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -47,7 +46,7 @@ Node *next; /// pointer to the next value on the list
|
|||||||
*/
|
*/
|
||||||
class list {
|
class list {
|
||||||
private:
|
private:
|
||||||
Node *head{nullptr}; // link before the actual first element
|
Node *head = nullptr; // link before the actual first element
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* List constructor. Initializes the first link.
|
* List constructor. Initializes the first link.
|
||||||
@ -55,7 +54,7 @@ class list {
|
|||||||
list() {
|
list() {
|
||||||
// Initialize the first link
|
// Initialize the first link
|
||||||
}
|
}
|
||||||
|
bool isEmpty();
|
||||||
void insert(int32_t new_elem);
|
void insert(int32_t new_elem);
|
||||||
void reverseList();
|
void reverseList();
|
||||||
void display();
|
void display();
|
||||||
@ -63,18 +62,29 @@ class list {
|
|||||||
int last();
|
int last();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* function checks if list is empty
|
||||||
|
* @returns true if list is empty
|
||||||
|
* @returns false if list is not empty
|
||||||
|
*/
|
||||||
|
bool list::isEmpty() {
|
||||||
|
if (head == nullptr) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* function adds new element to the end of the list
|
* function adds new element to the end of the list
|
||||||
* @param new_elem to be added to the end of the list
|
* @param new_elem to be added to the end of the list
|
||||||
*/
|
*/
|
||||||
void list::insert(int32_t n) {
|
void list::insert(int32_t n) {
|
||||||
Node *new_node = new Node();
|
Node *new_node = new Node();
|
||||||
Node *temp = nullptr;
|
Node *temp = nullptr;
|
||||||
new_node->val = n;
|
new_node->val = n;
|
||||||
new_node->next = nullptr;
|
new_node->next = nullptr;
|
||||||
if (head == nullptr) {
|
if (isEmpty()) {
|
||||||
head = new_node;
|
head = new_node;
|
||||||
} else {
|
} else {
|
||||||
temp = head;
|
temp = head;
|
||||||
@ -86,11 +96,11 @@ void list::insert(int32_t n) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* function reverseList for reversing the list
|
* function reverseList for reversing the list
|
||||||
* @brief Using current,previous and next pointer.
|
* @brief Using current,previous and next pointer.
|
||||||
* @returns 'void'
|
* @returns 'void'
|
||||||
*/
|
*/
|
||||||
void list:: reverseList() {
|
void list::reverseList() {
|
||||||
Node *curr = head;
|
Node *curr = head;
|
||||||
Node *prev = nullptr, *next_node = nullptr;
|
Node *prev = nullptr, *next_node = nullptr;
|
||||||
while (curr != nullptr) {
|
while (curr != nullptr) {
|
||||||
@ -107,24 +117,30 @@ void list:: reverseList() {
|
|||||||
* @returns 'int n'
|
* @returns 'int n'
|
||||||
* @brief returns the first element in the list
|
* @brief returns the first element in the list
|
||||||
*/
|
*/
|
||||||
int list::top()
|
int list::top() {
|
||||||
{
|
if (!isEmpty()) {
|
||||||
int n=head->val;
|
int n = head->val;
|
||||||
return n;
|
return n;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* function to find the last element of the list
|
* function to find the last element of the list
|
||||||
* @returns 'int t->val'
|
* @returns 'int t->val'
|
||||||
* @brief returns the last element of the list
|
* @brief returns the last element of the list
|
||||||
*/
|
*/
|
||||||
int list::last()
|
int list::last() {
|
||||||
{
|
if (!isEmpty()) {
|
||||||
Node *t=head;
|
Node *t = head;
|
||||||
while(t->next!=nullptr)
|
while (t->next != nullptr) {
|
||||||
{
|
t = t->next;
|
||||||
t=t->next;
|
}
|
||||||
|
return t->val;
|
||||||
}
|
}
|
||||||
return t->val;
|
else return 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -132,15 +148,16 @@ int list::last()
|
|||||||
* @returns 'void'
|
* @returns 'void'
|
||||||
*/
|
*/
|
||||||
void list::display() {
|
void list::display() {
|
||||||
Node *node=head;
|
if (!isEmpty()) {
|
||||||
while (node != nullptr) {
|
Node *node = head;
|
||||||
std::cout << node->val << "\t";
|
while (node != nullptr) {
|
||||||
node = node->next;
|
std::cout << node->val << "\t";
|
||||||
|
node = node->next;
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
std::cout << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} // namespace linked_list
|
} // namespace linked_list
|
||||||
} // namespace data_structures
|
} // namespace data_structures
|
||||||
|
|
||||||
@ -150,8 +167,8 @@ void list::display() {
|
|||||||
*/
|
*/
|
||||||
static void test() {
|
static void test() {
|
||||||
data_structures::linked_list::list L;
|
data_structures::linked_list::list L;
|
||||||
// 1st test
|
// 1st test
|
||||||
|
|
||||||
L.insert(11);
|
L.insert(11);
|
||||||
L.insert(12);
|
L.insert(12);
|
||||||
L.insert(15);
|
L.insert(15);
|
||||||
@ -159,14 +176,13 @@ static void test() {
|
|||||||
L.insert(12);
|
L.insert(12);
|
||||||
L.insert(20);
|
L.insert(20);
|
||||||
L.insert(18);
|
L.insert(18);
|
||||||
assert(L.top()==11);
|
assert(L.top() == 11);
|
||||||
assert(L.last()==18);
|
assert(L.last() == 18);
|
||||||
L.reverseList();
|
L.reverseList();
|
||||||
//Reversal Testing
|
// Reversal Testing
|
||||||
assert(L.top()==18);
|
assert(L.top() == 18);
|
||||||
assert(L.last()==11);
|
assert(L.last() == 11);
|
||||||
std::cout << "Passed" << std::endl;
|
std::cout << "Passed" << std::endl;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -174,6 +190,6 @@ static void test() {
|
|||||||
* @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;
|
||||||
}
|
}
|
||||||
|
190
data_structures/tempCodeRunnerFile.cpp
Normal file
190
data_structures/tempCodeRunnerFile.cpp
Normal file
@ -0,0 +1,190 @@
|
|||||||
|
/**
|
||||||
|
* @file
|
||||||
|
* @brief Implementation of reversing a single linked list algorithm.
|
||||||
|
* @details
|
||||||
|
* The linked list is a data structure used for holding a sequence of
|
||||||
|
* values, which can be added, displayed,reversed or removed.
|
||||||
|
* ### Algorithm
|
||||||
|
* Values can be added by iterating to the end of a list(by following
|
||||||
|
* the pointers) starting from the first link. Whichever link points to null
|
||||||
|
* is considered the last link and is pointed to the new value.
|
||||||
|
*
|
||||||
|
* Linked List can be reversed by using 3 pointers: current, previous, and
|
||||||
|
* next_node; we keep iterating until the last node. Meanwhile, before changing
|
||||||
|
* to the next of current, we store it in the next_node pointer, now we store
|
||||||
|
* the prev pointer in the current of next, this is where the actual reversal
|
||||||
|
* happens. And then we move the prev and current pointers one step forward.
|
||||||
|
* Then the head node is made to point to the last node (prev pointer) after
|
||||||
|
* completion of an iteration.
|
||||||
|
* Graphic Explanation:https://bit.ly/3nbVrFe
|
||||||
|
*/
|
||||||
|
#include <cassert> /// for assert
|
||||||
|
#include <iostream> /// for I/O operations
|
||||||
|
#include <memory> /// for dynamic memory
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @namespace data_structures
|
||||||
|
* @brief Data Structures algorithms
|
||||||
|
*/
|
||||||
|
namespace data_structures {
|
||||||
|
/**
|
||||||
|
* @namespace linked_list
|
||||||
|
* @brief Functions for singly linked list algorithm
|
||||||
|
*/
|
||||||
|
namespace linked_list {
|
||||||
|
/**
|
||||||
|
* A Node class containing a value and pointer to another link
|
||||||
|
*/
|
||||||
|
class Node {
|
||||||
|
public:
|
||||||
|
int val; /// value of the current link
|
||||||
|
Node *next; /// pointer to the next value on the list
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A list class containing a sequence of links
|
||||||
|
*/
|
||||||
|
class list {
|
||||||
|
private:
|
||||||
|
Node *head = nullptr; // link before the actual first element
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* List constructor. Initializes the first link.
|
||||||
|
*/
|
||||||
|
list() {
|
||||||
|
// Initialize the first link
|
||||||
|
}
|
||||||
|
bool isEmpty();
|
||||||
|
void insert(int32_t new_elem);
|
||||||
|
void reverseList();
|
||||||
|
void display();
|
||||||
|
int top();
|
||||||
|
int last();
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* function checks if list is empty
|
||||||
|
* @returns true if list is empty
|
||||||
|
* @returns false if list is not empty
|
||||||
|
*/
|
||||||
|
bool list::isEmpty() {
|
||||||
|
if (head == nullptr) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* function adds new element to the end of the list
|
||||||
|
* @param new_elem to be added to the end of the list
|
||||||
|
*/
|
||||||
|
void list::insert(int32_t n) {
|
||||||
|
Node *new_node = new Node();
|
||||||
|
Node *temp = nullptr;
|
||||||
|
new_node->val = n;
|
||||||
|
new_node->next = nullptr;
|
||||||
|
if (isEmpty()) {
|
||||||
|
head = new_node;
|
||||||
|
} else {
|
||||||
|
temp = head;
|
||||||
|
while (temp->next != nullptr) {
|
||||||
|
temp = temp->next;
|
||||||
|
}
|
||||||
|
temp->next = new_node;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* function reverseList for reversing the list
|
||||||
|
* @brief Using current,previous and next pointer.
|
||||||
|
* @returns 'void'
|
||||||
|
*/
|
||||||
|
void list::reverseList() {
|
||||||
|
Node *curr = head;
|
||||||
|
Node *prev = nullptr, *next_node = nullptr;
|
||||||
|
while (curr != nullptr) {
|
||||||
|
next_node = curr->next;
|
||||||
|
curr->next = prev;
|
||||||
|
prev = curr;
|
||||||
|
curr = next_node;
|
||||||
|
}
|
||||||
|
head = prev;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* function to find the top element of the list
|
||||||
|
* @returns 'int n'
|
||||||
|
* @brief returns the first element in the list
|
||||||
|
*/
|
||||||
|
int list::top() {
|
||||||
|
if (!isEmpty()) {
|
||||||
|
int n = head->val;
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* function to find the last element of the list
|
||||||
|
* @returns 'int t->val'
|
||||||
|
* @brief returns the last element of the list
|
||||||
|
*/
|
||||||
|
int list::last() {
|
||||||
|
if (!isEmpty()) {
|
||||||
|
Node *t = head;
|
||||||
|
while (t->next != nullptr) {
|
||||||
|
t = t->next;
|
||||||
|
}
|
||||||
|
return t->val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* function displays all the elements in the list
|
||||||
|
* @returns 'void'
|
||||||
|
*/
|
||||||
|
void list::display() {
|
||||||
|
if (!isEmpty()) {
|
||||||
|
Node *node = head;
|
||||||
|
while (node != nullptr) {
|
||||||
|
std::cout << node->val << "\t";
|
||||||
|
node = node->next;
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace linked_list
|
||||||
|
} // namespace data_structures
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Self-test implementations
|
||||||
|
* @returns void
|
||||||
|
*/
|
||||||
|
static void test() {
|
||||||
|
data_structures::linked_list::list L;
|
||||||
|
// 1st test
|
||||||
|
|
||||||
|
L.insert(11);
|
||||||
|
L.insert(12);
|
||||||
|
L.insert(15);
|
||||||
|
L.insert(10);
|
||||||
|
L.insert(12);
|
||||||
|
L.insert(20);
|
||||||
|
L.insert(18);
|
||||||
|
assert(L.top() == 11);
|
||||||
|
assert(L.last() == 18);
|
||||||
|
L.reverseList();
|
||||||
|
// Reversal Testing
|
||||||
|
assert(L.top() == 18);
|
||||||
|
assert(L.last() == 11);
|
||||||
|
std::cout << "Passed" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Main function
|
||||||
|
* @returns 0 on exit
|
||||||
|
*/
|
||||||
|
int main() {
|
||||||
|
test(); // run self-test implementations
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user