diff --git a/data_structures/reverse_a_linked_list.cpp b/data_structures/reverse_a_linked_list.cpp index cd21a1d45..5cd11783d 100644 --- a/data_structures/reverse_a_linked_list.cpp +++ b/data_structures/reverse_a_linked_list.cpp @@ -9,22 +9,23 @@ * 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 ,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; - * now we store the prev pointer in the current of next ,this is where the actual reversal happens, + * 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; 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 iteration . + * Then the head node is made to point to the last node (prev pointer) after + * completion of iteration . */ #include /** * A Node class containing a value and pointer to next node; */ -class Node -{ -public: - int val; ///< value of the current link - Node *next; ///< pointer to the next value on the list +class Node { + public: + int val; ///< value of the current link + Node *next; ///< pointer to the next value on 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 * 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 *temp; + Node *temp = nullptr; new_node->val = n; - new_node->next = NULL; - if (*head == NULL) - { + new_node->next = nullptr; + if (*head == nullptr) { *head = new_node; - } - else - { + } else { temp = *head; - while (temp->next != NULL) + while (temp->next != nullptr) { temp = temp->next; + } temp->next = new_node; } } @@ -55,10 +53,8 @@ void create(Node **head, int n) * @returns 'void' * @param node pointer for traversing the list. */ -void printList(Node *node) -{ - while (node != NULL) - { +void printList(Node *node) { + while (node != nullptr) { std::cout << node->val << "\t"; node = node->next; } @@ -69,12 +65,10 @@ void printList(Node *node) * function reverseList for reversing the list * @param double pointer to head node */ -void reverseList(Node **head) -{ +void reverseList(Node **head) { Node *curr = *head; - Node *prev = NULL, *next_node = NULL; - while (curr != NULL) - { + Node *prev = nullptr, *next_node = nullptr; + while (curr != nullptr) { next_node = curr->next; curr->next = prev; prev = curr; @@ -87,12 +81,10 @@ void reverseList(Node **head) * Allows the user add ,display and reverse values from the list. * @returns 0 on exit */ -int main() -{ - int n; - Node *head = NULL; - while (1) - { +int main() { + int n = 0; + Node *head = nullptr; + while (true) { std::cout << "\n1. Insert"; std::cout << "\n2. Display"; std::cout << "\n3. Resverse"; @@ -100,36 +92,31 @@ int main() std::cout << "\n\nEnter you choice : "; std::cin >> n; - switch (n) - { - case 1: - { - int a; - std::cout << "\nEnter the element to be inserted : "; - std::cin >> a; - create(&head, a); + switch (n) { + case 1: { + int a = 0; + std::cout << "\nEnter the element to be inserted : "; + std::cin >> a; + create(&head, a); - break; - } - case 2: - { - std::cout << "Printing the Linked List\n"; - std::cout << "\n"; - printList(head); + break; + } + case 2: { + std::cout << "Printing the Linked List\n"; + std::cout << "\n"; + printList(head); - break; - } - case 3: - { - reverseList(&head); - break; - } - case 4: - { - exit(0); - } - default: - std::cout << "Invalid input\n"; + break; + } + case 3: { + reverseList(&head); + break; + } + case 4: { + exit(0); + } + default: + std::cout << "Invalid input\n"; } } return 0;