c语言从键盘读入一串字符,判断输入的字符串是否是回文。要求:

(1) 实现栈的操作(建立空栈、判空、入栈、出栈、取栈顶元素);
(2) 实现队列的操作(建立空队列、判空、入队、出队、取队头元
素);
(3) 使用一个栈和一个队列设计回文判断算法,如果是回文,输出
Yes,否则输出 No
求全部代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*定义链表结点********************************************************************/
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);
}

//钱不好赚哇,望采纳

追问

运行报错

追答

你是什么编译器

追问

vc6.0,不行的话我去下一个vs2012试一下,没问题的话我会认证不过会稍微晚一点。

追答//把出队函数改成如下就好了:
/*出队*/
int queue_pop(queue_t *q, char* p) {
    if (&q->head == q->tail) {
        return 0;
    } else {
        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;
    }
}

温馨提示:答案为网友推荐,仅供参考
相似回答