c语言写一个数组栈

长度为20,要求有入栈,出栈,判断满了没,三个函数。输入小于20的字符后,把每个字符入栈,不包括空格。如果用户输入了“^"符号,则停止入栈。

谢谢
然后要把栈里的元素从下面到上面打印出来

#include <stdio.h>
#include <stdlib.h>
#define Size 20

typedef char ElementType;
typedef struct
{
    ElementType    data[Size];
    int top;
}Stack;    

int  StackFull(Stack *s)
{
    return (s->top==Size-1);
}

void PushStack(Stack *s, char *buf)
{
    if(NULL==s || NULL==buf)
        return;
    while( *buf!='\0' && *buf!='^' && !StackFull(s))
    {
        if(*buf != ' ')
            s->data[++s->top] = *buf;
        ++buf;    
    }
}

void PopStack(Stack *s)
{
    if(NULL == s)
        return;
    while(s->top != -1)
        putchar(s->data[s->top--]);
}

int main()
{
    char str[Size];
    Stack *s = (Stack*)malloc(sizeof(Stack));
    s->top = -1;
    
    gets(str);
    PushStack(s, str);
    PopStack(s);
                  
    return 0;
}

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