题解 | #点击消除#

点击消除

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

全部评论

相关推荐

嵐jlu:我是山川🐔里🐔🧱的,阿里系简历全过; 你这简历一看就还是半成品啊,没有荣誉经历奖项什么的吗?
投递阿里巴巴集团等公司10个岗位
点赞 评论 收藏
分享
点赞 评论 收藏
分享
完美的潜伏者许愿简历...:隐藏信息被你提取出来了,暗示,这就是暗示
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务