Wednesday, 5 June 2013

Double Linklist in Data Structure



#include<iostream.h>
#include<conio.h>
class node
{
public:
node *next;
node *prev;
int data;
void add(int num);
void display();
int search(int);
int count();
void sort();
void remove(int);
void insert(int pos,int num);
void Reverse();
};
node *p;

/* add  function*/
void node::add(int num)
{
node *q=p;
if(p==NULL)
{
p=new node;
p->data=num;
p->prev=NULL;
p->next=NULL;
}
else
{
while(q->next!=NULL)
{
q=q->next;
}
q->next=new node;
q->next->data=num;
q->next->prev=q;
q->next->next=NULL;
}
}

/* insert  function*/
void node::insert(int pos,int num)
{
node *q=p;
node *temp;
if(pos==1)
{
p=new node;
p->data=num;
p->prev=NULL;
p->next=q;
}

if(pos==count()+1)
{
add(num);
}

else{
for(int i=1;i<=(pos-2);i++)
{
q=q->next;
}
temp=q->next;
q->next=new node;
q->next->data=num;
q->next->next=temp;
q->next->prev=q;
q->next->next->prev=q->next;
}
}

/* search  function*/
int node::search(int num)
{      int flag;
int cnt=1;
node *q=p;
if(p==NULL)
{
cout<<"\n List is empty";
}
else
while(q!=NULL)
{
if(q->data==num)
{
flag=1;
break;
}
cnt++;
q=q->next;
}
if(flag==1)
return cnt;
else
return -1;
}
/* Reverse  function*/
void node :: Reverse()
{
node *q=p;
if(p==NULL)
{
cout<<"\n\n List is Empty";
}
else
{
while(q->next!=NULL)
{
q=q->next;
}
while(q!=NULL)
{
cout<<" "<<q->data;
q=q->prev;
}    }
}
/* remove  function*/
void node::remove(int pos)
{
node *q=p;
if(pos==1)
{
cout<<"\n Deleted node's value is: "<< q->data;
p=p->next;
p->prev=NULL;
delete q;
}
else   {
for(int i=1;i<=(pos-2);i++)
{
q=q->next;
}
node *temp;
cout<<"\n Deleted node's value is: "<< q->next->data;
temp=q->next;
q->next=q->next->next;
q->next->prev=q;
delete temp;
}    }

/* sort function*/
void node::sort()
{
node *i=p,*j;
int temp;
for(i=p;i!=NULL;i=i->next)
{
for(j=i;j!=NULL;j=j->next)
{
if(i->data > j->data)
{
temp=i->data;
i->data=j->data;
j->data=temp;
}
   }
 }
cout<<"\n The linked list has been sorted ";
}



/* count function*/
int node::count()
{       int i=0;
node *q=p;
if(p==NULL)
{
return -1;
}
else {
while(q!=NULL)
{
i++;
q=q->next;
}
return i;
}
}
/* display  function*/
void node:: display()
{
node *q=p;
if(p==NULL)
{
cout<<"\n\n Link list is empty";
}
else {
while(q!=NULL)
{
cout<<"\t "<<q->data;
q=q->next;
}
}
}
/* main()  function*/
void main()
{
node n1;
int ch;
char c;
do
{
cout<<"\n 1: ADD  2: DISPLAY  3: COUNT 4: INSERT\n 5: REMOVE 6: REVERSE 7: SEARCH 8: SORT";
cout<<"\n\n Enter Your Choice: ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n\n Enter Any number: " ;
int num;
cin>>num;
n1.add(num);
break;
case 2:
n1.display();
break;
case 3:
int cnt=n1.count();
if(cnt==-1)
{
cout<<"\n\n Linked list is empty";
}
else
{
cout<<"\n Number of node is: "<<cnt<<"\n";
}
break;
case 4:
int pos;
cout<<"\n\n Enter position to insert new node: ";
cin>>pos;
if(pos>=1 && pos<=(n1.count()+1))
{
cout<<"\n\n Enter any number: ";
int num;
cin>>num;
n1.insert(pos,num);
}
else
{
cout<<"\n\n Invalid position";
}
break;
case 5:
cout<<"\n\n Enter position to delete node: ";
cin>>pos;
if(pos>=1 && pos<=(n1.count()+1))
{

n1.remove(pos);
}
else
{
cout<<"\n\n Invalid position";
}
break;
case 6:
n1.Reverse();
break;
case 7:
int nu ;
cout<<"\n\n Enter the Number To  Search: ";
cin>>nu;
int n=n1.search(nu);
if(n==-1)
{
cout<<"\n Number Not Found";
}
else
{
cout<<"\n Number found at position: "<<n;
}
break;
case 8:
n1.sort();
break;
default:
cout<<"\n wrong selection";
break;
}
cout<<"\n Do you want to continue(y/n): ";
cin>>c;
}
while(c=='y');
}

No comments:

Post a Comment