This code is an example of queue in C++. As queue works on first come first out(FIFO) basis. Inserting an element into queue is known as enqueue and deleting an element is known as dequeue. Copy and paste the below code into your DevC++ editor and run the program.
#include<iostream> using namespace std; const int LIMIT=20; class Queue { int contents[LIMIT]; int head,tail; bool becomeFull; public: Queue(); bool isFull(); bool isEmpty(); void Enqueue(int x); // Insert data... int Dequeue(); // delete/remove data... }; /*****************************************/ int main() { Queue q1; q1.Enqueue(2); q1.Enqueue(6); q1.Enqueue(9); cout<<q1.Dequeue()<<endl; cout<<q1.Dequeue()<<endl; cout<<q1.Dequeue()<<endl; system("pause"); } /*****************************************/ /*****************************************/ Queue::Queue() { head=0; tail=0; becomeFull=false; } /*****************************************/ bool Queue::isFull() { if(tail==LIMIT) return (true); else return false; } /*****************************************/ bool Queue::isEmpty() { if(head==tail) return true; else return false; } /*****************************************/ void Queue::Enqueue(int x) { if(!isFull()) { contents[tail]=x; tail++; becomeFull=true; if(tail==LIMIT) tail=0; } } /*****************************************/ int Queue::Dequeue() { if(!isEmpty()) { int x=contents[head]; //contents[head]=0; head++; becomeFull=false; if(head==LIMIT) head=0; return x; } } /*****************************************/ /*****************************************/
0 comments:
Post a Comment