补全IsEmptyStack(SqStack *s), IsFullStack(SqStack *s) ,PushStack(SqStack *s,ElemType e) ,PopStack(SqStack *s), Count(SqStack *s) 给定栈的结构体,试按要求完成栈的判空、判满、出栈、入栈、统计栈元素个数函数功能,并在主函数中完成数组元素倒置。从键盘输入n(n<20),然后输入n个数。倒序输出数值。#include "stdio.h" #include "stdlib.h" #include "string.h" #define OK 1 #define ERROR 0 #define INIT_SIZE 10 #define INCREM 5 typedef int ElemType; typedef struct stack { ElemType *base; ElemType *top; int stacksize; }SqStack; int InitStack(SqStack *s); int PushStack(SqStack *s,ElemType e); ElemType PopStack(SqStack *s); int IsEmptyStack(SqStack *s); int IsFullStack(SqStack *s); int Count(SqStack *s); int main() { SqStack st; int n; ElemType a[21]; int i; InitStack(&st); scanf("%d",&n); if(n<=0) return ERROR; for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) { PushStack(&st,a[i]); } for(i=0;i<n;i++) { a[i]=PopStack(&st); } for(i=0;i<n;i++) printf("%d ",a[i]); return OK; } int InitStack(SqStack *s) { s->base=(ElemType *)malloc(sizeof(ElemType )*INIT_SIZE); if(!s->base) return ERROR; s->top=s->base; s->stacksize=INIT_SIZE; return OK; } int IsEmptyStack(SqStack *s) { } int IsFullStack(SqStack *s) { } int PushStack(SqStack *s,ElemType e) { } ElemType PopStack(SqStack *s) { } int Count(SqStack *s) { }
时间: 2023-06-01 20:01:56 浏览: 115
栈的结构体定义如下:
typedef int ElemType;
typedef struct {
ElemType data[20]; //栈的数组实现
int top; //栈顶指针
}SqStack;
//判空
int IsEmptyStack(SqStack *s){
if(s->top == -1) //栈顶指针为-1表示栈为空
return 1;
else
return 0;
}
//判满
int IsFullStack(SqStack *s){
if(s->top == 19) //栈顶指针为19表示栈已满
return 1;
else
return 0;
}
//入栈
int PushStack(SqStack *s, ElemType e){
if(IsFullStack(s)) //如果栈已满,返回0表示入栈失败
return 0;
else{
s->top++; //栈顶指针+1
s->data[s->top] = e; //将元素e压入栈中
return 1; //返回1表示入栈成功
}
}
//出栈
int PopStack(SqStack *s, ElemType *e){
if(IsEmptyStack(s)) //如果栈为空,返回0表示出栈失败
return 0;
else{
*e = s->data[s->top]; //将栈顶元素存入e中
s->top--; //栈顶指针-1
return 1; //返回1表示出栈成功
}
}
//统计栈元素个数
int Count(SqStack *s){
return s->top + 1; //栈顶指针+1即为栈中元素个数
}
//主函数
int main(){
int n, i, e;
SqStack s;
s.top = -1; //初始化栈顶指针为-1
printf("请输入n(n<20):");
scanf("%d", &n);
printf("请输入%d个数:\n", n);
for(i=0; i<n; i++){
scanf("%d", &e);
PushStack(&s, e); //将元素e入栈
}
printf("倒序输出数值:");
while(!IsEmptyStack(&s)){ //栈非空时循环
PopStack(&s, &e); //出栈并将栈顶元素存入e中
printf("%d ", e); //输出元素e
}
printf("\n");
return 0;
}
阅读全文