Merge branch 'tisha_patch1' of https://github.com/Tishasoumya-02/C-Plus-Plus into tisha_patch1

This commit is contained in:
Tishasoumya-02
2021-09-07 09:57:33 +05:30
2 changed files with 54 additions and 67 deletions

View File

@@ -52,6 +52,7 @@
* [Queue Using Linkedlist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/queue_using_linkedlist.cpp)
* [Queue Using Two Stacks](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/queue_using_two_stacks.cpp)
* [Rb Tree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/rb_tree.cpp)
* [Reverse A Linked List](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/reverse_a_linked_list.cpp)
* [Skip List](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/skip_list.cpp)
* [Sparse Table](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/sparse_table.cpp)
* [Stack](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/stack.h)

View File

@@ -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,
* 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 .
* 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.
*/
#include <iostream>
/**
* 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,33 +33,28 @@ 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;
}
}
/**
* function displays all the elements in the list
* @returns 'void'
* @brief Utility function that displays all the elements in the list
* @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;
@@ -83,16 +77,13 @@ void reverseList(Node **head)
*head = prev;
}
/**
* Main function:
* Allows the user add ,display and reverse values from the list.
* @brief Main function
* @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 +91,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;