From f55c76d0fb82c94eb996ddfc41a0758d11cb331b Mon Sep 17 00:00:00 2001 From: Samrat De Date: Thu, 21 May 2020 17:32:51 +0530 Subject: [PATCH 1/8] fix: Doubly Linked List algorithm bug --- data_structure/Doubly Linked List.cpp | 34 +++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/data_structure/Doubly Linked List.cpp b/data_structure/Doubly Linked List.cpp index 84a239d3f..a1b6da3ad 100644 --- a/data_structure/Doubly Linked List.cpp +++ b/data_structure/Doubly Linked List.cpp @@ -38,13 +38,37 @@ void insert(int x) void remove(int x) { node *t = start; - while (t->val != x) + while (t != NULL && t->val != x) { - t = t->next; + t = t-> next; } - t->prev->next = t->next; - t->next->prev = t->prev; - delete t; + if (t == NULL) + { + return; + } + // if first element is removed + if (t->prev == NULL) + { + if (t->next == NULL) + { + start = NULL; + } + else + { + start = t->next; + start->prev = NULL; + } + } + else if (t->next == NULL) + { + t->prev->next = NULL; + } + else + { + t->prev->next = t->next; + t->next->prev = t->prev; + } + delete t; } void search(int x) From d90fdf8282c368139ff8b4ff85e8cf203bf76af9 Mon Sep 17 00:00:00 2001 From: Samrat De Date: Thu, 21 May 2020 19:40:07 +0530 Subject: [PATCH 2/8] handled all automated failing tests --- data_structure/Doubly Linked List.cpp | 248 +++++++++++--------------- 1 file changed, 107 insertions(+), 141 deletions(-) diff --git a/data_structure/Doubly Linked List.cpp b/data_structure/Doubly Linked List.cpp index a1b6da3ad..a333bd374 100644 --- a/data_structure/Doubly Linked List.cpp +++ b/data_structure/Doubly Linked List.cpp @@ -1,158 +1,124 @@ #include -using namespace std; -struct node -{ - int val; - node *prev; - node *next; +struct node { + int val; + node *prev; + node *next; }; node *start; -void insert(int x) -{ - node *t = start; - if (start != NULL) - { - while (t->next != NULL) - { - t = t->next; - } - node *n = new node; - t->next = n; - n->prev = t; - n->val = x; - n->next = NULL; - } - else - { - node *n = new node; - n->val = x; - n->prev = NULL; - n->next = NULL; - start = n; - } +void insert(int x) { + node *t = start; + if (start != NULL) { + while (t->next != NULL) { + t = t->next; + } + node *n = new node; + t->next = n; + n->prev = t; + n->val = x; + n->next = NULL; + } else { + node *n = new node; + n->val = x; + n->prev = NULL; + n->next = NULL; + start = n; + } } -void remove(int x) -{ - node *t = start; - while (t != NULL && t->val != x) - { - t = t-> next; - } - if (t == NULL) - { - return; - } - // if first element is removed - if (t->prev == NULL) - { - if (t->next == NULL) - { - start = NULL; - } - else - { - start = t->next; - start->prev = NULL; - } - } - else if (t->next == NULL) - { - t->prev->next = NULL; - } - else - { - t->prev->next = t->next; - t->next->prev = t->prev; - } - delete t; +void remove(int x) { + node *t = start; + while (t != NULL && t->val != x) { + t = t-> next; + } + if (t == NULL) { + return; + } + if (t->prev == NULL) { + if (t->next == NULL) { + start = NULL; + } else { + start = t->next; + start->prev = NULL; + } + } else if (t->next == NULL) { + t->prev->next = NULL; + } else { + t->prev->next = t->next; + t->next->prev = t->prev; + } } -void search(int x) -{ - node *t = start; - int found = 0; - while (t != NULL) - { - if (t->val == x) - { - cout << "\nFound"; - found = 1; - break; - } - t = t->next; - } - if (found == 0) - { - cout << "\nNot Found"; - } +void search(int x) { + node *t = start; + int found = 0; + while (t != NULL) { + if (t->val == x) { + std::cout << "\nFound"; + found = 1; + break; + } + t = t->next; + } + if (found == 0) { + std::cout << "\nNot Found"; + } } -void show() -{ - node *t = start; - while (t != NULL) - { - cout << t->val << "\t"; - t = t->next; - } +void show() { + node *t = start; + while (t != NULL) { + std::cout << t->val << "\t"; + t = t->next; + } } -void reverseShow() -{ - node *t = start; - while (t->next != NULL) - { - t = t->next; - } - while (t != NULL) - { - cout << t->val << "\t"; - t = t->prev; - } +void reverseShow() { + node *t = start; + while (t->next != NULL) { + t = t->next; + } + while (t != NULL) { + std::cout << t->val << "\t"; + t = t->prev; + } } -int main() -{ - int choice, x; - do - { - cout << "\n1. Insert"; - cout << "\n2. Delete"; - cout << "\n3. Search"; - cout << "\n4. Forward print"; - cout << "\n5. Reverse print"; - cout << "\n\nEnter you choice : "; - cin >> choice; - switch (choice) - { - case 1: - cout << "\nEnter the element to be inserted : "; - cin >> x; - ; - insert(x); - break; - case 2: - cout << "\nEnter the element to be removed : "; - cin >> x; - remove(x); - break; - case 3: - cout << "\nEnter the element to be searched : "; - cin >> x; - search(x); - break; - case 4: - show(); - break; - case 5: - reverseShow(); - break; - } - } while (choice != 0); - - return 0; +int main() { + int choice, x; + do { + std::cout << "\n1. Insert"; + std::cout << "\n2. Delete"; + std::cout << "\n3. Search"; + std::cout << "\n4. Forward print"; + std::cout << "\n5. Reverse print"; + std::cout << "\n\nEnter you choice : "; + std::cin >> choice; + switch (choice) { + case 1: + std::cout << "\nEnter the element to be inserted : "; + std::cin >> x; + insert(x); + break; + case 2: + std::cout << "\nEnter the element to be removed : "; + std::cin >> x; + remove(x); + break; + case 3: + std::cout << "\nEnter the element to be searched : "; + std::cin >> x; + search(x); + break; + case 4: + show(); + break; + case 5: + reverseShow(); + break; + } + } while (choice != 0); + return 0; } From 74fc841f105df7e4ec4a6b7201c41446fa2c30d4 Mon Sep 17 00:00:00 2001 From: Samrat De Date: Thu, 21 May 2020 19:46:40 +0530 Subject: [PATCH 3/8] handled all automated failing tests --- data_structure/{Doubly Linked List.cpp => doubly_linked_list.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename data_structure/{Doubly Linked List.cpp => doubly_linked_list.cpp} (100%) diff --git a/data_structure/Doubly Linked List.cpp b/data_structure/doubly_linked_list.cpp similarity index 100% rename from data_structure/Doubly Linked List.cpp rename to data_structure/doubly_linked_list.cpp From e5af368065c9fb80774c18c461bf3e0e9dd243a6 Mon Sep 17 00:00:00 2001 From: Samrat De Date: Fri, 22 May 2020 09:07:04 +0530 Subject: [PATCH 4/8] wrapped all the functions inside a class --- data_structure/doubly_linked_list.cpp | 39 ++++++++++++++++++--------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/data_structure/doubly_linked_list.cpp b/data_structure/doubly_linked_list.cpp index a333bd374..78c322b76 100644 --- a/data_structure/doubly_linked_list.cpp +++ b/data_structure/doubly_linked_list.cpp @@ -1,14 +1,26 @@ #include +#include +#include struct node { int val; node *prev; node *next; +}*start; + +class double_linked_list { + public: + double_linked_list() { + start = NULL; + } + void insert(int x); + void remove(int x); + void search(int x); + void show(); + void reverseShow(); }; -node *start; - -void insert(int x) { +void double_linked_list::insert(int x) { node *t = start; if (start != NULL) { while (t->next != NULL) { @@ -28,7 +40,7 @@ void insert(int x) { } } -void remove(int x) { +void double_linked_list::remove(int x) { node *t = start; while (t != NULL && t->val != x) { t = t-> next; @@ -51,7 +63,7 @@ void remove(int x) { } } -void search(int x) { +void double_linked_list::search(int x) { node *t = start; int found = 0; while (t != NULL) { @@ -67,7 +79,7 @@ void search(int x) { } } -void show() { +void double_linked_list::show() { node *t = start; while (t != NULL) { std::cout << t->val << "\t"; @@ -75,9 +87,9 @@ void show() { } } -void reverseShow() { +void double_linked_list::reverseShow() { node *t = start; - while (t->next != NULL) { + while (t != NULL && t->next != NULL) { t = t->next; } while (t != NULL) { @@ -88,6 +100,7 @@ void reverseShow() { int main() { int choice, x; + double_linked_list ob; do { std::cout << "\n1. Insert"; std::cout << "\n2. Delete"; @@ -100,23 +113,23 @@ int main() { case 1: std::cout << "\nEnter the element to be inserted : "; std::cin >> x; - insert(x); + ob.insert(x); break; case 2: std::cout << "\nEnter the element to be removed : "; std::cin >> x; - remove(x); + ob.remove(x); break; case 3: std::cout << "\nEnter the element to be searched : "; std::cin >> x; - search(x); + ob.search(x); break; case 4: - show(); + ob.show(); break; case 5: - reverseShow(); + ob.reverseShow(); break; } } while (choice != 0); From a67f9278e3cf8e0b317125c4d4e5fd4988a3a9d7 Mon Sep 17 00:00:00 2001 From: Samrat De Date: Fri, 22 May 2020 09:11:56 +0530 Subject: [PATCH 5/8] wrapped all the functions inside a class --- data_structure/doubly_linked_list.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/data_structure/doubly_linked_list.cpp b/data_structure/doubly_linked_list.cpp index 78c322b76..805516a6f 100644 --- a/data_structure/doubly_linked_list.cpp +++ b/data_structure/doubly_linked_list.cpp @@ -9,15 +9,15 @@ struct node { }*start; class double_linked_list { - public: - double_linked_list() { - start = NULL; - } - void insert(int x); - void remove(int x); - void search(int x); - void show(); - void reverseShow(); + public: + double_linked_list() { + start = NULL; + } + void insert(int x); + void remove(int x); + void search(int x); + void show(); + void reverseShow(); }; void double_linked_list::insert(int x) { From 3df1f82bf6439cd1539b2c48e917dfc27173b059 Mon Sep 17 00:00:00 2001 From: Samrat De Date: Fri, 22 May 2020 09:14:31 +0530 Subject: [PATCH 6/8] wrapped all the functions inside a class --- data_structure/doubly_linked_list.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/data_structure/doubly_linked_list.cpp b/data_structure/doubly_linked_list.cpp index 805516a6f..17ea2983a 100644 --- a/data_structure/doubly_linked_list.cpp +++ b/data_structure/doubly_linked_list.cpp @@ -9,15 +9,15 @@ struct node { }*start; class double_linked_list { - public: - double_linked_list() { - start = NULL; - } - void insert(int x); - void remove(int x); - void search(int x); - void show(); - void reverseShow(); + public: + double_linked_list() { + start = NULL; + } + void insert(int x); + void remove(int x); + void search(int x); + void show(); + void reverseShow(); }; void double_linked_list::insert(int x) { From 90c246035f8c09e6ba0dda4ea7f67008f0283bb1 Mon Sep 17 00:00:00 2001 From: Samrat De Date: Fri, 22 May 2020 09:21:03 +0530 Subject: [PATCH 7/8] wrapped all the functions inside a class --- data_structure/doubly_linked_list.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structure/doubly_linked_list.cpp b/data_structure/doubly_linked_list.cpp index 17ea2983a..f8d4d83a2 100644 --- a/data_structure/doubly_linked_list.cpp +++ b/data_structure/doubly_linked_list.cpp @@ -11,7 +11,7 @@ struct node { class double_linked_list { public: double_linked_list() { - start = NULL; + start = NULL; } void insert(int x); void remove(int x); From c18c3c129aeedb0631b72dea27ded94e3057aaeb Mon Sep 17 00:00:00 2001 From: Samrat De Date: Fri, 22 May 2020 09:23:02 +0530 Subject: [PATCH 8/8] wrapped all the functions inside a class --- data_structure/doubly_linked_list.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structure/doubly_linked_list.cpp b/data_structure/doubly_linked_list.cpp index f8d4d83a2..17ea2983a 100644 --- a/data_structure/doubly_linked_list.cpp +++ b/data_structure/doubly_linked_list.cpp @@ -11,7 +11,7 @@ struct node { class double_linked_list { public: double_linked_list() { - start = NULL; + start = NULL; } void insert(int x); void remove(int x);