题解 | #点击消除#
点击消除
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;
}
查看10道真题和解析