| #include<iostream.h> const int SIZE=20; typedef int itemType; struct Queue { private: itemType items[SIZE]; int front,rear,count; public: Queue(){ front=0;rear=SIZE-1;count=0;} Queue(const Queue&q); void getCounters(int &front,int &rear,int &count,itemType*&q); int add(itemType newItem); int remove(); int getFront(itemType &item); int isEmpty(); int isFull(); void showqueue(); }; //------------------------------------------------------ Queue::Queue(const Queue&q) { itemType* ptr; q.getCounters(front,rear,count,ptr); for(int index=front,i=0; i!=count;index=(++index)%SIZE,i++) items[index]=ptr[index]; } //------------------------------------------------------- void Queue::getCounters(int &f,int &r,int &c,itemType*&ptr) { f=front; r=rear; c=count; ptr=items; } //---------------------------------------------------------- int Queue::isEmpty(){return (count==0);} //--------------------------------------------------------- int Queue::isFull(){return (count==SIZE);}; //------------------------------------------------------ int Queue::add(itemType newItem) { if(isFull()) { cout<<"Full Queue"; return(0); } else { rear=(++rear)%SIZE; items[rear]=newItem; ++count; return 1; } } //------------------------------------------------- int Queue::remove() { if(isFull()) { cout<<"Empty queue"; return 1; } else { front=(++front)%SIZE; --count; return 1; } } //----------------------------------------------- int Queue::getFront(itemType &item) { if(isEmpty()) { cout<<"Empty Queue"; return 0; } else { item=items[front]; return 1; } } //------------------------------------------------- void Queue:: showqueue() { cout<<" Printing the queue"; for(int index=front,i=0; i!=count;index=(++index)%SIZE,i++) cout<<"-"<<items[index]; cout<<"\n"; } //----------------------------------------------- void main(){ class Queue q1; q1.add(2); q1.add(5); q1.add(7); class Queue q2(q1); cout<<"\n q2 : "; q2.showqueue(); q1.remove(); cout<<"\n q1 : "; q1.showqueue(); int q1front; q1.getFront(q1front); cout<<"\n q1 front : "; cout<<q1front; } |