
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ program to implement Run Length Encoding using Linked Lists
In this tutorial, we will be discussing a program to implement Run Length Encoding using Linked Lists.
For this we will be given with a linked list. OUr task is too encode the elements of the Linked List using Run Length Encoding.
For example, if the elements of the linked list are “a->a->a->a->a” then in run length encoding they will be replaced by “a → 5”.
Example
#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; }
Output
a 2 b 2 r 2
- Related Articles
- C++ Program to Implement a Binary Search Tree using Linked Lists
- C++ Program to Implement Hash Tables Chaining with Doubly Linked Lists
- C++ Program to Implement Hash Tables chaining with Singly Linked Lists
- Program to find minimum length of lossy Run-Length Encoding in Python
- Run Length Encoding in Python
- C++ Program to Implement Queue using Linked List
- C++ Program to Implement Stack using linked list
- Program to find min length of run-length encoding after removing at most k characters in Python
- Program to implement run length string decoding iterator class in Python
- C++ Program to Implement Singly Linked List
- C++ Program to Implement Doubly Linked List
- Merge two sorted linked lists using C++.
- Merge Sort for Linked Lists using C++.
- C++ Program to Implement Circular Singly Linked List
- C++ Program to Implement Circular Doubly Linked List

Advertisements