INSERTION_AT_FRONTEND

HOME

/*PROGRAM
FOR THE INSERTION OF THE ELEMENTS

     
INTO THE LINKEDLIST  AT THE FRONTEND 

          
USING SELF  REFERENTIAL
CLASSES     */

#include”iostream.h”


class List //NAME OF THE
CLASS

{
private:  
//DATA MEMBERS

   
int data;

   
List *next;

public:        

   
void insert_front(int); //MEMBER
FUNCTIONS

   
void display();

}
*head;  //OBJECT
ACTS AS NODE

void
List::insert_front(int ele) //FUNCTION
DECLARATION

{

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

}

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,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_front(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

4
ENTER
4 ELEMENTS

1
2
3
4
THE
LIST IS

4
3
2
1
Press
any key to continue 

*/

HOME

Advertisements

6 Comments »

  1. 1
    bharat Says:

    i understood very easily
    it’s a nice site

  2. 2
    K.ADI NARAYANA Says:

    It’s very nice
    but in case of input/output
    pls specify wat is the input and
    wat is the output

  3. 3
    rajesh Says:

    hi bhanu the idea of having a blog for datastructures learning will definetly enrich our knowledge.

  4. 4
    shareef Says:

    nice one , comments helps me alot , try keep theory of the content.

  5. 5
    chandu Says:

    NICE AND GOOD
    IMPROVE FURTHER IN KEEPING DATA INADDITION TO CODE

  6. 6
    R.Naveen Says:

    i understanded easily about code for linked list programs


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: