美文网首页
数据结构c语言实现顺序栈

数据结构c语言实现顺序栈

作者: 甜柚小仙女 | 来源:发表于2019-01-27 12:09 被阅读0次

include<stdio.h>

#include<stdlib.h>
#include<string.h>
#include<math.h>
#define MAXSIZE 100
#define ElemType char
typedef struct{
    ElemType data[100];
    int top;
}stack;

void initStack(stack *s){
    s->top=-1;
}
int isEmpty(stack s){
    if(s.top==-1){
        return 1;
    }
    return 0;
}
int isFull(stack s){
    if(s.top==MAXSIZE-1){
        return 1;
    }
    return 0;
} 
void pushStack(stack *s,ElemType ele){
    if(!isFull(*s)){
        s->data[++s->top] = ele;
    }
    else{
        printf("栈已满\n"); 
    }   
}
ElemType popStack(stack *s){
    if(!isEmpty(*s)){
        return s->data[s->top--];
    }
    printf("栈为空\n");
}
int getSize(stack s){
    return s.top+1;
}
int main(){
    stack s;
    int i;
    initStack(&s);
    for(i=0;i<10;i++){
        pushStack(&s,i);
    }   
    printf("%d",popStack(&s));
    printf("%d",popStack(&s));
    printf("%d\n",popStack(&s));
    printf("栈是否为空(1为空):%d\n",isEmpty(s));
    printf("栈是否满(1为满):%d\n",isFull(s));
    printf("栈的深度:%d\n",getSize(s));
    
}

相关文章

网友评论

      本文标题:数据结构c语言实现顺序栈

      本文链接:https://www.haomeiwen.com/subject/iypgjqtx.html