INSERTION_AT_REARTEND

HOME
/*
PROGRAM FOR THE INSERTION OF THE ELEMENTS

    
INTO THE LINKEDLIST  AT THE END  USING SELF 

           

    
REFERENTIAL CLASSES   */

#include”iostream.h”

class
List //NAME OF THE
CLASS

{
private:   
//DATA MEMBERS

   
int data;

   
List *next;

public:       

   //MEMBER FUNCTIONS

  
void insert_end(int); 

   
void display();

}
*head;  //OBJECT
ACTS AS NODE

//FUNCTION
DECLARATION

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;

   
}

}

//DISPLAY
THE RESULT

void
List::display()      
{

   
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

{
  //INITIALIZATION OF HEADER
NODE

   
head=NULL;

List
l; //OBJECT OF THE
CLASS List

   
int x,a[20];

cout<<“ENTER
THE NO OF ELEMENTS

     
TO BE INSERTED\n”;

   
cin>>x;

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

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

   
{

   
    cin>>a[i];

//PASING
THE ELEMENTS TO THE FUNCTION THROUGH OBJECT

   
l.insert_end(a[i]);

   
}

   
cout<<“THE LIST IS\n”;

//CALLING
FUNCTION TO DISPLAY RESULT THROUGH OBJECT

      
l.display();

}

//PROGRAM
ENDS

/*
OUTPUT:
ENTER
THE NO OF ELEMENTS TO

           

      
BE INSERTED
5
ENTER
5ELEMENTS

12
34
56
67
89
THE
LIST IS

12
34
56
67
89
Press
any key to continue 

*/

HOME

Advertisements

2 Comments »

  1. […] INSERTION_AT_REARTEND « Codefords’s Weblog Says: March 27, 2008 at 9:27 am […]

  2. 2
    ashok Says:

    it,s very interesting


RSS Feed for this entry

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: