题解 | #点击消除#

点击消除

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;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务