Showing posts with label queue. Show all posts
Showing posts with label queue. Show all posts

Sunday, 2 July 2017

C++ program of circular queue

In this program we will see how to insert,delete and show data in Circular Queue.

//Program starts

#include<conio.h>
#include<iostream>
using namespace std;

class cirq{
          private:
                    int d,size,cq[5],counter,front,rear;
          public:
                    cirq();
                    void create();
                    void incq(int d);
                    void delcq();
                    void show();
};

cirq::cirq()
{
          size=5;
          counter=0;
          front=-1;
          rear=-1;
}

void cirq::create()
{
          for(int i=0;i<5;i++)
          {
                    cout<<"Enter data at ["<<i<<"] : ";
                    cin>>cq[i];
                   
          }
          front=0;
          rear=4;
          counter=5;
         
}

void cirq::incq(int d)
{
          if(counter==5)
          {
                    cout<<"Queue is full\n";
          }
          else
          {
                    cout<<"Enter data : ";
                    cin>>d;
                    rear=(rear+1)%size;
                    cq[rear]=d;
                    counter++;
          }
}

void cirq::delcq()
{
          if(counter==-1)
          {
                    cout<<"Queue is empty\n";
          }
          else
          {
                    front=(front+1)%size;
                    counter--;
          }
}

void cirq::show()
{
          for(int i=0;i<counter;i++)
          {
                    cout<<"Data at ["<<(front+i)%5<<"] = "<<cq[(front+i)%5]<<endl;
          }
}


int main()
{
          cirq c; //Creating Object
          int b,d;
          c.create();    
do{
          cout<<"\n Press 1 to show data\n Press 2 to Delete data\n Press 3 to Insert data \n Press 4 to EXIT the program\nValue= ";
          cin>>b;
          switch(b)
          {
                    case 1:
                              c.show();
                              break;
                    case 2:
                              c.delcq();
                              break;
                    case 3:
                              c.incq(d);
                              break;
                    case 4:
                              cout<<"Bye! Bye!";
return 1;
          }
}
while(1);
         
         
}


OUTPUT:
Share:

Saturday, 20 May 2017

C++ program of queue

In this program we will take a look on working of queue by craeting, inserting, deleting and displaying an queue.
program:

#include<iostream>
#include<conio.h>

using namespace std;

class queue{
private:
int front,counter,maxsize,rear,q[5];
public:
queue();
void create();
void enqueue(int d);
void dequeue();
void show();
};

queue::queue()
{
front=-1;
rear=-1;
maxsize=5;
counter=0;
}
/*
void queue::create()
{
for(int i=0;i<5;i++)
{
cout<<"Enter data at ["<<i<<"] : ";
cin>>q[i];
}
front=0;
rear=5;
}
*/
void queue::enqueue(int d)
{
if(rear==4)
{
cout<<"\n\nQueue is full\n";
}
else
{
cout<<"Enter data : ";
cin>>d;

rear++;
q[rear]=d;
if(front==-1)
{
front=rear=0;
}
}
}

void queue::dequeue()
{
if(front==rear||front==-1)
{
cout<<"\n\nQueue is empty\n";
}
else
{
front++;
cout<<"\n\nData is deleted\n";
if(front==4&&rear==4)
{
front=rear=-1;
}
}
}

void queue::show()
{
if(rear==-1&&front==-1)
{
cout<<"\nQueue is empty\n";
}
else
{

for(int i=front;i<=rear;i++)
{
cout<<"Data at ["<<i<<"] == "<<q[i]<<endl;
}

}
}

int main()
{
queue q;
int c,d;

// q.create();

do{
cout<<"\nPress 1 for enqueue\nPress 2 for dequeue\nPress 3 for show\nPress 4 for exit\nValue == ";
cin>>c;

switch(c)
{
case 1:
q.enqueue(d);
break;
case 2:
q.dequeue();
break;
case 3:
q.show();
break;
case 4:
return 1;
}
}
while(1);
}
//Code Ends

Screenshot:


Share: