কম্পিউটার

সার্কুলারলি সিঙ্গলি লিংকড লিস্ট বাছাই করার জন্য C++ প্রোগ্রাম


ডাটা স্ট্রাকচারে, লিঙ্কড লিস্ট হল ডাটা এলিমেন্টের একটি রৈখিক সংগ্রহ। একটি তালিকার প্রতিটি উপাদান বা নোড দুটি আইটেম নিয়ে গঠিত - ডেটা এবং পরবর্তী নোডের একটি রেফারেন্স। শেষ নোড নাল একটি রেফারেন্স আছে. একটি লিঙ্কযুক্ত তালিকায় প্রবেশ বিন্দুকে তালিকার প্রধান বলা হয়।

তালিকার প্রতিটি নোড এককভাবে লিঙ্কযুক্ত তালিকায় বিষয়বস্তু এবং তালিকার পরবর্তী নোডের একটি পয়েন্টার বা রেফারেন্স সংরক্ষণ করে। এককভাবে লিঙ্ক করা তালিকা পূর্ববর্তী নোডের কোনো পয়েন্টার বা রেফারেন্স সংরক্ষণ করে না।

যেহেতু এটি একটি বাছাই করা এককভাবে লিঙ্ক করা তালিকা, তাই লিঙ্ক করা তালিকার ডেটা আইটেমগুলি সর্বদা সাজানো থাকবে৷

সাজানো সার্কুলারলি সিঙ্গলি লিংকড লিস্ট

বাস্তবায়নের জন্য এখানে একটি C++ প্রোগ্রাম রয়েছে

অ্যালগরিদম

Begin
   function createnode() to insert node in the list:
      It checks whether the list is empty or not. If the list is empty put the node as first element and update head.
      If list is not empty,
      It creates a newnode and inserts the number in the data field of the newnode.
      Now the newnode will be inserted in such a way that linked list will remain sorted.
      If it gets inserted at the last, then the newnode points to the head.
      If the newnode inserted at the first, then the linked list starts from there.
End
Begin
   function display() to print the list content having n number of nodes:
      Initialize c = 0.
      Initialize pointer variable with the start address
      while (c <= n)
         Print the node info
         Update pointer variable
         Increment c.
End

উদাহরণ কোড

#include<iostream>
using namespace std;
struct nod {
   int d;
   nod *n;
}
*p = NULL, *head = NULL, *q = NULL, *np = NULL;
int c = 0;
void createnode(int n) {
   np = new nod;
   np->d = n;
   np->n = NULL;
   if (c == 0) {
      head = np;
      p = head;
      p->n = head;
      c++;
   } else if (c == 1) {
      p = head;
      q = p;
      if (np->d < p->d) {
         np->n = p;
         head = np;
         p->n = np;
      } else if (np->d > p->d) {
         p->n = np;
         np->n = head;
      }
      c++;
   } else {
      p = head;
      q = p;
      if (np->d < p->d) {
         np->n = p;
         head = np;
         do {
            p = p->n;
         }
         while (p->n != q);
         p->n = head;
      } else if (np->d > p->d) {
         while (p->n != head && q->d < np->d) {
            q = p;
            p = p->n;
            if (p->n == head) {
               p->n = np;
               np->n = head;
            } else if (np->d< p->d) {
               q->n = np;
               np->n = p;
               break;
            }
         }
      }
   }
}
void display(int i) {
   nod *t = head;
   int c = 0;
   while (c <= i ) {
      cout<<t->d<<"\t";
      t = t->n;
      c++;
   }
}
int main() {
   int i = 0, n, a;
   cout<<"enter the no of nodes\n";
   cin>>n;
   while (i < n) {
      cout<<"\nenter value of node\n";
      cin>>a;
      createnode(a);
      i++;
   }
   cout<<"sorted circularly singly link list"<<endl;
   display(n);
}

আউটপুট

enter the no of nodes
5
enter value of node
6
enter value of node
4
enter value of node
7
enter value of node
3
enter value of node
2
sorted circularly singly link list
2 3 4 6 7 2

  1. বাইনারি সার্চ ট্রিকে সি++ এ এককভাবে লিঙ্ক করা তালিকায় রূপান্তর করার প্রোগ্রাম?

  2. C++ এ k স্থান দ্বারা একটি লিঙ্ক করা তালিকা ঘোরানোর প্রোগ্রাম

  3. C++ এ দ্বিগুণ লিঙ্কযুক্ত তালিকার আকার খোঁজার প্রোগ্রাম

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