题解 | #点击消除#

点击消除

https://www.nowcoder.com/practice/8d3643ec29654cf8908b5cf3a0479fd5

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 300000
typedef struct stacknode {
    char data;
    struct stacknode* next;
} Stack, *StackNode;
void Stackpop(StackNode* s) {
    StackNode p = (Stack*)malloc(sizeof(Stack));
    p = *s;
    *s = (*s)->next;
    free(p);
    p=NULL;
}
void Stackpush(StackNode* s, char c) {
    StackNode p = (Stack*)malloc(sizeof(Stack));
    p->data = c;
    p->next = *s;
    *s = p;
}
int main() {
    StackNode s=NULL;
    StackNode p=NULL;
    char str[N];
    scanf("%s", str);
    for (int i = 0; i < strlen(str); i++) {
        if (s==NULL) {
            Stackpush(&s, str[i]);
        } else if (s->data == str[i]) {
            Stackpop(&s);
        } else {
            Stackpush(&s, str[i]);
        }
    }
    if (s== NULL) {
        printf("0\n");
    } else {
        while (s != NULL) {
            Stackpush(&p, s->data);
            s=s->next;
        }
        while (p!=NULL) {
            printf("%c",p->data);
            p=p->next;
        }
    }
    return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
02-16 22:33
杉川机器人 嵌入式工程师 18.0k*13.0, 年终奖1~9个月浮动
点赞 评论 收藏
分享
01-29 16:08
已编辑
华南农业大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务