Program to implement Run Length Encoding using Linked Lists
Given a Linked List as the input. The task is to encode the given linked list using Run Length Encoding. That is, to replace a block of contiguous characters by the character followed by it’s count.
For Example, in Run Length Encoding “a->a->a->a->a” will be replaced by “a->5”.
Note: For non-repeating nodes, do not append count 1. For example, a->b->b will be replaced by “a->b->2” and not “a->1->b->2”.
Examples:
Input : List = a->a->a->a->a->b->r->r->r->NULL
Output : a->5->b->r->3->NULL
Explanation :
The character ‘a’ repeats 5 times.
The character ‘b’ repeats 1 time.
The character ‘r’ repeats 3 times.
Hence the output is a->5->b->r->3->NULL.
Input : a->a->a->a->a->a->a->a->a->a->b->r->r->r->a->a->a->NULL
Output : a->1->0->b->r->3->a->3->NULL
Approach:
- Traverse through the list.
- Consider the first character as c.
- Consider the current character as x.
- If the character is the same as c then increment the count.
- If the characters are not same then add the count to the list and append the next character to the list reset the count to 1.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
char data;
struct Node* next;
Node( int x)
{
data = x;
next = NULL;
}
};
void append( struct Node* head_ref, char new_data)
{
struct Node* new_node = new Node(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 printList(Node* node)
{
while (node != NULL) {
cout << node->data << " " ;
node = node->next;
}
}
void RLE(Node* head)
{
Node* p = head;
Node* temp = new Node(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)
append(temp, '0' + (count / 10));
append(temp, '0' + (count % 10));
}
count = 1;
append(temp, x);
c = x;
}
p = p->next;
}
if (count != 0)
append(temp, '0' + count);
printList(temp);
}
int main()
{
Node* head = new Node( 'a' );
head->next = new Node( 'a' );
head->next->next = new Node( 'a' );
head->next->next->next = new Node( 'b' );
head->next->next->next->next = new Node( 'r' );
head->next->next->next->next->next = new Node( 'r' );
RLE(head);
return 0;
}
|
Java
class GFG
{
static class Node
{
char data;
Node next;
};
static Node newNode( char data)
{
Node temp = new Node();
temp.data = data;
temp.next = null ;
return temp;
}
static void append(Node head_ref, char new_data)
{
Node new_node = newNode(new_data);
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 ;
}
static void printList(Node node)
{
while (node != null )
{
System.out.print(node.data+ " " );
node = node.next;
}
}
static void RLE(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 )
append(temp, ( char ) ( '0' + (count / 10 )));
append(temp, ( char ) ( '0' + (count % 10 )));
}
count = 1 ;
append(temp, x);
c = x;
}
p = p.next;
}
if (count != 0 )
append(temp, ( char ) ( '0' + count));
printList(temp);
}
public static void main(String[] args)
{
Node head = newNode( 'a' );
head.next = newNode( 'a' );
head.next.next = newNode( 'a' );
head.next.next.next = newNode( 'b' );
head.next.next.next.next = newNode( 'r' );
head.next.next.next.next.next = newNode( 'r' );
RLE(head);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def append(head_ref, new_data):
_node = Node(new_data);
last = head_ref;
if (head_ref = = None ):
head_ref = _node;
return ;
while (last. next ! = None ):
last = last. next ;
last. next = _node;
return ;
def printList(node):
while (node ! = None ):
print (node.data, end = ' ' )
node = node. next ;
def RLE(head):
p = head;
temp = Node(p.data);
c = p.data;
p = p. next ;
count = 1 ;
while (p ! = None ):
x = p.data;
if (c = = x):
count + = 1
else :
if (count > 1 ):
if (count > 9 ):
append(temp, chr ( ord ( '0' ) + (count / / 10 )));
append(temp, chr ( ord ( '0' ) + (count % 10 )));
count = 1 ;
append(temp, x);
c = x;
p = p. next ;
if (count ! = 0 ):
append(temp, chr ( ord ( '0' ) + count))
printList(temp);
if __name__ = = '__main__' :
head = Node( 'a' );
head. next = Node( 'a' );
head. next . next = Node( 'a' );
head. next . next . next = Node( 'b' );
head. next . next . next . next = Node( 'r' );
head. next . next . next . next . next = Node( 'r' );
RLE(head);
|
C#
using System;
class GFG
{
public class Node
{
public char data;
public Node next;
};
static Node newNode( char data)
{
Node temp = new Node();
temp.data = data;
temp.next = null ;
return temp;
}
static void append(Node head_ref, char new_data)
{
Node new_node = newNode(new_data);
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 ;
}
static void printList(Node node)
{
while (node != null )
{
Console.Write(node.data+ " " );
node = node.next;
}
}
static void RLE(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)
append(temp, ( char ) ( '0' + (count / 10)));
append(temp, ( char ) ( '0' + (count % 10)));
}
count = 1;
append(temp, x);
c = x;
}
p = p.next;
}
if (count != 0)
append(temp, ( char ) ( '0' + count));
printList(temp);
}
public static void Main()
{
Node head = newNode( 'a' );
head.next = newNode( 'a' );
head.next.next = newNode( 'a' );
head.next.next.next = newNode( 'b' );
head.next.next.next.next = newNode( 'r' );
head.next.next.next.next.next = newNode( 'r' );
RLE(head);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function newNode(data) {
var temp = new Node();
temp.data = data;
temp.next = null ;
return temp;
}
function append(head_ref, new_data) {
var new_node = newNode(new_data);
var last = head_ref;
if (head_ref == null ) {
head_ref = new_node;
return ;
}
while (last.next != null ) last = last.next;
last.next = new_node;
return ;
}
function printList(node) {
while (node != null ) {
document.write(node.data + " " );
node = node.next;
}
}
function RLE(head) {
var p = head;
var temp = newNode(p.data);
var c = p.data;
p = p.next;
var count = 1;
while (p != null ) {
var x = p.data;
if (c == x)
count++;
else {
if (count > 1) {
if (count > 9)
append(
temp,
String.fromCharCode( "0" .charCodeAt(0) +
parseInt(count / 10))
);
append(
temp,
String.fromCharCode( "0" .charCodeAt(0) +
(count % 10))
);
}
count = 1;
append(temp, x);
c = x;
}
p = p.next;
}
if (count != 0)
append(temp, String.fromCharCode( "0" .charCodeAt(0) +
count));
printList(temp);
}
var head = newNode( "a" );
head.next = newNode( "a" );
head.next.next = newNode( "a" );
head.next.next.next = newNode( "b" );
head.next.next.next.next = newNode( "r" );
head.next.next.next.next.next = newNode( "r" );
RLE(head);
</script>
|
Complexity Analysis:
- Time Complexity: O(N2)
- Auxiliary Space: O(1)
In Place Conversion:
The idea here is to modify the existing list based on the frequency of characters rather than creating a new list if system enforces space constraint.
- Traverse through the list.
- Compare current character with the next character. If same then increment the count value.
- Delete nodes whose frequency is greater than 2.
- If characters are not same, then update the count value.
C++
#include<stdio.h>
#include<stdlib.h>
struct Node
{
char data;
struct Node* next;
Node( int x)
{
data = x;
next = NULL;
}
};
Node* insert (Node *head, int data)
{
if (head == NULL)
return new Node(data);
head->next = insert(head->next, data);
return head;
}
void printList (Node* head)
{
while (head != NULL)
{
printf ( "%c " ,head->data);
head = head->next;
}
return ;
}
void runLengthEncode (Node* head)
{
Node* temp = NULL;
Node* ptr = NULL;
int count = 0;
temp = head;
while (temp != NULL)
{
ptr = temp;
count = 1;
while (temp->next != NULL &&
temp->data == temp->next->data)
{
count++;
if (count > 2)
{
ptr->next = temp->next;
free (temp);
temp = ptr;
}
temp = temp->next;
}
if (count > 1)
temp->data = count + '0' ;
temp = temp->next;
}
return ;
}
int main()
{
Node* head = new Node( 'a' );
head->next = new Node( 'a' );
head->next->next = new Node( 'a' );
head->next->next->next = new Node( 'b' );
head->next->next->next->next = new Node( 'r' );
head->next->next->next->next->next = new Node( 'r' );
runLengthEncode (head);
printList (head);
return 0;
}
|
Java
class GFG {
static class Node
{
char data;
Node next;
};
static Node newNode( char data)
{
Node temp = new Node();
temp.data = data;
temp.next = null ;
return temp;
}
static Node insert(Node head, char data)
{
if (head == null )
return newNode(data);
head.next = insert(head.next, data);
return head;
}
static void printList (Node head)
{
while (head != null )
{
System.out.print(head.data + " " );
head = head.next;
}
return ;
}
static void runLengthEncode (Node head)
{
Node temp;
Node ptr;
int count = 0 ;
temp = head;
while (temp != null )
{
ptr = temp;
count = 1 ;
while (temp.next != null &&
temp.data == temp.next.data)
{
count++;
if (count > 2 )
{
ptr.next = temp.next;
temp= null ;
temp = ptr;
}
temp = temp.next;
}
if (count > 1 )
temp.data = ( char ) (count + '0' );
temp = temp.next;
}
return ;
}
public static void main(String [] args)
{
Node head = newNode( 'a' );
head.next = newNode( 'a' );
head.next.next = newNode( 'a' );
head.next.next.next = newNode( 'b' );
head.next.next.next.next = newNode( 'r' );
head.next.next.next.next.next = newNode( 'r' );
runLengthEncode (head);
printList (head);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def insert(head, data):
if (head = = None ):
return Node(data);
head. next = insert(head. next , data);
return head;
def printList(head):
while (head ! = None ):
print (head.data, end = ' ' )
head = head. next ;
return ;
def runLengthEncode(head):
temp = None ;
ptr = None ;
count = 0 ;
temp = head;
while (temp ! = None ):
ptr = temp;
count = 1 ;
while (temp. next ! = None and
temp.data = = temp. next .data):
count + = 1
if (count > 2 ):
ptr. next = temp. next ;
del (temp);
temp = ptr;
temp = temp. next ;
if (count > 1 ):
temp.data = count ;
temp = temp. next ;
return ;
if __name__ = = '__main__' :
head = Node( 'a' );
head. next = Node( 'a' );
head. next . next = Node( 'a' );
head. next . next . next = Node( 'b' );
head. next . next . next . next = Node( 'r' );
head. next . next . next . next . next = Node( 'r' );
runLengthEncode(head);
printList(head);
|
C#
using System;
class GFG
{
class Node
{
public char data;
public Node next;
};
static Node newNode( char data)
{
Node temp = new Node();
temp.data = data;
temp.next = null ;
return temp;
}
static Node insert(Node head, char data)
{
if (head == null )
return newNode(data);
head.next = insert(head.next, data);
return head;
}
static void printList(Node head)
{
while (head != null )
{
Console.Write(head.data + " " );
head = head.next;
}
}
static void runLengthEncode(Node head)
{
Node temp;
Node ptr;
int count = 0;
temp = head;
while (temp != null )
{
ptr = temp;
count = 1;
while (temp.next != null &&
temp.data == temp.next.data)
{
count++;
if (count > 2)
{
ptr.next = temp.next;
temp = null ;
temp = ptr;
}
temp = temp.next;
}
if (count > 1)
temp.data = ( char )(count + '0' );
temp = temp.next;
}
}
static void Main( string [] args)
{
Node head = newNode( 'a' );
head.next = newNode( 'a' );
head.next.next = newNode( 'a' );
head.next.next.next = newNode( 'b' );
head.next.next.next.next = newNode( 'r' );
head.next.next.next.next.next = newNode( 'r' );
runLengthEncode(head);
printList(head);
}
}
|
Javascript
<script>
class Node
{
constructor(x)
{
this .data=x;
this .next= null ;
}
}
function insert (head,data)
{
if (head == null )
return new Node(data);
head.next = insert(head.next, data);
return head;
}
function printList (head)
{
while (head != null )
{
document.write(head.data+ " " );
head = head.next;
}
return ;
}
function runLengthEncode (head)
{
let temp = null ;
let ptr = null ;
let count = 0;
temp = head;
while (temp != null )
{
ptr = temp;
count = 1;
while (temp.next != null &&
temp.data == temp.next.data)
{
count++;
if (count > 2)
{
ptr.next = temp.next;
delete (temp);
temp = ptr;
}
temp = temp.next;
}
if (count > 1)
temp.data = count ;
temp = temp.next;
}
return ;
}
let head = new Node( 'a' );
head.next = new Node( 'a' );
head.next.next = new Node( 'a' );
head.next.next.next = new Node( 'b' );
head.next.next.next.next = new Node( 'r' );
head.next.next.next.next.next = new Node( 'r' );
runLengthEncode (head);
printList (head);
</script>
|
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(1)
Last Updated :
25 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...