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