Computer >> कंप्यूटर >  >> प्रोग्रामिंग >> C++

सी ++ प्रोग्राम लिंक्ड लिस्ट का उपयोग करके कतार को लागू करने के लिए


एक क्यू एक सार डेटा संरचना है जिसमें तत्वों का संग्रह होता है। Queue FIFO तंत्र को लागू करता है अर्थात जो तत्व पहले डाला जाता है उसे भी पहले हटा दिया जाता है। दूसरे शब्दों में, हाल ही में जोड़े गए सबसे कम तत्व को पहले कतार में हटा दिया जाता है।

लिंक की गई सूची का उपयोग करके कतार को लागू करने वाला एक प्रोग्राम इस प्रकार दिया गया है -

उदाहरण

#include <iostream>
using namespace std;
struct node {
   int data;
   struct node *next;
};
struct node* front = NULL;
struct node* rear = NULL;
struct node* temp;
void Insert() {
   int val;
   cout<<"Insert the element in queue : "<<endl;
   cin>>val;
   if (rear == NULL) {
      rear = (struct node *)malloc(sizeof(struct node));
      rear->next = NULL;
      rear->data = val;
      front = rear;
   } else {
      temp=(struct node *)malloc(sizeof(struct node));
      rear->next = temp;
      temp->data = val;
      temp->next = NULL;
      rear = temp;
   }
}
void Delete() {
   temp = front;
   if (front == NULL) {
      cout<<"Underflow"<<endl;
      return;
   }
   else
   if (temp->next != NULL) {
      temp = temp->next;
      cout<<"Element deleted from queue is : "<<front->data<<endl;
      free(front);
      front = temp;
   } else {
      cout<<"Element deleted from queue is : "<<front->data<<endl;
      free(front);
      front = NULL;
      rear = NULL;
   }
}
void Display() {
   temp = front;
   if ((front == NULL) && (rear == NULL)) {
      cout<<"Queue is empty"<<endl;
      return;
   }
   cout<<"Queue elements are: ";
   while (temp != NULL) {
      cout<<temp->data<<" ";
      temp = temp->next;
   }
   cout<<endl;
}
int main() {
   int ch;
   cout<<"1) Insert element to queue"<<endl;
   cout<<"2) Delete element from queue"<<endl;
   cout<<"3) Display all the elements of queue"<<endl;
   cout<<"4) Exit"<<endl;
   do {
      cout<<"Enter your choice : "<<endl;
      cin>>ch;
      switch (ch) {
         case 1: Insert();
         break;
         case 2: Delete();
         break;
         case 3: Display();
         break;
         case 4: cout<<"Exit"<<endl;
         break;
         default: cout<<"Invalid choice"<<endl;
      }
   } while(ch!=4);
   return 0;
}

आउटपुट

उपरोक्त कार्यक्रम का आउटपुट इस प्रकार है

1) Insert element to queue
2) Delete element from queue
3) Display all the elements of queue
4) Exit
Enter your choice : 1
Insert the element in queue : 4
Enter your choice : 1
Insert the element in queue : 3
Enter your choice : 1
Insert the element in queue : 5
Enter your choice : 2
Element deleted from queue is : 4
Enter your choice : 3
Queue elements are : 3 5
Enter your choice : 7
Invalid choice
Enter your choice : 4
Exit

उपरोक्त कार्यक्रम में, फ़ंक्शन सम्मिलित करें () कतार में एक तत्व सम्मिलित करता है। यदि रियर NULL है, तो कतार खाली है और एक ही तत्व डाला गया है। अन्यथा, आवश्यक तत्व के साथ पीछे के बाद एक नोड डाला जाता है और फिर उस नोड को पीछे की ओर सेट किया जाता है। यह नीचे दिखाया गया है -

void Insert() {
   int val;
   cout<<"Insert the element in queue : "<<endl;
   cin>>val;
   if (rear == NULL) {
      rear = (struct node *)malloc(sizeof(struct node));
      rear->next = NULL;
      rear->data = val;
      front = rear;
   } else {
      temp=(struct node *)malloc(sizeof(struct node));
      rear->next = temp;
      temp->data = val;
      temp->next = NULL;
      rear = temp;
   }
}

फ़ंक्शन डिलीट () में, यदि कतार में कोई तत्व नहीं हैं तो यह अंडरफ्लो स्थिति है। यदि कतार में केवल एक तत्व है जो हटा दिया गया है और आगे और पीछे NULL पर सेट है। अन्यथा, सामने वाला तत्व हटा दिया जाता है और अगला तत्व अगले तत्व की ओर इशारा करता है। यह नीचे दिखाया गया है -

void Delete() {
   temp = front;
   if (front == NULL) {
      cout<<"Underflow"<<endl;
      return;
   } else
   if (temp->next != NULL) {
      temp = temp->next;
      cout<<"Element deleted from queue is : "<<front->data<<endl;
      free(front);
      front = temp;
   } else {
      cout<<"Element deleted from queue is : "<<front->data<<endl;
      free(front);
      front = NULL;
      rear = NULL;
   }
}

फ़ंक्शन डिस्प्ले () में, यदि आगे और पीछे NULL हैं तो कतार खाली है। अन्यथा, सभी कतार तत्वों को अस्थायी चर की मदद से थोड़ी देर के लूप का उपयोग करके प्रदर्शित किया जाता है। यह नीचे दिखाया गया है -

void Display() {
   temp = front;
   if ((front == NULL) && (rear == NULL)) {
      cout<<"Queue is empty"<<endl;
      return;
   }
   cout<<"Queue elements are: ";
   while (temp != NULL) {
      cout<<temp->data<<" ";
      temp = temp->next;
   }
   cout<<endl;
}

फ़ंक्शन मुख्य () उपयोगकर्ता को एक विकल्प प्रदान करता है यदि वे कतार सम्मिलित करना, हटाना या प्रदर्शित करना चाहते हैं। उपयोगकर्ता की प्रतिक्रिया के अनुसार, उपयुक्त फ़ंक्शन को स्विच का उपयोग करके कहा जाता है। यदि उपयोगकर्ता एक अमान्य प्रतिक्रिया दर्ज करता है, तो वह मुद्रित होता है। इसके लिए कोड स्निपेट नीचे दिया गया है -

int main() {
   int ch;
   cout<<"1) Insert element to queue"<<endl;
   cout<<"2) Delete element from queue"<<endl;
   cout<<"3) Display all the elements of queue"<<endl;
   cout<<"4) Exit"<<endl;
   do {
      cout<<"Enter your choice : "<<endl;
      cin>>ch;
      switch (ch) {
         case 1: Insert();
         break;
         case 2: Delete();
         break;
         case 3: Display();
         break;
         case 4: cout<<"Exit"<<endl;
         break;
         default: cout<<"Invalid choice"<<endl;
      }
   } while(ch!=4);
   return 0;
}

  1. सी ++ में डबल लिंक्ड सूची का उपयोग कर प्राथमिकता कतार

    हमें डेटा और प्राथमिकता एक पूर्णांक मान के रूप में दी जाती है और कार्य दी गई प्राथमिकता के अनुसार एक डबल लिंक्ड सूची बनाना और परिणाम प्रदर्शित करना है। Queue एक FIFO डेटा संरचना है जिसमें जो तत्व पहले डाला जाता है वह सबसे पहले निकाला जाता है। प्राथमिकता कतार एक प्रकार की कतार है जिसमें प्राथमिकता क

  1. सी++ प्रोग्राम एडजेंसी लिस्ट को लागू करने के लिए

    एक ग्राफ की आसन्न सूची प्रतिनिधित्व लिंक्ड सूची प्रतिनिधित्व है। इस निरूपण में हमारे पास सूचियों की एक सरणी है सरणी का आकार V है। यहाँ V शीर्षों की संख्या है। दूसरे शब्दों में, हम कह सकते हैं कि हमारे पास विभिन्न सूचियों के V नंबर को संग्रहीत करने के लिए एक सरणी है। यदि कोई सूची शीर्षलेख u वर्टेक्स

  1. सी ++ प्रोग्राम लिंक्ड लिस्ट का उपयोग करके ग्राफ का प्रतिनिधित्व करने के लिए

    एक ग्राफ की घटना मैट्रिक्स स्मृति में संग्रहीत करने के लिए एक ग्राफ का एक और प्रतिनिधित्व है। यह मैट्रिक्स एक वर्ग मैट्रिक्स नहीं है। आपतन मैट्रिक्स का क्रम V x E है। जहाँ V शीर्षों की संख्या है और E ग्राफ़ में किनारों की संख्या है। इस मैट्रिक्स की प्रत्येक पंक्ति में हम कोने रख रहे हैं, और प्रत्ये