ENQUEUE

ENQUEUE

HOME

/*
PROGRAM FOR ENQUEUEING THE ELEMENTS  

        
USING SELF  REFERENTIAL CLASSES   */


#include”iostream.h”

class
queue //NAME OF THE
CLASS

{
private:  //DATA MEMBERS
  
 int data;

  
 queue *next;

public:       
 

  
 void enqueue(int);
//MEMBER FUNCTIONS

   
void display();

}
*head;  
//OBJECT ACTS AS NODE

//FUNCTION
DECLARATION

void
queue::enqueue(int ele)   

{
  
 queue *n,*temp;

   
n=new(queue);//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
queue::display()         &

nbsp; 
 
{
queue 
*temp;

  
 

  
 if(head==NULL)

  
 cout<<“LIST IS EMPTY\n”;

  
 else

  
 {

  
 temp=head;

  
 while(temp!=NULL)

  
 {

  
         

  
 cout<<temp->data<<endl;

  
 temp=temp->next;

  
 }

  
 }

}

 //main()
FUNCTION STARTS

void
main()          &nbsp

;           
 
{
head=NULL;//INITIALIZATION OF HEADER
NODE

queue
l;  //OBJECT
OF THE CLASS List

  
 int x,a[20];

cout<<“ENTER
THE NO OF ELEMENTS TO BE ENQUEUED\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.enqueue(a[i]);        &

nbsp;  
 
  
 }

  
 cout<<“THE LIST IS\n”;

  
 l.display();        &nbsp

;        
 
}

//PROGRAM
ENDS

/*
OUTPUT:

ENTER
THE NO OF ELEMENTS

     
TO BE ENQUEUED

5
ENTER
5ELEMENTS

34
56
78
12
30
THE
LIST IS

34
56
78
12
30
Press
any key to continue

*/

HOME

Advertisements

1 Comment »

  1. 1
    MURALI Says:

    i have just visited appearance is good


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: