কম্পিউটার

লিঙ্ক করা তালিকা ব্যবহার করে সারি বাস্তবায়নের জন্য C++ প্রোগ্রাম


একটি সারি হল একটি বিমূর্ত ডেটা কাঠামো যাতে উপাদানগুলির একটি সংগ্রহ থাকে৷ সারি 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

উপরের প্রোগ্রামে, Insert() ফাংশনটি সারিতে একটি উপাদান সন্নিবেশিত করে। যদি পিছনে 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;
   }
}

Delete() ফাংশনে, যদি সারিতে কোনো উপাদান না থাকে তবে এটি আন্ডারফ্লো কন্ডিশন। যদি সারিতে শুধুমাত্র একটি উপাদান থাকে যা মুছে ফেলা হয় এবং সামনে এবং পিছনে 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 হয় তবে সারি খালি থাকে। অন্যথায়, টেম্প ভেরিয়েবলের সাহায্যে একটি while লুপ ব্যবহার করে সমস্ত সারি উপাদানগুলি প্রদর্শিত হয়। এটি নীচে দেখানো হয়েছে -

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;
}

ফাংশন main() ব্যবহারকারীকে একটি পছন্দ প্রদান করে যদি তারা সারিটি সন্নিবেশ, মুছে বা প্রদর্শন করতে চায়। ব্যবহারকারীর প্রতিক্রিয়া অনুসারে, উপযুক্ত ফাংশনকে বলা হয় সুইচ ব্যবহার করে। যদি ব্যবহারকারী একটি অবৈধ প্রতিক্রিয়া প্রবেশ করে, তাহলে সেটি মুদ্রিত হয়। এর জন্য কোড স্নিপেট নিচে দেওয়া হল -

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. C++ ব্যবহার করে একটি লিঙ্ক করা তালিকা উল্টান

  2. C++ এ দ্বিগুণ লিঙ্কযুক্ত তালিকা ব্যবহার করে অগ্রাধিকার সারি

  3. সংলগ্নতা তালিকা বাস্তবায়নের জন্য C++ প্রোগ্রাম

  4. লিংকড লিস্ট ব্যবহার করে গ্রাফ রিপ্রেজেন্ট করার জন্য C++ প্রোগ্রাম