Showing posts with label Queue. Show all posts
Showing posts with label Queue. Show all posts

20130330

How to write cpp program for Circular Queue using Array


How to write C++ program for Circular Queue using Array
C++ program to implement Circular Queue using Array
Circular Queue implementation using Array
CPP program for implementing Circular Queue using Array


#include<iostream>
using namespace std;
#define MAXSIZE 4

class queue
{
private:
int cq[MAXSIZE];
int front, rear;
public:
queue()
{
int cq[MAXSIZE]={0};//error: array must be initialized with a brace-enclosed initializer
front= -1;
rear = -1;
}


void insert(int item)
{
cout<<"\n in insert.\t";
if( ( (front==0)&&(rear==MAXSIZE-1)%MAXSIZE ) || front==(rear+1)%MAXSIZE )
{
cout<<"Q is full.\n";
}
else
{
if(front==-1)
front=rear=0;
else
rear=(rear+1)%MAXSIZE;

cq[rear]=item;

cout<<"\n front = "<<front<<" rear = "<<rear<<endl;

}

}


void display()
{
int i;
for(i=0;i<MAXSIZE;i++)
{
cout<<cq[i]<<" ";
}
cout<<endl;
}


void deletee()
{
if(front==-1)
{
cout<<"Q is empty.\n";
}
else
{
int temp;
temp=cq[front];
cq[front]=0;
if(front==rear)//only 1 elt was there
front=rear=-1;//delete the single so q is empty
else
front=(front+1)%MAXSIZE;//if more than 1 elt then delete frm front

cout<<"\n Deleted elt is:"<<temp<<endl;
cout<<"\n front = "<<front<<" rear = "<<rear<<endl;
}
}

};
int main()
{
queue q;
q.insert(10);
q.insert(20);
q.insert(30);
q.insert(40);
q.display();

q.insert(111);//Q is full so make same place by deleting fronts
q.display();

q.deletee();
q.display();
q.insert(100);
q.display();



return 0;
}
/*
in insert.
front = 0 rear = 0

in insert.
front = 0 rear = 1

in insert.
front = 0 rear = 2

in insert.
front = 0 rear = 3
10 20 30 40

in insert. Q is full.
10 20 30 40

Deleted elt is 10.

front = 1 rear = 3
0 20 30 40

in insert.
front = 1 rear = 0
100 20 30 40
*/
Simples Way to learn Data Structure in C++ CPP or C

How to write cpp program for Circular Queue using Linked List



 /*C++ Program for Circular Queue implementation using linked list
Cpp program to implement Circular Queue using Linked List*/
C++ Program for implementing Circular Queue with Linked List

#include<iostream>
using namespace std;

class queue
{
private:
struct node
{
int data;
node* link;
};
node *front;
node *rear;

public:
void insert(int);
void display();
void del();
queue()
{