HOME
/*..PROGRAM
TO IMPLEMENT INSERSION AT ANYWHERE
ON
DOUBLE LINKED LIST USING SELFREFERENTIAL CLASSES..*/
#include<iostream.h>
int
count=0;
class
dll //CLASS
DECLARATION
{
private:
char data[11];
dll *left,*right;//DATA
MEMBERS
public:
//MEMBER FUNCTIONS
void inser_end( char[] );
void inser_beg(char[] );
void inser_any(char[]);
void show();
}
*head;
//FUNCTION
FOR INSERTING NODE AT THE END
void
dll::inser_end(char s[])
{
dll *n,*temp;
n=new(dll);
for(int i=0;s[i]!=”;i++)
n->data[i]=s[i];
n->data[i]=”;
n->left=NULL;
n->right=NULL;
if(head==NULL)
head=n;
else
{
temp=head;
while(temp->right!=NULL)
{
temp=temp->right;
}
temp->right=n;
n->left=temp;
}
}
/*FUNCTION
FOR INSERTING NODE
AT THE BEGGNING*/
void
dll::inser_beg(char s1[ ])
{
dll *n,*temp;
n=new(dll);
for(int i=0;s1[i]!=”;i++)
n->data[i]=s1[i];
n->data[i]=”;
n->right=NULL;
n->left=NULL;
if(head==NULL)
head=n;
else
{
temp=head;
n->right=temp;
temp->left=n;
head=n;
}
}
//FUNCTION
FOR INSERTING NODE AT ANY POSITION
void
dll::inser_any(char s[ ])
{
int pos;
dll *n,*temp,*old;
n=new(dll);
for(int i=0;s[i]!=”;i++)
n->data[i]=s[i];
n->data[i]=s[i];
n->right=NULL;
n->left=NULL;
cout<<”ENTER THE POSITON
&nb
sp;
OF INSERTION”;
cin>>pos;
if(pos>count+1)
cout<<”INSUFFICIENT NODES TO
&nb
sp;
INSERT AT “<<pos<<” POSITION\n”;
else
{
temp=old=head;
if(pos==1)
inser_beg(s);
else
if(pos==count+1)
inser_end(s);
else
{
for(i=1;i<pos;i++)
{
old=temp;
temp=temp->right;
}
old->right=n;
temp->left=n;
n->right=temp;
n->left=old;
}
}
}
//FUNCTION
FOR DISPLAY OUTPUT
void
dll::show()
{
count=0;
dll *temp;
if(head==NULL)
cout<<”NO NODES IN THE LIST “;
else
{
temp=head;
while(temp!=NULL)
{
count++;
cout<<temp->data<<endl;
temp=temp->right;
}
}
cout<<”THE
NO OF NODES ->”<<count<<endl;
}
void
main()//MAIN()
FUNCTION STARTS
{
char x[22];
head=NULL;
dll d;//OBJECT CREATION
cout<<”THE
STRINGS IN THE LIST ARE\n”;
d.inser_end(“Bhanu”);
d.inser_end(“Surendra”);
d.inser_end(“Durga”);
d.inser_end(“Kartheek”);
d.show();
cout<<”ENTER
THE STRING TO BE
INSERTED AT SPECIED POSITION\n”;
cin>>x;
d.inser_any(x);
d.show();
}//END OF MAIN()
//PROGRAM
ENDS
/*
OUTPUT:
THE
STRINGS IN THE LIST ARE
Bhanu
Surendra
Durga
Kartheek
THE
NO OF NODES ->4
ENTER
THE STRING TO BE INSERTED
AT SPECIED POSITION
PHANI
ENTER
THE POSITON OF INSERTION2
Bhanu
PHANI
Surendra
Durga
Kartheek
THE
NO OF NODES ->5
Press
any key to continue
*/
HOME