Archive for the ‘QUEUE’ Category

QUEUES

April 1, 2008

HOME

Queues:
Queue is also a list of elements with insertion is permitted at one end called rear end and deletion permitted at another end called front end.

It follows the property of First In First Out [FIFO].

Queues also implemented using Arrays and Linked

lists.

HOME

Advertisements

DEQUEUE

March 27, 2008

DEQUEUE

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:  //MEMBER
FUNCTIONS       

  
 int dequeue();

  
 void enqueue(int);  

   
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;

  
 }

}
//FUNCTION
FOR DEQUEUEING

int
queue::dequeue()

{
  
 queue *temp;

  
 if(head==NULL)

  
 cout<<“QUEUE IS EMPTY:”;

  
 else

  
 {

  
 temp=head;

  
 head=temp->next;

  
 delete temp;

  
     

  
 }

  
 return(0);

}

//DISPLAY
THE RESULT

void
queue::display()

{
queue 
*temp;

  
 

  
 if(head==NULL)

  
 cout<<” 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;

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]);

  
 }

  
 cout<<“THE LIST IS\n”;

 //CALLING
FUNCTION TO DISPLAY

//RESULT
THROUGH OBJECT

l.display();

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

  
 {

  
 l.dequeue();

cout<<“AFTER
DELETING THE FRONT

      
END “<<a[j]<<” THE QUEUE IS\n”;

  
 l.display();

  
 }

}

//PROGRAM
ENDS

/*
OUTPUT:

 
ENTER THE NO OF
ELEMENTS TO BE ENQUEUED

5
ENTER
5ELEMENTS

12
34
45
67
89
THE
LIST IS

12
34
45
67
89
AFTER
DELETING THE FRONT

        
END 12 THE QUEUE IS

34
45
67
89
AFTER
DELETING THE FRONT

        
END 34 THE QUEUE IS

45
67
89
AFTER
DELETING THE FRONT

         
END 45 THE QUEUE IS

67
89
AFTER
DELETING THE FRONT

        
END 67 THE QUEUE IS

89
AFTER
DELETING THE FRONT

       
END 89 THE QUEUE IS

 EMPTY
Press
any key to continue

*/

HOME

ENQUEUE

March 27, 2008

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