mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
clang-format and clang-tidy fixes for 5cac47c9
This commit is contained in:
parent
fef4c55eab
commit
7cb21c9123
@ -9,22 +9,23 @@
|
|||||||
* the pointers) starting from the first link. Whichever link points to null
|
* the pointers) starting from the first link. Whichever link points to null
|
||||||
* is considered the last link and is pointed to the new value.
|
* is considered the last link and is pointed to the new value.
|
||||||
*
|
*
|
||||||
* Linked List can be reversed by using 3 pointers ,current ,prev and next_node ;
|
* Linked List can be reversed by using 3 pointers ,current ,prev and next_node
|
||||||
* We keep iterating till the last node,meanwhile before changing to the next of current ,we store it in the next_node pointer;
|
* ; We keep iterating till the last node,meanwhile before changing to the next
|
||||||
* now we store the prev pointer in the current of next ,this is where the actual reversal happens,
|
* 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.
|
* 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 iteration .
|
* Then the head node is made to point to the last node (prev pointer) after
|
||||||
|
* completion of iteration .
|
||||||
*/
|
*/
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A Node class containing a value and pointer to next node;
|
* A Node class containing a value and pointer to next node;
|
||||||
*/
|
*/
|
||||||
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
|
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* function adds new element to the end of the list
|
* function adds new element to the end of the list
|
||||||
@ -32,21 +33,18 @@ public:
|
|||||||
* double pointer to the head node of the list to
|
* double pointer to the head node of the list to
|
||||||
* reflect the changes globally in the whole program
|
* reflect the changes globally in the whole program
|
||||||
*/
|
*/
|
||||||
void create(Node **head, int n)
|
void create(Node **head, int n) {
|
||||||
{
|
|
||||||
Node *new_node = new Node();
|
Node *new_node = new Node();
|
||||||
Node *temp;
|
Node *temp = nullptr;
|
||||||
new_node->val = n;
|
new_node->val = n;
|
||||||
new_node->next = NULL;
|
new_node->next = nullptr;
|
||||||
if (*head == NULL)
|
if (*head == nullptr) {
|
||||||
{
|
|
||||||
*head = new_node;
|
*head = new_node;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
temp = *head;
|
temp = *head;
|
||||||
while (temp->next != NULL)
|
while (temp->next != nullptr) {
|
||||||
temp = temp->next;
|
temp = temp->next;
|
||||||
|
}
|
||||||
temp->next = new_node;
|
temp->next = new_node;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -55,10 +53,8 @@ void create(Node **head, int n)
|
|||||||
* @returns 'void'
|
* @returns 'void'
|
||||||
* @param node pointer for traversing the list.
|
* @param node pointer for traversing the list.
|
||||||
*/
|
*/
|
||||||
void printList(Node *node)
|
void printList(Node *node) {
|
||||||
{
|
while (node != nullptr) {
|
||||||
while (node != NULL)
|
|
||||||
{
|
|
||||||
std::cout << node->val << "\t";
|
std::cout << node->val << "\t";
|
||||||
node = node->next;
|
node = node->next;
|
||||||
}
|
}
|
||||||
@ -69,12 +65,10 @@ void printList(Node *node)
|
|||||||
* function reverseList for reversing the list
|
* function reverseList for reversing the list
|
||||||
* @param double pointer to head node
|
* @param double pointer to head node
|
||||||
*/
|
*/
|
||||||
void reverseList(Node **head)
|
void reverseList(Node **head) {
|
||||||
{
|
|
||||||
Node *curr = *head;
|
Node *curr = *head;
|
||||||
Node *prev = NULL, *next_node = NULL;
|
Node *prev = nullptr, *next_node = nullptr;
|
||||||
while (curr != NULL)
|
while (curr != nullptr) {
|
||||||
{
|
|
||||||
next_node = curr->next;
|
next_node = curr->next;
|
||||||
curr->next = prev;
|
curr->next = prev;
|
||||||
prev = curr;
|
prev = curr;
|
||||||
@ -87,12 +81,10 @@ void reverseList(Node **head)
|
|||||||
* Allows the user add ,display and reverse values from the list.
|
* Allows the user add ,display and reverse values from the list.
|
||||||
* @returns 0 on exit
|
* @returns 0 on exit
|
||||||
*/
|
*/
|
||||||
int main()
|
int main() {
|
||||||
{
|
int n = 0;
|
||||||
int n;
|
Node *head = nullptr;
|
||||||
Node *head = NULL;
|
while (true) {
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
std::cout << "\n1. Insert";
|
std::cout << "\n1. Insert";
|
||||||
std::cout << "\n2. Display";
|
std::cout << "\n2. Display";
|
||||||
std::cout << "\n3. Resverse";
|
std::cout << "\n3. Resverse";
|
||||||
@ -100,36 +92,31 @@ int main()
|
|||||||
std::cout << "\n\nEnter you choice : ";
|
std::cout << "\n\nEnter you choice : ";
|
||||||
std::cin >> n;
|
std::cin >> n;
|
||||||
|
|
||||||
switch (n)
|
switch (n) {
|
||||||
{
|
case 1: {
|
||||||
case 1:
|
int a = 0;
|
||||||
{
|
std::cout << "\nEnter the element to be inserted : ";
|
||||||
int a;
|
std::cin >> a;
|
||||||
std::cout << "\nEnter the element to be inserted : ";
|
create(&head, a);
|
||||||
std::cin >> a;
|
|
||||||
create(&head, a);
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 2:
|
case 2: {
|
||||||
{
|
std::cout << "Printing the Linked List\n";
|
||||||
std::cout << "Printing the Linked List\n";
|
std::cout << "\n";
|
||||||
std::cout << "\n";
|
printList(head);
|
||||||
printList(head);
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 3:
|
case 3: {
|
||||||
{
|
reverseList(&head);
|
||||||
reverseList(&head);
|
break;
|
||||||
break;
|
}
|
||||||
}
|
case 4: {
|
||||||
case 4:
|
exit(0);
|
||||||
{
|
}
|
||||||
exit(0);
|
default:
|
||||||
}
|
std::cout << "Invalid input\n";
|
||||||
default:
|
|
||||||
std::cout << "Invalid input\n";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user