কম্পিউটার টিউটোরিয়াল

C++ প্রোগ্রাম লিংকড লিস্ট (Linked List) ব্যবহার করে রান লেথ এনকোডিং (Run Length Encoding) ইমপ্লিমেন্টেশন


এই টিউটোরিয়ালে আমরা লিংকড লিস্ট (Linked List) ব্যবহার করে রান লেথ এনকোডিং (Run Length Encoding) ইমপ্লিমেন্টেশন সম্পর্কে আলোচনা করব।

এখানে একটি লিংকড লিস্ট দেওয়া থাকবে। আমাদের কাজ হবে লিংকড লিস্টের এলিমেন্টগুলো রান লেথ এনকোডিং দিয়ে এনকোড (encode) করা।

উদাহরণস্বরূপ, যদি লিংকড লিস্টের এলিমেন্টগুলো "a->a->a->a->a" হয়, তবে রান লেথ এনকোডিং-এ সেগুলো "a → 5" দিয়ে রিপ্লেস হয়ে যাবে।

উদাহরণ

#include <bits/stdc++.h> using namespace std; //structuring linked list node struct Node {    char data;    struct Node* next; }; //creating a new node Node* newNode(char data){    Node* temp = new Node;    temp->data = data;    temp->next = NULL;    return temp; } //adding nodes to the list void add_node(struct Node* head_ref, char new_data){    struct Node* new_node = newNode(new_data);    struct Node* last = head_ref;    if (head_ref == NULL) {       head_ref = new_node;       return;    }    while (last->next != NULL)       last = last->next;    last->next = new_node;    return; } void print_llist(Node* node){    while (node != NULL) {       cout << node->data << " ";       node = node->next;    } } //encoding the given list void llist_encode(Node* head){    Node* p = head;    Node* temp = newNode(p->data);    char c = p->data;    p = p->next;    int count = 1;    while (p != NULL) {       char x = p->data;       if (c == x)          count++;       else {          if (count > 1) {             if (count > 9)                add_node(temp, '0' + (count / 10));             add_node(temp, '0' + (count % 10));          }          count = 1;          add_node(temp, x);          c = x;       }       p = p->next;    }    if (count != 0)       add_node(temp, '0' + count);    print_llist(temp); } int main(){    Node* head = newNode('a');    head->next = newNode('a');    head->next->next = newNode('b');    head->next->next->next = newNode('b');    head->next->next->next->next = newNode('r');    head->next->next->next->next->next = newNode('r');    llist_encode(head);    return 0; }

আউটপুট

a 2 b 2 r 2

  1. C++ ব্যবহার করে সিঙ্গলি লিংকড লিস্টে মার্জ সর্ট

  2. C++ ব্যবহার করে দুটি সর্টেড লিংকড লিস্ট মার্জ করা

  3. C++ প্রোগ্রাম: রিকারশন ব্যবহার করে স্ট্রিং-এর দৈর্ঘ্য বের করা

  4. লিংকড লিস্ট ব্যবহার করে C++-এ দুটি পলিনোমিয়াল যোগ করা