C link list example

. . . .
.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

typedef struct no
{
    char *str;
    int cnt;
    struct no *next;
} nodes;

int main()
{
    int cou = 0;
    nodes *head = NULL;
    char *inp = malloc(10101 * sizeof(char));
    while (fgets(inp, 10101, stdin))
    {
        if (inp[strlen(inp) - 1] == '\n')
            inp[strlen(inp) - 1] = '\0';
        nodes *ls = head;

        if (*inp == '-')
        {
            inp += 1;
            while (ls != NULL)
            {
                if (strcmp(inp, (ls->str)) == 0)
                {
                    ls->cnt = (ls->cnt) - 1;
                    break;
                }
                ls = ls->next;
            }
        }
        else
        {
            if (head == NULL)
            {
                nodes *news = malloc(sizeof(nodes));
                news->cnt = 1;
                news->str = strdup(inp);
                news->next = NULL;
                head = news;
            }
            else
            {
                int find = 0;
                while (ls != NULL)
                {
                    if (strcmp(inp, (ls->str)) == 0)
                    {
                        (ls->cnt) = (ls->cnt) + 1;
                        find = 1;
                        break;
                    }
                    ls = ls->next;
                }
                if (find == 0)
                {
                    ls = head;
                    while (ls->next != NULL)
                    {
                        ls = ls->next;
                    }
                    nodes *news = malloc(sizeof(nodes));
                    news->cnt = 1;
                    news->str = strdup(inp);
                    news->next = NULL;
                    ls->next = news;
                }
            }
        }
    }
    nodes *ls = head;
    while (ls != NULL)
    {
        printf("%s: %d\n", ls->str, ls->cnt);
        ls = ls->next;
    }

/* 
* #Link list
*  -insert
*  -search
*  -print
*  -update
*  -clear
*/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

#define TRUE 1
#define FALSE 0

typedef struct n {
    char *str;
    int cnt;
    struct n *next;
} node;

int search(node *list, char *str);
node *insert(node *list, char *str);
void print(node *list);
void update(node *list, char *str);
void clear(node *list);

int main() {
    char *str = malloc(10101 * sizeof(char));
    int f = 0;
    node *head = NULL;
    node *list = head;
    while(fgets(str, 10101, stdin)) {
        str[strlen(str) - 1] = str[strlen(str) - 1] == '\n' ? '\0' : '\0';
        list = head;
        f = search(list, str);
        if(f == FALSE) {
            head = insert(head, str);
        } else {
            update(head, str);
        }
    }
    print(head);
    clear(head);
}

int search(node *list, char *str) {
    if(list == NULL) return 0;
    if(strcmp(list->str, str) == 0) return 1;
    else return search(list->next, str);
}

node *insert(node *list, char *str) {
    if(list == NULL) {
        node *news = malloc(sizeof(node));
        news->str = strdup(str);
        news->cnt = 1;
        news->next = NULL;
        return news;
    }
    list->next = insert(list->next, str);
    return list;
}

void print(node *list) {
    if(list == NULL) return;
    printf("%s %d\n", list->str, list->cnt);
    print(list->next);
    return;
}

void update(node *list, char *str) {
    if(list == NULL) return;
    if(strcmp(list->str, str) == 0) {
        list->cnt = list->cnt + 1;
    } else {
        update(list->next, str);
    }
    return;
}

void clear(node *list) {
    if(list == NULL) return;
    clear(list->next);
    list->str = NULL;
    list->cnt = 0;
    list->next = NULL;
    free(list);
    return;
}


Tags: C