数据结构顺序栈的入栈与出栈
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 100
typedef int ElemType;
typedef struct
{
ElemType data[MaxSize];
int top;
} SqStack;
//进桟
/*已知变量:初始化的栈,数组,数组长度*/
int Push(SqStack *s,ElemType a[],intn){
int i;
if(s->top==MaxSize-1)
return 0;
for(i=0;i<n;i++){
s->top++;
s->data[s->top]=a[i];
}
return 1;
}
//出栈
/*存了数据的栈*/
int Pop(SqStack *s)
{ int e;
if(s->top==-1)
return-1;
e= s->data[s->top];
s->top--;
return e;
}
int main(){
//初始化变量
int i,j;
ElemType arr[4]={3,5,7,8};
SqStack *s;
s=(SqStack*)malloc(sizeof(SqStack));
s->top=-1;
//入栈
Push(s,arr,4);
//出栈
for(i=0;i<4;i++){
printf("%d ",Pop(s));
}
printf("\n");
}
本内容由安康学院"雨季"原创!