क्यू
क्यू जिसे FIFO के रूप में लागू किया जाता है जहां इन्सर्शन एक छोर (रियर) पर किए जाते हैं और डिलीशन दूसरे छोर (फ्रंट) से किए जाते हैं। पहला एलिमेंट जो दर्ज किया गया था, वह पहले डिलीट होता है।
क्यू ऑपरेशन्स हैं −
EnQueue (int data) − रियर छोर पर इन्सर्शन
int DeQueue()− फ्रंट छोर से डिलीशन
यह ऐरे का उपयोग करके C++ में क्यू को लागू करने का एक प्रोग्राम है।
एल्गोरिदम
Begin function Enqueue() क्यू में एलिमेंट्स इन्सर्ट करने के लिए: यदि क्यू पूरी तरह भरा हुआ है तो "Overflow" प्रिंट करें। अन्यथा रियर पर एलिमेंट इन्सर्ट करें। रियर का मान अपडेट करें End Begin function Dequeue() क्यू से एलिमेंट्स डिलीट करने के लिए: यदि क्यू पूरी तरह खाली है तो "Underflow" प्रिंट करें। अन्यथा फ्रंट से एलिमेंट इन्सर्ट करें। रियर का मान अपडेट करें। End
उदाहरण कोड
#include <bits/stdc++.h> using namespace std; struct Q { int f, r, capacity; int* q; Q(int c) { f = r= 0; capacity = c; q = new int; } ~Q() { delete[] q; } void Enqueue(int d) { if (capacity == r) { //चेक करें कि क्यू खाली है या नहीं printf("\nQueue is full\n"); return; } else { q[r] = d; //डेटा इन्सर्ट करें r++; //रियर अपडेट करें } return; } void Dequeue() { if (f == r) { printf("\nQueue is empty\n"); return; } else { for (int i = 0; i < r - 1; i++) { q[i] = q[i + 1]; } r--; //रियर अपडेट करें } return; } void Display() //क्यू डिस्प्ले करें { int i; if (f == r) { printf("\nQueue is Empty\n"); return; } for (i = f; i < r; i++) { printf(" %d <-- ", q[i]); } return; } void Front() { if (f == r) { printf("\nQueue is Empty\n"); return; } printf("\nFront Element is: %d", q[f]); //क्यू का फ्रंट एलिमेंट प्रिंट करें return; } }; int main(void) { Q qu(3); qu.Display(); cout<<"after inserting elements"<<endl; qu.Enqueue(10); qu.Enqueue(20); qu.Enqueue(30); qu.Display(); qu.Dequeue(); qu.Dequeue(); printf("\n\nafter two node deletion\n\n"); qu.Display(); qu.Front(); return 0; }आउटपुट
Queue is Empty 10 <-- 20 <-- 30 <-- after two node deletion 30 <-- Front Element is: 30