Las estructuras de datos mentira - La estructura matriz de pila


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAXSIZE 100
typedef int SElemType;
typedef int Status;
#define ERROR 0
#define TRUE 1
#define OK 1
#define FALSE 0
typedef struct{
    SElemType data[MAXSIZE];
    int top;
}SqStack;


 Status InitStack(SqStack* s){
    s->top=-1;
    for(int i=0;i<MAXSIZE;i++){
        s->data[i]=-1;
    }
}

Status Push(SqStack* s,SElemType e){
    if(s->top==MAXSIZE-1){
        return ERROR;
    }
    s->top++;
    s->data[s->top]=e;
    return OK;
}

Status Pop(SqStack* s,SElemType* e){
    if(s->top==-1){
        return ERROR;
    }
    s->top--;
    *e=s->data[s->top];
    return OK;
}

int main(void){
    SqStack* s=malloc(sizeof(SqStack));
    int data;
    if(InitStack(s)){
        printf("success init stack\n");
    }
    for(int i=0;i<10;i++){
        Push(s,rand()%100+1);
    }
    printf("pushed 10 rand numbers\n");
    for(int i=0;i<10;i++){
        printf("%d ",s->data[i]);
    }
    printf("\n");
    printf("pop all numbers in stack\n");
    for(int i=0;i<10;i++){
        Pop(s,&data);
        printf("%d ",data);
    }
    printf("\n over\n");
    return 0;
}
Se han publicado 19 artículos originales · ganado elogios 1 · vistas 3131

Supongo que te gusta

Origin blog.csdn.net/qq_41603639/article/details/104859373
Recomendado
Clasificación