INSERTION_AT_ANYWHERE

HOME

/*PROGRAM
FOR THE INSERTION OF

 
THE ELEMENTS INTO THE LINKEDLIST  

  AT
ANYWHERE  USING SELF  REFERENTIAL  
         CLASSES*/


#include<iostream.h>
#include<stdlib.h>

class
List //NAME OF THE
CLASS

{
private:  
//DATA MEMBERS

  
 int data;

  
 List *next;

public:       
 

 
void insert_front(int);

 
void insert_anywhere(int,int);

 
void insert_end(int);//MEMBER
FUNCTIONS

 
void display();

}
*head; //OBJECT ACTS
AS NODE

void
List::insert_front(int ele)  

{        /*FUNCTION  FOR THE
INSERTING AT THE   FRONTEND */

 
List *n,*temp;

   
n=new(List);  //MEMORY
ALLOCATION

  
 n->data=ele;

  
 n->next=NULL;

 /*IF THE LIST IS EMPTY THEN
ADD 1st NODE AS HEADER NODE*/

  
 if(head==NULL)

  
     head=n;

  
 else

  
 {

  
     temp=head;

  
     n->next=temp;

  
     head=n;

  
 }

}
/*FUNCTION
DECLARATION FOR THE INSERTING AT THE END*/

void
List::insert_end(int ele)   

{

 
List *n,*temp;

   
n=new(List);//MEMORY
ALLOCATION

  
 n->data=ele;

  
 n->next=NULL;

 /*IF THE LIST IS EMPTY THEN
ADD 1st NODE AS HEADER NODE*/

  
 if(head==NULL)

  
     head=n;

  
 else

  
 {

  
 temp=head;

  
 while(temp->next!=NULL)

  
 temp=temp->next;

       
temp->next=n;

  
 }

}

/*FUNCTION
FOR THE INSERTING ELEMENTS AT ANY POSITION*/

void
List::insert_anywhere(int pos,int ele)   

{
  
 List *n,*temp;

  
 int  q=1;

  
 n=new(List);

  
 n->data=ele;

  
 n->next=NULL;

  
 temp=head;

   //GOING TO THE GIVEN
POSITION
 

  
 while(q<pos-1&&temp->next!=NULL)   &

nbsp;    
 
  
 {

  
     temp=temp->next;

  
     q++;

  
 }

//INSERTIG
THE NODE AT THE GIVEN POSITION

  
 n->next=temp->next->next;     &

nbsp;          
 
  
 temp->next=n;

}

void
List::display() //DISPLAY
THE RESULT

{
  
 List  *temp;

  
 

  
 if(head==NULL)

  
     cout<<“LIST IS
EMPTY\n”;

  
 else

  
 {

  
 temp=head;

  
 while(temp!=NULL)

  
 {

  
         

  
 cout<<temp->data<<endl;

  
 temp=temp->next;

  
 }

  
 }

}

void
main() //main()
FUNCTION STARTS

{
  
 head=NULL;//INITIALIZATION
OF HEADER NODE

  
 List l; //OBJECT
OF THE CLASS List

  
 int x,p,a[20],r;

s:
cout<<“ENTER
THE NO OF ELEMENTS TO BE INSERTED\n”;

  
 cin>>r;

  
 if(r==0)

  
 {

  
 cout<<“INVALID NUMBER TRY AGAIN”;

  
 goto s;

  
 }

  
 cout<<“ENTER
“<<r<<” ELEMENTS\n”;

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

  
 {

  
     cin>>a[i];

  
 l.insert_end(a[i]);

  
 }

cout<<“ENTER
THE POSITION OF  THE ELEMENT TO BE INSERTED”;

  
 cin>>p;

  
 if(p>r+2)

  
 {

  
 cout<<“LIST LENGTH IS ONLY “<<r;

  
 exit(0);

  
 cout<<“ENTER THE ELEMENT”;

  
 cin>>x;

  
 if(p==1)

  
 l.insert_front(x);  

else
if(p==r+1)

  
 l.insert_end(x);

else  //PASING THE ELEMENTS TO THE
FUNCTION
THROUGH OBJECT

    
l.insert_anywhere(p,x);

  
 cout<<“THE LIST IS\n”;

//CALLING FUNCTION TO DISPLAY
RESULT
THROUGH OBJECT

  
 l.display();        &nbsp

;         
 
}

//PROGRAM
ENDS

/*
OUTPUT:

ENTER
THE NO OF ELEMENTS TO BE INSERTED

4
ENTER
4 ELEMENTS

1
2
3
4
ENTER
THE POSITION OF  THE ELEMENT TO BE INSERTED

3
ENTER
THE ELEMENT

333
THE
LIST IS

1
2
333
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: