📜  链表中的运行长度解码

📅  最后修改于: 2022-05-13 01:57:43.627000             🧑  作者: Mango

链表中的运行长度解码

给定一个使用运行长度编码算法编码的编码链表。任务是解码给定的链表并生成输入字符串。
行程长度编码:在行程长度编码,对输入字符串通过由字符后跟其计数字符串中替换重复字符的子串进行编码。如果字符是单一的并且不重复,则不会添加它的计数。例如,如果输入字符串是“wwwwaaadexxxxxx”,那么函数应该返回“w4a3dex6”
例子:

方法

  • 遍历链表。
  • 将当前字符存储在变量c 中
  • 检查下一个节点是否为数字并将该数字存储在count 中,否则count1
  • 将字符c附加到列表count次。

下面是上述方法的实现:



C++
// C++ program to decode a linked list
#include 
using namespace std;
 
// Linked list node
struct Node {
    char data;
    struct Node* next;
};
 
// Utility function to create a new Node
Node* newNode(char data)
{
    Node* temp = new Node;
    temp->data = data;
    temp->next = NULL;
 
    return temp;
}
 
// Function to append nodes to a list
void append(struct Node* head_ref, char new_data)
{
    struct Node* new_node = new Node;
 
    struct Node* last = head_ref;
 
    new_node->data = new_data;
 
    new_node->next = NULL;
 
    if (head_ref == NULL) {
        head_ref = new_node;
        return;
    }
 
    while (last->next != NULL)
        last = last->next;
 
    last->next = new_node;
    return;
}
 
// Function to print list
void printList(Node* node)
{
    while (node != NULL) {
        cout << node->data << " ";
 
        node = node->next;
    }
}
 
// Function to decode the linked list
string decodeList(Node* head)
{
    Node* p = head;
    string res = "";
    int count;
 
    // While there are nodes left
    while (p) {
 
        // To store the count by which the current
        // character needs to be repeated
        count = 0;
 
        // Get the current character
        char c = p->data;
        if (p->next) {
            Node* temp = p->next;
 
            // If current node is a digit
            if (temp && temp->data >= '0'
                && temp->data <= '9') {
 
                // Generate the integer from
                // the consecutive digits
                while (temp && temp->data >= '0'
                       && temp->data <= '9') {
                    count = count * 10 + (temp->data - '0');
                    temp = temp->next;
                }
                p = temp;
            }
            else {
                count = 1;
                p = p->next;
            }
        }
        else {
            count = 1;
            p = p->next;
        }
 
        // Repeat the character count times
        for (int i = 0; i < count; i++) {
            res += c;
        }
    }
 
    return res;
}
 
// Driver code
int main()
{
    // Creating the linked list
    Node* head = newNode('a');
    head->next = newNode('5');
    head->next->next = newNode('b');
    head->next->next->next = newNode('r');
 
    cout << decodeList(head);
 
    return 0;
}


Java
// Java program to decode a linked list
class GFG {
 
    // Linked list node
    static class Node {
        char data;
        Node next;
    };
 
    // Utility function to create a new Node
    static Node newNode(char data)
    {
        Node temp = new Node();
        temp.data = data;
        temp.next = null;
 
        return temp;
    }
 
    // Function to append nodes to a list
    static void append(Node head_ref, char new_data)
    {
        Node new_node = new Node();
 
        Node last = head_ref;
 
        new_node.data = new_data;
 
        new_node.next = null;
 
        if (head_ref == null) {
            head_ref = new_node;
            return;
        }
 
        while (last.next != null)
            last = last.next;
 
        last.next = new_node;
        return;
    }
 
    // Function to print list
    static void printList(Node node)
    {
        while (node != null) {
            System.out.print(node.data + " ");
 
            node = node.next;
        }
    }
 
    // Function to decode the linked list
    static String decodeList(Node head)
    {
        // Pointer used to traverse through all
        // the nodes in the list
        Node p = head;
 
        // String to store the decoded message
        String res = "";
 
        int count;
 
        // While there are nodes left
        while (p != null) {
 
            // To store the count by which the current
            // character needs to be repeated
            count = 0;
 
            // Get the current character
            char c = p.data;
            if (p.next != null) {
                Node temp = p.next;
 
                // If current node is a digit
                if (temp != null && temp.data >= '0'
                    && temp.data <= '9') {
 
                    // Generate the integer from
                    // the consecutive digits
                    while (temp != null && temp.data >= '0'
                           && temp.data <= '9') {
                        count = count * 10 + (temp.data - '0');
                        temp = temp.next;
                    }
                    p = temp;
                }
                else {
                    count = 1;
                    p = p.next;
                }
            }
            else {
                count = 1;
                p = p.next;
            }
 
            // Repeat the character count times
            for (int i = 0; i < count; i++) {
                res += c;
            }
        }
 
        return res;
    }
 
    // Driver code
    public static void main(String args[])
    {
        // Creating the linked list
        Node head = newNode('a');
        head.next = newNode('5');
        head.next.next = newNode('b');
        head.next.next.next = newNode('r');
 
        System.out.println(decodeList(head));
    }
}
 
/* This code contributed by PrinciRaj1992 */


Python3
# Python3 program to decode a linked list
  
# Linked list node
class Node:  
    def __init__(self, data):       
        self.data = data
        self.next = None
  
# Utility function to create a Node
def newNode(data):
    temp = Node(data);
    temp.next = None;
  
    return temp;
 
# Function to append nodes to a list
def append(head_ref, new_data):
    new_node = Node;
    last = head_ref;
    new_node.data = new_data;
    new_node.next = None;
    if (head_ref == None):
        head_ref = new_node;
        return;
    while (last.next != None):
        last = last.next;
    last.next = new_node;
    return;
  
# Function to print list
def printList(node):
    while (node != None):       
        print(node.data, end = ' ')
        node = node.next;
      
# Function to decode the linked list
def decodeList(head):
    p = head;
    res = "";
    count = 0
  
    # While there are nodes left
    while (p != None):
  
        # To store the count by which the current
        # character needs to be repeated
        count = 0;
  
        # Get the current character
        c = p.data;
        if (p.next != None):
             
            temp = p.next;
  
            # If current node is a digit
            if (temp != None and ord(temp.data) >= ord('0')
                and ord(temp.data) <= ord('9')):
  
                # Generate the integer from
                # the consecutive digits
                while (temp != None and ord(temp.data) >= ord('0')
                       and ord(temp.data) <= ord('9')):
                    count = count * 10 + ord(temp.data) - ord('0')
                    temp = temp.next;
             
                p = temp;
             
            else:
                count = 1;
                p = p.next;           
        else:
            count = 1;
            p = p.next;
          
        # Repeat the character count times
        for i in range(0, count):      
            res += c;
    return res;
  
# Driver code
if __name__=='__main__':
     
    # Creating the linked list
    head = newNode('a');
    head.next = newNode('5');
    head.next.next = newNode('b');
    head.next.next.next = newNode('r');
  
    print(decodeList(head))
 
  # This code is contributed by rutvik_56


C#
// C# program to decode a linked list
using System;
 
public class GFG {
 
    // Linked list node
    public class Node {
        public char data;
        public Node next;
    };
 
    // Utility function to create a new Node
    static Node newNode(char data)
    {
        Node temp = new Node();
        temp.data = data;
        temp.next = null;
 
        return temp;
    }
 
    // Function to append nodes to a list
    static void append(Node head_ref, char new_data)
    {
        Node new_node = new Node();
 
        Node last = head_ref;
 
        new_node.data = new_data;
 
        new_node.next = null;
 
        if (head_ref == null) {
            head_ref = new_node;
            return;
        }
 
        while (last.next != null)
            last = last.next;
 
        last.next = new_node;
        return;
    }
 
    // Function to print list
    static void printList(Node node)
    {
        while (node != null) {
            Console.Write(node.data + " ");
 
            node = node.next;
        }
    }
 
    // Function to decode the linked list
    static String decodeList(Node head)
    {
        // Pointer used to traverse through all
        // the nodes in the list
        Node p = head;
 
        // String to store the decoded message
        String res = "";
 
        int count;
 
        // While there are nodes left
        while (p != null) {
 
            // To store the count by which the current
            // character needs to be repeated
            count = 0;
 
            // Get the current character
            char c = p.data;
            if (p.next != null) {
                Node temp = p.next;
 
                // If current node is a digit
                if (temp != null && temp.data >= '0'
                    && temp.data <= '9') {
 
                    // Generate the integer from
                    // the consecutive digits
                    while (temp != null && temp.data >= '0'
                           && temp.data <= '9') {
                        count = count * 10 + (temp.data - '0');
                        temp = temp.next;
                    }
                    p = temp;
                }
                else {
                    count = 1;
                    p = p.next;
                }
            }
            else {
                count = 1;
                p = p.next;
            }
 
            // Repeat the character count times
            for (int i = 0; i < count; i++) {
                res += c;
            }
        }
 
        return res;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        // Creating the linked list
        Node head = newNode('a');
        head.next = newNode('5');
        head.next.next = newNode('b');
        head.next.next.next = newNode('r');
 
        Console.WriteLine(decodeList(head));
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
aaaaabr

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程