#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
node *next;
node *previous;
};
node *h = NULL, *l = NULL;
node* getnewnode(int v)
{
node *t = (node*) malloc(sizeof(node));
t->data = v;
t->next = NULL;
t->previous = NULL;
return t;
}
void insertLast(int v)
{
node *n = getn(v);
if(h == NULL)
{
h = n;
l = n;
return;
}
n->previous = l;
l->next = n;
l = n;
}
void insertFast(int v)
{
node *n = getn(v);
if(h == NULL)
{
h = n;
l = n;
return;
}
n->next = h;
h->previous = n;
h = n;
}
void insertmid(int v, int p)
{
node *n = getn(v);
if(h == NULL)
{
h = n;
l = n;
return;
}
node *t = (node*) malloc(sizeof(node));
t = h;
int i = 1;
while(i<p-1 && t->next != NULL)
{
t = t->next;
i++;
}
n->next = t->next;
n->previous = t;
if(t->next != NULL)
{
t->next ->previous = n;
}
t->next = n;
}
void printforward()
{
node *t = (node*) malloc(sizeof(node));
t = h;
while(t != NULL)
{
cout << t->data << endl;
t = t->next;
}
cout << endl;
}
void printReverse()
{
node *n = (node*) malloc(sizeof(node));
n = l;
while(n != NULL)
{
cout << n->data << endl;
n = n->previous;
}
cout << endl;
}
void DeleteFirst()
{
if(h == NULL || h->next == NULL)
{
h = NULL;
l = NULL;
return;
}
h->next ->previous = NULL;
h = h->next;
}
void DeleteLast()
{
if(h == NULL || h->next == NULL)
{
h = NULL;
l = NULL;
return;
}
node *n = (node*) malloc(sizeof(node));
n = h;
while(n ->next ->next != NULL)
{
n = n->next;
}
n->next = NULL;
l = n;
}
void search1(int v)
{
node *t = (node*) malloc(sizeof(node));
t = h;
int c = 0;
while(t != NULL)
{
if(t->data == v)
c++;
t = t->next;
}
cout << v << " is Found " << c << " Times." << endl;
}
void deletemid(int v)
{
node *n = (node*) malloc(sizeof(node));
n = h;
int c = 0;
while(n->next != NULL)
{
if(n->next->data == v)
{
if(n->next->next == NULL)
{
n->next = NULL;
c = 1;
}
else
{
n->next ->next ->previous = n->next;
n->next = n->next ->next;
c = 1;
}
break;
}
n = n->next;
}
if(c == 1)
cout << v << " Successfully Deleted." << endl << endl;
else
cout << v << " is not Deleted." << endl << endl;
}
int main()
{
insrt(1);
insrt(2);
insrt(3);
insrt(4);
insrt(5);
insrt(2);
insrt(2);
insrt(2);
insrt(6);
printforward();
deletemid(2);
printforward();
DeleteFirst();
DeleteLast();
printforward();
srch(2);
printReverse();
return 0;
}
using namespace std;
struct node
{
int data;
node *next;
node *previous;
};
node *h = NULL, *l = NULL;
node* getnewnode(int v)
{
node *t = (node*) malloc(sizeof(node));
t->data = v;
t->next = NULL;
t->previous = NULL;
return t;
}
void insertLast(int v)
{
node *n = getn(v);
if(h == NULL)
{
h = n;
l = n;
return;
}
n->previous = l;
l->next = n;
l = n;
}
void insertFast(int v)
{
node *n = getn(v);
if(h == NULL)
{
h = n;
l = n;
return;
}
n->next = h;
h->previous = n;
h = n;
}
void insertmid(int v, int p)
{
node *n = getn(v);
if(h == NULL)
{
h = n;
l = n;
return;
}
node *t = (node*) malloc(sizeof(node));
t = h;
int i = 1;
while(i<p-1 && t->next != NULL)
{
t = t->next;
i++;
}
n->next = t->next;
n->previous = t;
if(t->next != NULL)
{
t->next ->previous = n;
}
t->next = n;
}
void printforward()
{
node *t = (node*) malloc(sizeof(node));
t = h;
while(t != NULL)
{
cout << t->data << endl;
t = t->next;
}
cout << endl;
}
void printReverse()
{
node *n = (node*) malloc(sizeof(node));
n = l;
while(n != NULL)
{
cout << n->data << endl;
n = n->previous;
}
cout << endl;
}
void DeleteFirst()
{
if(h == NULL || h->next == NULL)
{
h = NULL;
l = NULL;
return;
}
h->next ->previous = NULL;
h = h->next;
}
void DeleteLast()
{
if(h == NULL || h->next == NULL)
{
h = NULL;
l = NULL;
return;
}
node *n = (node*) malloc(sizeof(node));
n = h;
while(n ->next ->next != NULL)
{
n = n->next;
}
n->next = NULL;
l = n;
}
void search1(int v)
{
node *t = (node*) malloc(sizeof(node));
t = h;
int c = 0;
while(t != NULL)
{
if(t->data == v)
c++;
t = t->next;
}
cout << v << " is Found " << c << " Times." << endl;
}
void deletemid(int v)
{
node *n = (node*) malloc(sizeof(node));
n = h;
int c = 0;
while(n->next != NULL)
{
if(n->next->data == v)
{
if(n->next->next == NULL)
{
n->next = NULL;
c = 1;
}
else
{
n->next ->next ->previous = n->next;
n->next = n->next ->next;
c = 1;
}
break;
}
n = n->next;
}
if(c == 1)
cout << v << " Successfully Deleted." << endl << endl;
else
cout << v << " is not Deleted." << endl << endl;
}
int main()
{
insrt(1);
insrt(2);
insrt(3);
insrt(4);
insrt(5);
insrt(2);
insrt(2);
insrt(2);
insrt(6);
printforward();
deletemid(2);
printforward();
DeleteFirst();
DeleteLast();
printforward();
srch(2);
printReverse();
return 0;
}
Comments
Post a Comment