#include
#include
#include
/*定义链表结点********************************************************************/
typedef struct st_node {
char ch;
struct st_node *next;
} node_t;
/*定义队列和栈********************************************************************/
typedef struct {
node_t head;
node_t *tail;
} queue_t, stack_t;
/*队列操作方法********************************************************************/
/*初始化队列*/
void queue_init(queue_t *q) {
q->head.next = NULL;
q->tail = &q->head;
}
/*清空队列*/
void queue_clear(queue_t *q) {
node_t *n = q->head.next, *t;
while (n) {
t = n;
n = n->next;
free(t);
}
q->head.next = NULL;
q->tail = &q->head;
}
/*入队*/
void queue_push(queue_t *q, char ch) {
node_t *n = (node_t*)malloc(sizeof(node_t));
n->ch = ch;
n->next = NULL;
q->tail->next = n;
q->tail = n;
}
/*出队*/
int queue_pop(queue_t *q, char* p) {
if (&q->head == q->tail) {
return 0;
}
node_t *f = q->head.next;
*p = f->ch;
q->head.next = f->next;
if (f == q->tail) q->tail = &q->head;
free(f);
return 1;
}
/*判断队列是否为空*/
int queue_empty(queue_t *q) {
return &q->head == q->tail ? 1 : 0;
}
/*栈操作方法********************************************************************/
/*初始化栈*/
void stack_init(stack_t *s) {
queue_init(s);
}
/*清空栈*/
void stack_clear(stack_t *s) {
queue_clear(s);
}
/*入栈*/
void stack_push(stack_t* s, char ch) {
node_t *n = (node_t*)malloc(sizeof(node_t));
n->ch = ch;
n->next = s->head.next;
s->head.next = n;
if (&s->head == s->tail) s->tail = n;
}
/*出栈*/
int stack_pop(stack_t* s, char* p) {
return queue_pop(s, p);
}
/*判断栈是否为空*/
int stack_empty(stack_t *s) {
return queue_empty(s);
}
/*注意上面队列和栈的操作上唯一的区别是入队和入栈的方法不一样,其它都一样。入队是直接放到队尾,而入栈是放到队头*/
/*主函数*/
void main() {
queue_t q;
stack_t s;
char buf[256] = { 0 };
int len, i;
char ch, ch1, ch2;
queue_init(&q);
stack_init(&s);
printf("请输出一个字符串(长度不能超过 %d 个字符):", sizeof(buf) - 1);
scanf("%s", buf);
len = strlen(buf);
for (i = 0; i < len; ++i) {
ch = buf[i];
queue_push(&q, ch);
stack_push(&s, ch);
}
while (queue_pop(&q, &ch1) && stack_pop(&s, &ch2)) {
if (ch1 != ch2) break;
}
if (queue_empty(&q) && stack_empty(&s)) {
printf("YES", buf);
} else {
printf("NO", buf);
}
queue_clear(&q);
stack_clear(&s);
}
//钱不好赚哇,望采纳