Implementing tokenizer and adding tokens to the linked list

Implementing tokenizer and adding tokens to the linked list

Here we go

#include 
#include 
#include 
struct Node
{
    char * value;
    struct Node *next;
};
struct Node *head = NULL;
struct Node *last = NULL;
char* substr(char*str, int start, int length)
{
    const char* from = str;
    char *to = (char*) malloc(sizeof(str)/sizeof(char));
    strncpy(to, from+start, length);
    return to;
}

void addToLinkedList(char* value)
{
    struct Node *curr_node = malloc(sizeof(struct Node));
    char * newtemp = malloc(strlen(value));
    strncpy(newtemp, value, strlen(value));
    curr_node->value = newtemp;
    curr_node->next = NULL;
    if (head == NULL)
    {
        head = curr_node;
        last = head;
    }
    else
    {
        last->next = curr_node;
        last = curr_node;
    }

}

void printLinkedList()
{
    struct Node * curr_node = head;
    while (curr_node !=NULL)
    {
        printf("%sn", curr_node->value);
        curr_node = curr_node->next;
    }
}
void main()
{
    int delimiter_size = 3, i=0, j=0;
    char* delimit = "abc";
    char* text = "someabccodingabctoabcparseab";
    int length = strlen(text);
    char* temp = (char*)malloc(length);
    while(i < length)
    {
        if (text[i] == delimit[0]) //check if the begin
        {
            //check if the 
            if (strcmp(substr(text, i, delimiter_size), delimit) == 0)
            {
                addToLinkedList(temp);
                i+=delimiter_size;
                j=0;
                memset(temp, '', strlen(temp));
                continue;
            }
        }
        temp[j] = text[i];
        i++;
        j++;
    }
    if (strlen(temp))
    {
        addToLinkedList(temp);
    }
    printLinkedList();
}

See how you would solve these known algorithm problems

String Ordered Permutation Algorithm Problem

Array reversal in Recurrsion

Check if two strings are anagrams or not

binary tree problems with solution

Check if there are three numbers a, b, c giving a total T from array A

Find the first occurence of number in the sorted array

Java solution for checking anagram strings – tell if phrases are anagrams

Implement Queue Using two Stacks – JavaScript algorithm

Get maximum occurring character

Find the pairs that makes K Complementary in the given array java solution

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*