题解 | #简单错误记录#

简单错误记录

http://www.nowcoder.com/practice/2baa6aba39214d6ea91a2e03dff3fbeb

用链表实现

#include<string.h>
#include<stdlib.h>
void process(char* const str) { //把输入的字符串前面多余部分给“掐”掉
    int i, j;
    char* p;
    
    for (i = 0; str[i] != ' '; i++);
    
    for (j = i; j > i - 17; j--) {
        if (str[j] == '\\') {
            p = str + j + 1;
            int k;
            for (k = 0; p[k]; k++) {
                str[k] = p[k];
            }
            str[k] = 0;
            return;
        }
    }
    p = str + i - 16;
    int k;
    for (k = 0; p[k]; k++) {
        str[k] = p[k];
    }
    str[k] = 0;
}
typedef struct mlog_ {//记录错误信息、数量、next
    char mis[22];
    int n;
    struct mlog_* next;
}mlog;
void add(mlog* head, mlog* node) {//读取一条错误信息到链表,有重复的合并,没重复的添加
    mlog* p = head;
    while (p->next != NULL) {
        if (strcmp(p->mis, node->mis) == 0) {
            p->n++;
            return;
        }
        p = p->next;
    }
    if (strcmp(p->mis, node->mis) == 0) {
        p->n++;
        return;
    }
    p->next = node;
}
void print(mlog* head) {//打印最近8个
    int i = 0;
    mlog* p = head;
    while (p->next) {
        i++;
        p = p->next;
    }
    while (i-->8) {
        head = head->next;
    }
    while (head=head->next) {
        printf("%s %d\n", head->mis, head->n);
    }
}
int main() {
    char str[102];
    mlog* head = (mlog*)malloc(sizeof(mlog));
    head->next = NULL;
    while (fgets(str, 102, stdin)) {
        process(str);
        
        mlog* node = (mlog*)malloc(sizeof(mlog));
        int i;
        for (i = 0; str[i] != '\n'; i++) {
            node->mis[i] = str[i];
        }
        node->mis[i] = 0;
        node->n = 1;
        node->next = NULL;

        add(head, node);
    }
    print(head);
}
全部评论

相关推荐

专心打鱼:互联网搬运工,贴子都要偷
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务