进出栈

来源:互联网 发布:福建 知乎 编辑:程序博客网 时间:2024/04/29 18:57

进出栈代码如下

#include<stdio.h>#include<stdlib.h>#define M 100typedef struct {    int data[M];    int top;}SqStack; //初始化 int InitSqStack(SqStack *S){    S->top=-1;    return 1;}//压栈int PushSqStack(SqStack *S,int e){    if(S->top==M-1)    {        return 0;    }    S->top++;    S->data[S->top]=e;    return 1;} //出栈int PopSqStack(SqStack *S,int *e){    if(S->top==-1)    {        return 0;    }    *e=S->data[S->top--];    return 1;}int main(){    SqStack S;    int e,j;    InitSqStack(&S);    //以下是测试    PushSqStack(&S,4);    PushSqStack(&S,8);    PushSqStack(&S,10);    PushSqStack(&S,78);    PushSqStack(&S,49);    for(j=0;j<M;j++)    {        if(PopSqStack(&S,&e)){            printf("%d ",e);        }    }    printf("\n");    return 0;} 

这里写图片描述

原创粉丝点击