handled all automated failing tests

This commit is contained in:
Samrat De 2020-05-21 19:40:07 +05:30
parent f55c76d0fb
commit d90fdf8282

View File

@ -1,158 +1,124 @@
#include <iostream> #include <iostream>
using namespace std;
struct node struct node {
{ int val;
int val; node *prev;
node *prev; node *next;
node *next;
}; };
node *start; node *start;
void insert(int x) void insert(int x) {
{ node *t = start;
node *t = start; if (start != NULL) {
if (start != NULL) while (t->next != NULL) {
{ t = t->next;
while (t->next != NULL) }
{ node *n = new node;
t = t->next; t->next = n;
} n->prev = t;
node *n = new node; n->val = x;
t->next = n; n->next = NULL;
n->prev = t; } else {
n->val = x; node *n = new node;
n->next = NULL; n->val = x;
} n->prev = NULL;
else n->next = NULL;
{ start = n;
node *n = new node; }
n->val = x;
n->prev = NULL;
n->next = NULL;
start = n;
}
} }
void remove(int x) void remove(int x) {
{ node *t = start;
node *t = start; while (t != NULL && t->val != x) {
while (t != NULL && t->val != x) t = t-> next;
{ }
t = t-> next; if (t == NULL) {
} return;
if (t == NULL) }
{ if (t->prev == NULL) {
return; if (t->next == NULL) {
} start = NULL;
// if first element is removed } else {
if (t->prev == NULL) start = t->next;
{ start->prev = NULL;
if (t->next == NULL) }
{ } else if (t->next == NULL) {
start = NULL; t->prev->next = NULL;
} } else {
else t->prev->next = t->next;
{ t->next->prev = t->prev;
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) void search(int x) {
{ node *t = start;
node *t = start; int found = 0;
int found = 0; while (t != NULL) {
while (t != NULL) if (t->val == x) {
{ std::cout << "\nFound";
if (t->val == x) found = 1;
{ break;
cout << "\nFound"; }
found = 1; t = t->next;
break; }
} if (found == 0) {
t = t->next; std::cout << "\nNot Found";
} }
if (found == 0)
{
cout << "\nNot Found";
}
} }
void show() void show() {
{ node *t = start;
node *t = start; while (t != NULL) {
while (t != NULL) std::cout << t->val << "\t";
{ t = t->next;
cout << t->val << "\t"; }
t = t->next;
}
} }
void reverseShow() void reverseShow() {
{ node *t = start;
node *t = start; while (t->next != NULL) {
while (t->next != NULL) t = t->next;
{ }
t = t->next; while (t != NULL) {
} std::cout << t->val << "\t";
while (t != NULL) t = t->prev;
{ }
cout << t->val << "\t";
t = t->prev;
}
} }
int main() int main() {
{ int choice, x;
int choice, x; do {
do std::cout << "\n1. Insert";
{ std::cout << "\n2. Delete";
cout << "\n1. Insert"; std::cout << "\n3. Search";
cout << "\n2. Delete"; std::cout << "\n4. Forward print";
cout << "\n3. Search"; std::cout << "\n5. Reverse print";
cout << "\n4. Forward print"; std::cout << "\n\nEnter you choice : ";
cout << "\n5. Reverse print"; std::cin >> choice;
cout << "\n\nEnter you choice : "; switch (choice) {
cin >> choice; case 1:
switch (choice) std::cout << "\nEnter the element to be inserted : ";
{ std::cin >> x;
case 1: insert(x);
cout << "\nEnter the element to be inserted : "; break;
cin >> x; case 2:
; std::cout << "\nEnter the element to be removed : ";
insert(x); std::cin >> x;
break; remove(x);
case 2: break;
cout << "\nEnter the element to be removed : "; case 3:
cin >> x; std::cout << "\nEnter the element to be searched : ";
remove(x); std::cin >> x;
break; search(x);
case 3: break;
cout << "\nEnter the element to be searched : "; case 4:
cin >> x; show();
search(x); break;
break; case 5:
case 4: reverseShow();
show(); break;
break; }
case 5: } while (choice != 0);
reverseShow(); return 0;
break;
}
} while (choice != 0);
return 0;
} }