顺序栈C语言实现

“`

#include <stdio.h>

#define MAXSIZE 10001
#define ELEMTYPE int
#define STACK_EMPTY -9999
#define N 10

typedef struct stack
{
    ELEMTYPE data[MAXSIZE];
    int top;
} Seq_Stack;


void initStack(Seq_Stack *S);
void push(Seq_Stack *S,ELEMTYPE e);
ELEMTYPE pop(Seq_Stack *S);
int isStackEmpty(Seq_Stack *S);
int isStackFull(Seq_Stack *S);
void printStack(Seq_Stack *S);
int main(void)
{
    Seq_Stack Seq_S;
    initStack(&Seq_S);
    int i;
    for(i=1;i<=N;++i)
    {
        push(&Seq_S,i);
    }
    printStack(&Seq_S);
    return 0;
}

void initStack(Seq_Stack *S)
{
    S->top = -1;
}

int isStackEmpty(Seq_Stack *S)
{

    return S->top == -1;
}
int isStackFull(Seq_Stack *S)
{
    if(S->top == MAXSIZE-1) return 1;
    return 0;

}

void push(Seq_Stack *S,ELEMTYPE e)
{
    if(isStackFull(S)) return;
    S->data[++ S->top] = e;
}
ELEMTYPE pop(Seq_Stack *S)
{
    if(isStackEmpty(S)) return STACK_EMPTY;
    return S->data[S->top --];
}

void printStack(Seq_Stack *S)
{
    while(!isStackEmpty(S))
    {
        printf("%d	",pop(S));
    }
}

···

原文地址:https://www.cnblogs.com/yldf/p/11900173.html