You are on page 1of 9

/* circular queue program using pointer*/

/* every case is tested and shown in program*/


#include<iostream>
using namespace std;
struct node
{
 int data;
 node *link;
};
class cirque
{
private : Node *front,*rear;
    int count;
public:   cirque();
    void push(int n);
    void pop();
    void display();
    ~cirque();
};
cirque::cirque()
{
 front=rear=NULL;
 count=0;
}
void cirque::push(int n)
{
 node *temp;
 temp=new node;
 if(temp==NULL)
 {
  cout<<"memory is less";
  return;
 }
 temp->data=n;
 if(front==NULL)
 {
  front=rear=temp;
 }
 else
 {
 rear->link=temp;
 rear=rear->link;
 }
 rear->link=front;
 count++;
 
}
void cirque::pop()
{
 if(front==NULL)
 {
  cout<<"ciccular queue is empty";
  return;
 }
 node *temp;
 temp=front;
 cout<<endl<<"deleted item is"<<front->data;
 if(count>0)
 front=temp->link;
 rear->link=front;
 count--;
 delete temp;
}
void cirque::display()
{
 if(front==NULL)
 {
  cout<<endl<<"queue is empty";
  return;
 }
 int local = count;
 node *temp;
 temp=front;
 cout<<endl<<"queue elements are ";
 while(local)
 {
  cout<<" "<<temp->data;
  temp=temp->link;
  local--;
 }
}
cirque::~cirque()
{
 if(front==NULL)
 {
  cout<<endl<<"no memory used";
  return;
 }
 node *temp;
 int n=0;
 while(front->link!=rear->link)
 {
  temp=front;
  front=front->link;
  delete temp;
  cout<<endl<<++n<<" "<<"deleted ";
 }
}
int main()
{
 cirque q;
 q.push(1);
 q.display();
 q.push(2);
 q.display();
 q.push(3);
 q.display();
 q.push(4);
 q.display();
 q.pop();
 q.pop();
 return 0;
}
 
/*second program using array*/
#include<iostream>
using namespace std;
#define MAX 7  /*for array size*/
class cirq
{
private:int arr[MAX];
  int front,rear,count;  /*count to know the no of ele. Present in array.*/
public:
 cirq();
 void push(int n);
 void pop();
 void display();
 ~cirq();
};
cirq::cirq()
{
 front = rear =-1;
 count=0;
}
void cirq:: Push(int n)
{
 if(count<MAX)
 {
  if(front ==-1) 
  {
   front=rear=0;
  }
 else if(rear==MAX-1)  /* if rear is at maximum position then bring it in starting*/
   rear=0;
  else
   rear++;
  arr[rear]=n;
  count++;
 }
 else
 {
  cout<<endl<<"queue is full don't enter now";
 }
}
void cirq::pop()
{
 if(count>0)
 {
  cout<<endl<<"deleted item is"<<arr[front];
  if(front==MAX-1)  /* if front is at maximum position then bring it in starting*/
   front=0;
  else
   front++;
  count--;
 }
 else
  cout<<endl<<"circular queue is empty";
}
void cirq::display()
{
 int local = count;
 int front1=front;
 if(local<0)    /*if there is no element in quque*/
 {
  cout<<endl<<"circular queue is empty";
 }
 else
 {
  cout<<endl<<"circular queue elements are: ";
  while(local)
  {
   cout<<endl<<arr[front1];
   if(front1==MAX-1)   /* if front is at maximum position then bring it in starting*/
    front1=0;
   else
    front1++;
   local--;
  }
 }
}
cirq::~cirq()
{
}
int main()
{
 cirq q;
 q.push(1);
 q.push(2);
 q.push(3);
 q.push(4);
 q.push(5);
 q.push(6);
 q.push(7);
 q.display();
 q.pop();
 q.display();
 q.push(8);
 q.display();
 q.push(9);
 q.display();
 return 0;
}

Program 2

/****** C Program For Impelmetation Of Circular Queue *******/


#define MAX 5

struct queue
{
int arr[MAX];
int rear,front;
};

int isempty(struct queue *p)


{
if(p->front ==p->rear)
return 1;
else
return 0;
}

void insertq(struct queue *p,int v)


{
int t;
t = (p->rear+1)%MAX;
if(t == p->front)
printf("\nQueue Overflow\n");
else
{
p->rear=t;
p->arr[p->rear]=v;
}
}
int removeq(struct queue *p)
{
if(isempty(p))
{
printf("\nQueue Underflow");
exit(0);
}
else
{
p->front=(p->front + 1)%MAX;
return(p->arr[p->front]);
}
}

void main()
{
struct queue q;
char ch;
int no;
clrscr();
q.rear=q.front =0;
insertq(&q,7);
insertq(&q,10);
insertq(&q,12);
insertq(&q,15);
insertq(&q,8);
printf("\n%d\n",removeq(&q));
printf("%d\n",removeq(&q));
printf("%d\n",removeq(&q));
printf("%d\n",removeq(&q));
removeq(&q);
getch();
}
/************** OUTPUT ****************

Queue Overflow

7
10
12
15

Queue Underflow */

3. Copy constractor

Copy constructor is

 a constructor function with the same name as the class


 used to make deep copy of objects.

 There are 3 important places where a copy constructor is called.

1. When an object is created from another object of the same type


2. When an object is passed by value as a parameter to a function
3. When an object is returned from a function
 

If a copy constructor is not defined in a class, the compiler itself defines one. This will ensure a shallow
copy. If the class does not have pointer variables with dynamically allocated memory, then one need not
worry about defining a copy constructor. It can be left to the compiler's discretion.

But if the class has pointer variables and has some dynamic memory allocations, then it is a must to have
a copy constructor.

For ex:
      class A   //Without copy constructor
      {
           private:
           int x;
           public:
           A() {A = 10;}
           ~A() {}
      }

      class B    //With copy constructor


      {
           private:
           char *name;
           public:
           B()
           {
           name = new char[20];
           }
           ~B()
           {
           delete name[];
           }
     //Copy constructor
           B(const B &b)
           {
           name = new char[20];
           strcpy(name, b.name);
           }
      };

If we don't have a copy constructor for the class B. At the first place, if an object is created from some
existing object, we cannot be sure that the memory is allocated. Also, if the memory is deleted in
destructor, the delete operator might be called twice for the same memory location.

Example 2

The copy constructor lets you create a new object from an existing one by initialization. A copy constructor of a
class A is a non-template constructor in which the first parameter is of type A&, const A&, volatile A&, or
const volatile A&, and the rest of its parameters (if there are any) have default values.

If you do not declare a copy constructor for a class A, the compiler will implicitly declare one for you, which will
be an inline public member.

The following example demonstrates implicitly defined and user-defined copy constructors:
#include <iostream>
using namespace std;

struct A {
int i;
A() : i(10) { }
};

struct B {
int j;
B() : j(20) {
cout << "Constructor B(), j = " << j << endl;
}

B(B& arg) : j(arg.j) {


cout << "Copy constructor B(B&), j = " << j << endl;
}

B(const B&, int val = 30) : j(val) {


cout << "Copy constructor B(const B&, int), j = " << j <<
endl;
}
};

struct C {
C() { }
C(C&) { }
};

int main() {
A a;
A a1(a);
B b;
const B b_const;
B b1(b);
B b2(b_const);
const C c_const;
// C c1(c_const);
}

The following is the output of the above example:

Constructor B(), j = 20
Constructor B(), j = 20
Copy constructor B(B&), j = 20
Copy constructor B(const B&, int), j = 30

You might also like