BUPT-homework/semester3/pset0/7-3-central-list.c

64 lines
1.1 KiB
C
Raw Normal View History

2022-10-08 09:33:48 +08:00
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct stack {
int val;
struct stack *next;
} stack;
stack *push(stack *st, int val) {
stack *new = malloc(sizeof(stack));
new->next = st;
new->val = val;
return new;
}
int isEmpty(stack *st) { return st == NULL; }
int peek(stack *st) { return st->val; }
stack *pop(stack *st) {
stack *tmp = st->next;
free(st);
return tmp;
}
int main(void) {
stack *st = NULL;
int size = 0;
int in;
int last;
int lastUsed = 0;
int centFound = 0;
while (scanf("%d,", &in) != EOF) {
size++;
if (!isEmpty(st) && peek(st) == in) {
st = pop(st);
} else if (lastUsed == 1 && last == in) {
st = pop(st);
st = pop(st);
centFound = 1;
} else {
if (!isEmpty(st)) {
last = peek(st);
lastUsed = 1;
}
// printf("PUSHING: %d\n", in);
st = push(st, in);
}
}
if (size > 0 && isEmpty(st) && centFound == 1) {
printf("Yes");
} else if (size == 1) {
printf("Yes");
} else {
printf("No");
}
return 0;
}