From 7ed069648d7be4d9897129cfb181125dece6980f Mon Sep 17 00:00:00 2001 From: Marian Gusatu Date: Wed, 22 Apr 2020 11:44:47 +0300 Subject: [PATCH 1/8] fix: reverse check for empty list --- data_structure/Linked List.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/data_structure/Linked List.cpp b/data_structure/Linked List.cpp index cf976e3a5..646d69dc0 100644 --- a/data_structure/Linked List.cpp +++ b/data_structure/Linked List.cpp @@ -100,17 +100,19 @@ void show() void reverse() { node *first = start; - node *second = first->next; - while (second != NULL) - { - node *tem = second->next; - second->next = first; - first = second; - second = tem; - } + if (first != NULL){ + node *second = first->next; + while (second != NULL) + { + node *tem = second->next; + second->next = first; + first = second; + second = tem; + } - start->next = NULL; - start = first; + start->next = NULL; + start = first; + } } int main() From d2760d1e5450e488c38688bfa6a6eaa1a1136b5f Mon Sep 17 00:00:00 2001 From: Marian Gusatu <45270790+mariangusatu@users.noreply.github.com> Date: Wed, 22 Apr 2020 11:56:01 +0300 Subject: [PATCH 2/8] Update Linked List.cpp fix: reverse check for empty list + converting tabs to spaces --- data_structure/Linked List.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/data_structure/Linked List.cpp b/data_structure/Linked List.cpp index 646d69dc0..49e739a3c 100644 --- a/data_structure/Linked List.cpp +++ b/data_structure/Linked List.cpp @@ -101,15 +101,14 @@ void reverse() { node *first = start; if (first != NULL){ - node *second = first->next; - while (second != NULL) - { - node *tem = second->next; - second->next = first; - first = second; - second = tem; - } - + node *second = first->next; + while (second != NULL) + { + node *tem = second->next; + second->next = first; + first = second; + second = tem; + } start->next = NULL; start = first; } From 15a030201da2d665b0d119ca908f73c407e82928 Mon Sep 17 00:00:00 2001 From: Marian Gusatu <45270790+mariangusatu@users.noreply.github.com> Date: Wed, 22 Apr 2020 12:09:27 +0300 Subject: [PATCH 3/8] Update Linked List.cpp fix: reverse check for empty list + from tabs to spaces --- data_structure/Linked List.cpp | 237 +++++++++++++++++---------------- 1 file changed, 120 insertions(+), 117 deletions(-) diff --git a/data_structure/Linked List.cpp b/data_structure/Linked List.cpp index 49e739a3c..d7177805f 100644 --- a/data_structure/Linked List.cpp +++ b/data_structure/Linked List.cpp @@ -1,106 +1,104 @@ #include using namespace std; - struct node { - int val; - node *next; + int val; + 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->val = x; - n->next = NULL; - } - else - { - node *n = new node; - n->val = x; - n->next = NULL; - start = n; - } + node *t = start; + if (start != NULL) + { + while (t->next != NULL) + { + t = t->next; + } + node *n = new node; + t->next = n; + n->val = x; + n->next = NULL; + } + else + { + node *n = new node; + n->val = x; + n->next = NULL; + start = n; + } } void remove(int x) { + if (start == NULL) + { + cout << "\nLinked List is empty\n"; + return; + } + else if (start->val == x) + { + node *temp = start; + start = start->next; + delete temp; + return; + } + + node *temp = start, *parent = start; - if (start == NULL) - { - cout << "\nLinked List is empty\n"; - return; - } - else if (start->val == x) - { - node *temp = start; - start = start->next; - delete temp; - return; - } - - node *temp = start, *parent = start; - - while (temp != NULL && temp->val != x) - { - parent = temp; - temp = temp->next; - } - - if (temp == NULL) - { - cout << endl - << x << " not found in list\n"; - return; - } + while (temp != NULL && temp->val != x) + { + parent = temp; + temp = temp->next; + } + if (temp == NULL) + { + cout << endl << x << " not found in list\n"; + return; + } + parent->next = temp->next; - delete temp; + delete temp; } 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"; - } + 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 show() { - node *t = start; - while (t != NULL) - { - cout << t->val << "\t"; - t = t->next; - } + node *t = start; + while (t != NULL) + { + cout << t->val << "\t"; + t = t->next; + } } void reverse() { - node *first = start; - if (first != NULL){ + node *first = start; + if (first != NULL) + { node *second = first->next; while (second != NULL) { @@ -109,53 +107,58 @@ void reverse() first = second; second = tem; } - start->next = NULL; - start = first; + start->next = NULL; + start = first; + } + else + { + cout<<"\nEmpty list"; + } } int main() { - int choice, x; - do - { - cout << "\n1. Insert"; - cout << "\n2. Delete"; - cout << "\n3. Search"; - cout << "\n4. Print"; - cout << "\n5. Reverse"; - cout << "\n0. Exit"; - 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(); - cout << "\n"; - break; - case 5: - cout << "The reversed list: \n"; - reverse(); - show(); - cout << "\n"; - break; - } - } while (choice != 0); + int choice, x; + do + { + cout << "\n1. Insert"; + cout << "\n2. Delete"; + cout << "\n3. Search"; + cout << "\n4. Print"; + cout << "\n5. Reverse"; + cout << "\n0. Exit"; + 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(); + cout << "\n"; + break; + case 5: + cout << "The reversed list: \n"; + reverse(); + show(); + cout << "\n"; + break; + } + } while (choice != 0); - return 0; + return 0; } From 9c508e4fbb09845bfbc465124c8e62bbcfa1e4eb Mon Sep 17 00:00:00 2001 From: Marian Gusatu <45270790+mariangusatu@users.noreply.github.com> Date: Wed, 22 Apr 2020 12:18:56 +0300 Subject: [PATCH 4/8] Update Linked List.cpp fix: reverse check for empty list + cpplint coding style --- data_structure/Linked List.cpp | 82 ++++++++++++---------------------- 1 file changed, 28 insertions(+), 54 deletions(-) diff --git a/data_structure/Linked List.cpp b/data_structure/Linked List.cpp index d7177805f..14e59f47a 100644 --- a/data_structure/Linked List.cpp +++ b/data_structure/Linked List.cpp @@ -1,29 +1,23 @@ #include -using namespace std; -struct node -{ + +struct node { int val; node *next; }; node *start; -void insert(int x) -{ +void insert(int x) { node *t = start; - if (start != NULL) - { - while (t->next != NULL) - { + if (start != NULL) { + while (t->next != NULL) { t = t->next; } node *n = new node; t->next = n; n->val = x; n->next = NULL; - } - else - { + } else { node *n = new node; n->val = x; n->next = NULL; @@ -31,77 +25,62 @@ void insert(int x) } } -void remove(int x) -{ - if (start == NULL) - { +void remove(int x) { + if (start == NULL) { cout << "\nLinked List is empty\n"; return; - } - else if (start->val == x) - { + } else if (start->val == x) { node *temp = start; start = start->next; delete temp; return; } - + node *temp = start, *parent = start; - while (temp != NULL && temp->val != x) - { + while (temp != NULL && temp->val != x) { parent = temp; temp = temp->next; } - if (temp == NULL) - { + if (temp == NULL) { cout << endl << x << " not found in list\n"; return; } - - parent->next = temp->next; + + parent->next = temp->next; delete temp; } -void search(int x) -{ +void search(int x) { node *t = start; int found = 0; - while (t != NULL) - { - if (t->val == x) - { + while (t != NULL) { + if (t->val == x) { cout << "\nFound"; found = 1; break; } t = t->next; } - if (found == 0) - { + if (found == 0) { cout << "\nNot Found"; } } -void show() -{ +void show() { node *t = start; - while (t != NULL) - { + while (t != NULL) { cout << t->val << "\t"; t = t->next; } } -void reverse() -{ +void reverse() { node *first = start; - if (first != NULL) - { + if (first != NULL) { node *second = first->next; - while (second != NULL) - { + while (second != NULL) { node *tem = second->next; second->next = first; first = second; @@ -110,18 +89,14 @@ void reverse() start->next = NULL; start = first; } - else - { + else { cout<<"\nEmpty list"; - - } + } } -int main() -{ +int main() { int choice, x; - do - { + do { cout << "\n1. Insert"; cout << "\n2. Delete"; cout << "\n3. Search"; @@ -130,8 +105,7 @@ int main() cout << "\n0. Exit"; cout << "\n\nEnter you choice : "; cin >> choice; - switch (choice) - { + switch (choice) { case 1: cout << "\nEnter the element to be inserted : "; cin >> x; From 248c335bf53744b7f16f75d599d46a9d685ea74c Mon Sep 17 00:00:00 2001 From: Marian Gusatu <45270790+mariangusatu@users.noreply.github.com> Date: Wed, 22 Apr 2020 12:21:48 +0300 Subject: [PATCH 5/8] Update Linked List.cpp fix: reverse check for empty list + cpp lint final coding style --- data_structure/Linked List.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/data_structure/Linked List.cpp b/data_structure/Linked List.cpp index 14e59f47a..1d8557ba9 100644 --- a/data_structure/Linked List.cpp +++ b/data_structure/Linked List.cpp @@ -88,9 +88,8 @@ void reverse() { } start->next = NULL; start = first; - } - else { - cout<<"\nEmpty list"; + } else { + cout << "\nEmpty list"; } } From 7be0b8deb3016e263ea1ab7b1243c614a2e96fb6 Mon Sep 17 00:00:00 2001 From: Marian Gusatu <45270790+mariangusatu@users.noreply.github.com> Date: Wed, 22 Apr 2020 12:26:03 +0300 Subject: [PATCH 6/8] Update Linked List.cpp fix: reverse check for empty list + cpplint coding style --- data_structure/Linked List.cpp | 46 +++++++++++++++++----------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/data_structure/Linked List.cpp b/data_structure/Linked List.cpp index 1d8557ba9..4fcb471c3 100644 --- a/data_structure/Linked List.cpp +++ b/data_structure/Linked List.cpp @@ -27,7 +27,7 @@ void insert(int x) { void remove(int x) { if (start == NULL) { - cout << "\nLinked List is empty\n"; + std::cout << "\nLinked List is empty\n"; return; } else if (start->val == x) { node *temp = start; @@ -44,7 +44,7 @@ void remove(int x) { } if (temp == NULL) { - cout << endl << x << " not found in list\n"; + std::cout << endl << x << " not found in list\n"; return; } @@ -57,21 +57,21 @@ void search(int x) { int found = 0; while (t != NULL) { if (t->val == x) { - cout << "\nFound"; + std::cout << "\nFound"; found = 1; break; } t = t->next; } if (found == 0) { - cout << "\nNot Found"; + std::cout << "\nNot Found"; } } void show() { node *t = start; while (t != NULL) { - cout << t->val << "\t"; + std::cout << t->val << "\t"; t = t->next; } } @@ -89,46 +89,46 @@ void reverse() { start->next = NULL; start = first; } else { - cout << "\nEmpty list"; + std::cout << "\nEmpty list"; } } int main() { int choice, x; do { - cout << "\n1. Insert"; - cout << "\n2. Delete"; - cout << "\n3. Search"; - cout << "\n4. Print"; - cout << "\n5. Reverse"; - cout << "\n0. Exit"; - cout << "\n\nEnter you choice : "; - cin >> choice; + std::cout << "\n1. Insert"; + std::cout << "\n2. Delete"; + std::cout << "\n3. Search"; + std::cout << "\n4. Print"; + std::cout << "\n5. Reverse"; + std::cout << "\n0. Exit"; + std::cout << "\n\nEnter you choice : "; + std::cin >> choice; switch (choice) { case 1: - cout << "\nEnter the element to be inserted : "; - cin >> x; + std::cout << "\nEnter the element to be inserted : "; + std::cin >> x; insert(x); break; case 2: - cout << "\nEnter the element to be removed : "; - cin >> x; + std::cout << "\nEnter the element to be removed : "; + std::cin >> x; remove(x); break; case 3: - cout << "\nEnter the element to be searched : "; - cin >> x; + std::cout << "\nEnter the element to be searched : "; + std::cin >> x; search(x); break; case 4: show(); - cout << "\n"; + std::cout << "\n"; break; case 5: - cout << "The reversed list: \n"; + std::cout << "The reversed list: \n"; reverse(); show(); - cout << "\n"; + std::cout << "\n"; break; } } while (choice != 0); From 2edf620f7c7b2e8b17f04f5bcab7b822c1a1b076 Mon Sep 17 00:00:00 2001 From: Marian Gusatu <45270790+mariangusatu@users.noreply.github.com> Date: Wed, 22 Apr 2020 12:29:33 +0300 Subject: [PATCH 7/8] Update Linked List.cpp fix: reverse check for empty list + cpplint coding style --- data_structure/Linked List.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structure/Linked List.cpp b/data_structure/Linked List.cpp index 4fcb471c3..2af3f82a3 100644 --- a/data_structure/Linked List.cpp +++ b/data_structure/Linked List.cpp @@ -44,7 +44,7 @@ void remove(int x) { } if (temp == NULL) { - std::cout << endl << x << " not found in list\n"; + std::cout << std::endl << x << " not found in list\n"; return; } From d999b480e59995aa08c397ea4ba8482edcfd5e2d Mon Sep 17 00:00:00 2001 From: Marian Gusatu <45270790+mariangusatu@users.noreply.github.com> Date: Wed, 22 Apr 2020 12:33:03 +0300 Subject: [PATCH 8/8] Rename Linked List.cpp to linked_list.cpp fix: reverse check for empty list + cpplint identation --- data_structure/{Linked List.cpp => linked_list.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename data_structure/{Linked List.cpp => linked_list.cpp} (100%) diff --git a/data_structure/Linked List.cpp b/data_structure/linked_list.cpp similarity index 100% rename from data_structure/Linked List.cpp rename to data_structure/linked_list.cpp