如何用栈来实现字符串的逆序

可以利用c或c++编写程序,用栈实现字符串的逆序排列

你题目的解答代码如下:

#include "stdio.h"

#include "stdlib.h"

#define STACK_INIT_SIZE 100

#define STACKINCREMENT 10

typedef struct

{
    char *base;

    char *top;

    int stacksize;

} SqStack;


int InitStack(SqStack *S)

{
    S->base = (char *)malloc(STACK_INIT_SIZE * sizeof(char));

    if (!S->base)
        return 0;

    S->top = S->base;

    S->stacksize = STACK_INIT_SIZE;

    return 1;
}

int Push(SqStack *S, char e)

{
    if (S->top - S->base >= S->stacksize)

    {
        S->base = (char *)realloc(S->base, (S->stacksize + STACKINCREMENT) * sizeof(char));

        if (!S->base)
            return 0;

        S->top = S->base + S->stacksize;

        S->stacksize += STACKINCREMENT;
    }

    *S->top++ = e;

    return 1;
}

int Pop(SqStack *S, char *e)

{
    if (S->top == S->base)
        return 0;

    *e = *--S->top;

    return 1;
}

void main()
{
    SqStack S;

    char a[4];

    int i;

    InitStack(&S);

    printf("请输入字符:\n");

    for (i = 0; i < 4; i++)

        scanf("%c", &a[i]);

    for (i = 0; i < 4; i++)

        Push(&S, a[i]);

    for (i = 0; i < 4; i++)

        Pop(&S, &a[i]);

    for (i = 0; i < 4; i++)

        printf("%c", a[i]);
}

如有帮助,望采纳!谢谢!

请帮助提问者思考,用解题思路代替直接写代码。