File Name :- dll.cpp
#include<iostream.h>
#include<conio.h>
#include<alloc.h>
//structure of Node
struct node{
struct node *back;
int info;
struct node *next;
};
typedef struct node NODE;
NODE *first=NULL,*last=NULL,*n=NULL;
//creation of DLL.
void createdll(){
int ch=1,item;
while(ch==1){
clrscr();
n= malloc(sizeof(NODE));
if(n==NULL){
cout<<"Overflow Condition"<<endl;
getch();
return;
}
cout<<"Enter item to insert"<<endl;
cin>>item;
n->info=item;
n->back=NULL;
if(first==NULL){
n->next=NULL;
first=last=n;
}
else{
n->next=first;
first->back=n;
first=n;
}
cout<<"Do you want to add another node\n if yes press 1 \n if no press 0"<<endl;
cin>>ch;
}
cout<<"Doubly Linked List Created"<<endl;
getch();
return;
}
void traversefdll(){
NODE *temp=first;
int i=0;
clrscr();
if(temp==NULL){
cout<<"Doubly Linked List is Not Exist"<<endl;
getch();
return;
}
cout<<"Doubly Linked List is:-"<<endl;
while(temp!=NULL){i++;
cout<<"Node No:-"<<i<<endl<<"back part="<<temp->back<<endl<<"info part="<<temp-
>info<<endl<<"next Part="<<temp->next<<endl;
temp=temp->next;
}
cout<<"Linked List Traversed"<<endl<<i<<"Node's are
Visited."<<endl<<"first="<<first<<"and last="<<last<<endl;
getch();
return;
}
void traversebdll(){
NODE *temp=last;
int i=0;
clrscr();
if(temp==NULL){
cout<<"Doubly Linked List is Not Exist"<<endl;
getch();
return;
}
cout<<"Doubly Linked List is:-"<<endl;
while(temp!=NULL){i++;
cout<<"Node No:-"<<i<<endl<<"back part="<<temp->back<<endl<<"info part="<<temp-
>info<<endl<<"next Part="<<temp->next<<endl;
temp=temp->back;
}
cout<<"Linked List Traversed"<<endl<<i<<"Node's are
Visited."<<endl<<"first="<<first<<"and last="<<last<<endl;
getch();
return;
}
void searchdll(int item){
NODE *temp=first;
int i=0;
clrscr();
if(temp==NULL){
cout<<"Doubly Linked List is Not Exist"<<endl;
getch();
return;
}
while(temp!=NULL){
i++;
if(item==temp->info){
cout<<"Searching is Successful"<<endl<<item<<"is found at"<<endl<<"Node
no="<<i<<"back part="<<temp->back<<endl<<"info part="<<temp->info<<endl<<"next
part="<<temp->next<<endl;
getch();
return;
}
temp=temp->next;
}
cout<<"Searching is Unsuccessful!!"<<endl<<item<<" is not found"<<endl;
getch();
return;
}
void sortdll(NODE *f){
NODE *i,*j;
int swap;
if(f==NULL){
cout<<"Doubly Linked List is Not Exist"<<endl;
getch();
return;
}
for(i=f;i!=NULL;i=i->next){
for(j=i->next;j!=NULL;j=j->next){
if(i->info>j->info){
swap=i->info;
i->info=j->info;
j->info=swap;
}}}
cout<<"DLL is Sorted"<<endl;
getch();
return;
}
void insertfirstdll(){
int item;
clrscr();
n=malloc(sizeof(NODE));
if(n==NULL){
cout<<"Overflow Condition"<<endl;
getch();
return;
}
cout<<"Enter an item to insert"<<endl;
cin>>item;
n->info=item;
n->back=NULL;
if(first==NULL){
n->next=NULL;
first=last=n;
}
else{
n->next=first;
first->back=n;
first=n;
}
cout<<"First node is inserted in DLL"<<endl;
getch();
return;
}
void insertlastdll(){
int item;
clrscr();
n=malloc(sizeof(NODE));
if(n==NULL){
cout<<"Overflow Condition"<<endl;
getch();
return;
}
cout<<"Enter an item to insert"<<endl;
cin>>item;
n->info=item;
n->next=NULL;
if(last==NULL){
n->back=NULL;
first=last=n;
}
else{
n->back=last;
last->next=n;
last=n;
}
cout<<"Last node is inserted in DLL"<<endl;
getch();
return;
}
void deletefirstdll(){
clrscr();
if(first==NULL){
cout<<"Underflow Condition"<<endl;
getch();
return;
}
n=first;
if(first->next==NULL){
first=last=NULL;}
else{
first=first->next;
first->back=NULL;}
cout<<"first node"<<n->info<<" is deleted"<<endl;
getch();
free(n);
return;
}
void deletelastdll(){
clrscr();
if(last==NULL){
cout<<"Underflow Condition"<<endl;
getch();
return;
}
n=last;
if(last->back==NULL){
first=last=NULL;}
else{
last=last->back;
last->next=NULL;}
cout<<"Last node"<<n->info<<" is deleted"<<endl;
getch();
free(n);
return;
}
void main(){
int ch;
while(1){
clrscr();
cout<<"Doubly Linked List Operations:-\n 1. creation\n2. Traveresing Forward
\n3.Traversing Backward\n4.search\n5.Insertion\n6.Deletion\n7.Exit\nenter Your
Choice"<<endl;
cin>>ch;
case 1: createdll();
break;
case 2: traversefdll();
break;
case 3: traversebdll();
break;
case 4:
cout<<"Enter an item to search"<<endl;
cin>>item;
searchdll(item);
break;
case 5: cout<<"Insertion:-\n1.insert first node\n2.insert last node\nenter your
choice"<<endl;
cin>>ch;
switch(ch){
case 1: insertfirstdll();
break;
case 2:
insertlastdll();
break;
}
break;
case 6:
cout<<"Deletion:-\n1.Delete first node\n2.Delete last node\nenter your
choice"<<endl;
cin>>ch;
switch(ch){
case 1: deletefirstdll();
break;
case 2: deletelastdll();
break;
}
break;
case 7:exit(0);
default:
cout<<"Wrong Choice\nTry Again!!"<<endl;
getch();
}
}
}
File Name :- dll.c
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
//structure of Node.
struct node{
struct node *back;
int info;
struct node *next;
};
typedef struct node
NODE;
NODE
*first=NULL,*last=NULL,*n=NULL;
//creation of DLL.
void createdll(){
int ch=1,item;
while(ch==1){
clrscr();
n= malloc(sizeof(NODE));
if(n==NULL){
printf("Overflow
Condition\n");
getch();
return;
}
printf("Enter item
to insert\n");
scanf("%d",&item);
n->info=item;
n->back=NULL;
if(first==NULL){
n->next=NULL;
first=last=n;
}
else{
n->next=first;
first->back=n;
first=n;
}
printf("Do you want
to add another node\n if yes press 1 \n if no press 0\n");
scanf("%d",&ch);
}
printf("Doubly
Linked List Created\n");
getch();
return;
}
//Forward & Backward
traverse in DLL
void traversefdll(){
NODE *temp=first;
int i=0;
clrscr();
if(temp==NULL){
printf("Doubly
Linked List is Not Exist\n");
getch();
return;
}
printf("Doubly
Linked List is:-\n");
while(temp!=NULL){i++;
printf("Node
No:-%d\nback part=%u\ninfo part= %d\nnext
Part=%u\n",i,temp->back,temp->info,temp->next);
temp=temp->next;
}
printf("Linked List
Traversed \n %d Node's are Visited.\nfirst=%u and last=%u",i,first,last);
getch();
return;
}
void traversbdll(){
NODE *temp=last;
int i=0;
clrscr();
if(temp==NULL){
printf(" Doubly
Linked List is Not Exist\n");
getch();
return;
}
printf("Doubly
Linked List in Backward direction is:-\n");
while(temp!=NULL){i++;
printf("Node
No:-%d\nback part=%u\ninfo part= %d\nnext
Part=%u\n",i,temp->back,temp->info,temp->next);
temp=temp->back;
}
printf(" Doubly
Linked List Traversed \n %d Node's are Visited.\nfirst=%u and
last=%u",i,first,last);
getch();
return;
}
//search in DLL
void searchdll(int
item){
NODE *temp=first;
int i=0;
clrscr();
if(temp==NULL){
printf("Doubly
Linked List is Not Exist\n");
getch();
return;
}
while(temp!=NULL){
i++;
if(item==temp->info){
printf("Searching
is Successful\n%d is found at location=%u\n Node no= %d\n back part=%u \n info
part= %d \n next part=
%u\n",item,temp,i,temp->back,temp->info,temp->next);
getch();
return;
}
temp=temp->next;
}
printf("Searching
is Unsuccessful!!\n);
getch();
return;
}
//sort DLL
void sortdll(NODE *f){
NODE *i,*j;
int swap;
if(f==NULL){
printf("Doubly
Linked List is Not Exist\n");
getch();
return;
}
for(i=f;i!=NULL;i=i->next){
for(j=i->next;j!=NULL;j=j->next){
if(i->info>j->info){
swap=i->info;
i->info=j->info;
j->info=swap;
}}}
printf("DLL is
Sorted\n");
getch();
return;
}
//Insert First Node in
DLL
void insertfirstdll(){
int item;
clrscr();
n=malloc(sizeof(NODE));
if(n==NULL){
printf("Overflow
Condition\n");
getch();
return;
}
printf("Enter an
item to insert\n");
scanf("%d",&item);
n->info=item;
n->back=NULL;
if(first==NULL){
n->next=NULL;
first=last=n;
}
else{
n->next=first;
first->back=n;
first=n;
}
printf("First node
nserted\n");
getch();
return;
}
//Insert Last Node in
DLL
void insertlastdll(){
int item;
clrscr();
n=malloc(sizeof(NODE));
if(n==NULL){
printf("Overflow
Condition\n");
getch();
return;
}
printf("Enter item
to insert\n");
scanf("%d",&item);
n->info=item;
n->next=NULL;
if(last==NULL){
n->back=NULL;
first=last=n;
}
else{
n->back=last;
last->next=n;
last=n;
}
printf("last Node
inserted\n");
getch();
return;
}
//Insert a Node after
given Location in DLL
void
insertafterlocdll(NODE *loc){
int item;
clrscr();
n= malloc(sizeof(NODE));
if(n==NULL){
printf("Overflow
Condition\n");
getch();
return;
}
if(first==NULL){
printf("DLL is not
Exist\n");
getch();
return;
}
if(loc==last){
printf("location is
last Node\n Choose Option insert last node in dll\n");
getch();
return;
}
printf("Enter an
item to insert\n");
scanf("%d",&item);
n->info=item;
n->back=loc;
n->next=loc->next;
loc->next->back=n;
loc->next=n;
printf("node
inserted after given location\n");
getch();
return;
}
//Insert a Node before
given Location in DLL
void
insertbeforelocdll(NODE *loc){
int item;
clrscr();
n= malloc(sizeof(NODE));
if(n==NULL){
printf("Overflow
Condition\n");
getch();
return;
}
if(first==NULL){
printf("DLL is not
Exist\n");
getch();
return;
}
if(loc==first){
printf("location
sis first Node\nChoose Option insert first node in dll\n");
getch();
return;
}
printf("Enter an
item to insert\n");
scanf("%d",&item);
n->info=item;
n->back=loc->back;
n->next=loc;
loc->back->next=n;
loc->back=n;
}
printf("node
inserted before given location\n");
getch();
return;
}
//Delete First Node in
DLL
void deletefirstdll(){
clrscr();
if(first==NULL){
printf("Underflow
Condition\n");
getch();
return;
}
n=first;
if(first->next==NULL){
first=last=NULL;}
else{
first=first->next;
first->back=NULL;}
printf("first node
%d is deleted\n",n->info);
getch();
free(n);
return;
}
//Delete Last Node in
DLL
void deletelastdll(){
clrscr();
if(last==NULL){
printf("Underflow
Condition\n");
getch();
return;
}
n=last;
if(last->back==NULL){
first=last=NULL;}
else{
last=last->back;
last->next=NULL;}
printf("last node
%d is deleted\n",n->info);
getch();
free(n);
return;
}
//Delete a Node after
given Location in DLL
void
deleteafterlocdll(NODE *loc){
clrscr();
if(first==NULL){
printf("Underflow
Condition\n");
getch();
return;
}
if(loc==last){
printf("loc is last
node\ndeletion is not possible\n");
getch();
return;
}
if(loc->next=last){
printf("location is
second last node\nChoose option delete last node in dll\n");
getch();
return;
}
else{
n=loc->next;
loc->next->next->back=loc;
loc->next=loc->next->next;
}
printf("Node= %d
after given location is deleted\n",n->info);
getch();
free(n);
return;
}
//Delete a Node before
given Location in DLL
void
deletebeforelocdll(NODE *loc){
if(last==NULL){
printf("Underflow
Condition\n");
getch();
return;
}
if(loc==first){
printf("loc is
first node\ndeletion is not possible\n");
getch();
return;
}
if(loc->back=first){
printf("location is
second node\nChoose option delete first node in dll\n");
getch();
return;
}
else{
n=loc->back;
loc->back->back->next=loc;
loc->back=loc->back->back;
printf("Node= %d
before given location is deleted\n",n->info);
getch();
free(n);
return;
}
void main(){
int ch=0,item=0;
NODE *loc=NULL;
while(1){
clrscr();
printf("Doubly
Linked List
Operations:-\n1.Creation\n2.Traversing\n3.Searching\n4.Sorting\n5.Insertion\n6.Deletion\n7.Exit\nEnter
Your Choice = ");
scanf("%d",&ch);
switch(ch){
case 1: createdll();
break;
case 2:
printf("Traversing:-\n1.Forward Traverse\n2.Backward Traverse\nenter your
choice\n");
scanf("%d",&ch);
switch(ch){
case 1: traversefdll();
break;
case 2:
traversebdll();
break;
}
break;
case 3:
printf("Enter an
item to search\n");
scanf("%d",&item);
searchdll(item);
break;
case 4:
sortdll(start);
break;
case 5:
printf("Insertion:-\n1.insert first node\n2.insert last node\n3.insert
after given location\n4.insert before given location\nenter your
choice\n");
scanf("%d",&ch);
switch(ch){
case 1:
insertfirstdll();
break;
case 2:
insertlastdll();
break;
case 3:
printf("Enter
Location to insert Node\n");
scanf("%u",&loc);
insertafterlocdll(loc);
break;
case 4:
printf("Enter
Location to insert Node\n");
scanf("%u",&loc);
insertbeforelocdll(loc);
break;
}
break;
case 6:
printf("Deletion:-\n1.Delete
first node\n2.Delete last node\n3.Delete after given location\n4.Delete before
given location\nenter your choice\n");
scanf("%d",&ch);
switch(ch){
case 1: deletefirstdll();
break;
case 2:
deletelastdll();
break;
case 3:
printf("Enter
Location to insert Node\n");
scanf("%u",&loc);
deleteafterlocdll(loc);
break;
case 4:
printf("Enter
Location to insert Node\n");
scanf("%u",&loc);
deletebeforelocdll(loc);
break;
}
break;
case 7:
exit();
default:
printf("Wrong
Choice\nTry Again!!\n");
getch();
}
}
}
No comments:
Post a Comment