DLL_INSERTION_AT_FRONTEND

HOME
/*
 PROGRAM TO IMPLEMENT OPERATIONS

 
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_beg(char[ ] );//MEMBER FUNCTIONS

  
 

  
 void show();

}
*head;

/*FUNCTION
FOR INSERTING NODE
 AT THE BEGINNIN*/

{

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;

  
 }

}
  
 

  
     

void
dll::show()//FUNCTION
FOR DISPLAY OUTPUT

{
   
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;
 int
p;

 dll
d;//OBJECT

CREATION
 cout<<“ENTER
THE NO OF STRINGS

 TO
BE INSERTED AT THE BEGINNING”;

 cin>>p;
  
 

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

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

 {
 
cin>>x;

 d.inser_beg(x);
 }
 cout<<“AFTER
INSERTING AT

 THE
FIRST POSOTION THE LIST IS\n”;

 d.show();
  
 

 } //END OF MAIN()

//PROGRAM
ENDS

/*
OUTPUT:
ENTER
THE NO OF STRINGS TO BE

   
INSERTED AT THE BEGINNING

4
ENTER
THE 4  STRINGS

BHANU
DURGA
KRTHEEK
LAKSHMI
AFTER
INSERTING AT THE FIRST

    
POSOTION THE LIST IS

LAKSHMI
KRTHEEK
DURGA
BHANU
THE
NO OF NODES ->4

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: