顺序栈C语言实现

来源:互联网 发布:拖欠淘宝贷款后果 编辑:程序博客网 时间:2024/04/30 00:08

“`

#include <stdio.h>#define MAXSIZE 10001#define ELEMTYPE int#define STACK_EMPTY -9999#define N 10typedef 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\t",pop(S));    }}

···

0 0
原创粉丝点击