DLL_DELETION_ANYWHERE

DOUBLE LINKED LIST DELETION AT ANY WHERE


HOME

/*.PROGRAM
TO IMPLEMENT DELETION 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:
  
 void inser_end( char[] );

  
 void del_any();

  
 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 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;

}

//FUNCTION
FOR DELETING NODE AT ANY POSITION

void
dll::del_any()

{
  
 int pos;

  
 dll *temp,*old,*t;

cout<<“ENTER
THE POSITION OF DELETION”;

  
 cin>>pos;

  
 old=temp=head;

  
 if(pos==1)

  
     

  
 {

  
 temp=temp->right;

  
 t=temp;

  
 head=t;

  
 delete t;

  
 }

  
 

else
{
  
 

  
 for(int i=1;i<pos;i++)

  
 {

  
     old=temp;

  
     temp=temp->right;

  
 }

  
 if(pos==count)

  
 {

  
     temp->left=NULL;

  
     old->right=NULL;

  
     delete temp;

  
 }

  
 else

  
 {

  
 old->right=temp->right;

  
 temp->right->left=old;

   
delete temp;

  
 }

}
}

void 
main()//MAIN()
FUNCTION STARTS

{
  
 char x[22];

  
 int p;

  
 head=NULL;

  
 dll d;//OBJECT CREATION

cout<<“ENTER
THE NO OF STRINGS

     
TO BE INSERTED INTO THE LIST”;

  
 cin>>p;

  
 

cout<<“ENTER
THE “<<p<<”  STRINGS”;

  
 for(int i=0;i<p;i++)

  
 {

  
 cin>>x;

  
 d.inser_end(x);

  
 }

cout<<“AFTER
INSERTING AT

       
THE END THE LIST IS\n”;

  
 d.show();

  
 d.del_any();

cout<<“AFTER
DELETION THE

      
NODES IN THE  LIST ARE\n”;

  
 d.show();

}//END OF MAIN()

//PROGRAM
ENDS

/*
OUTPUT:
 
ENTER
THE NO OF STRINGS

 
TO BE INSERTED INTO THE LIST  4

ENTER
THE 4  STRINGS

BHANU
SURENDRA
SRI
SUKE
AFTER
INSERTING AT

     
THE END THE LIST IS

BHANU
SURENDRA
SRI
SUKE
THE
NO OF NODES ->4

ENTER
THE POSITION OF DELETION4

AFTER
DELETION THE

     
NODES IN THE  LIST ARE

BHANU
SURENDRA
SRI
THE
NO OF NODES ->3

Press
any key to continue

*/

HOME

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: