/*
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define true 1
#define false 0
#define Status int
typedef struct {
int *base;
int *top;
int stacksize;
} SqStack;
/*
Status SS_Init(SqStack *S, int max);
Status SS_IsFull(SqStack S);
Status SS_IsEmpty(SqStack S);
void SS_Length(SqStack S);
Status SS_push(SqStack *S, int e);
Status SS_pop(SqStack *S, int *e);
*/
Status SS_Init(SqStack *S, int max) {
//S->base = (int *)malloc(max * sizeof(int));
S->base = malloc(max * sizeof(int));
//if(!(S->base)) exit(1);
if(!S->base) exit(1);
S->top = S->base;
S->stacksize = max;
return true;
}
// 为什么不写释放函数?
void stack_deinit(SqStack *s) {
free(s->base);
}
//Status SS_IsFull(SqStack S) {
Status SS_IsFull(SqStack *S) {
//if(S.top - S.base == S.stacksize) {
if(S->top - S->base == S->stacksize) {
return true;
} else {
return false;
}
}
//Status SS_IsEmpty(SqStack S) {
Status SS_IsEmpty(SqStack *S) {
//if(S.top == S.base) {
if(S->top == S->base) {
return true;
} else {
return false;
}
}
//void SS_Length(SqStack S) { printf("Stack length:%d\n", S.top - S.base); }
//void SS_Length(SqStack S) { printf("Stack length:%ld\n", S.top - S.base); }
void SS_Length(SqStack *S) { printf("Stack length:%ld\n", S->top - S->base); }
Status SS_push(SqStack *S, int e) {
/*
int m;
m = SS_IsFull(*S);
*/
//int m = SS_IsFull(*S);
int m = SS_IsFull(S);
//if(!m) {
if(m) {
return false;
} else {
*(S->top) = e;
S->top++;
return true;
}
}
Status SS_pop(SqStack *S, int *e) {
/*
int m;
m = SS_IsEmpty(*S);
*/
//int m = SS_IsEmpty(*S);
int m = SS_IsEmpty(S);
//if(!m) {
if(m) {
return false;
} else {
(S->top)--;
*e = *(S->top);
}
return true;
}
//Status SS_print(SqStack S) {
Status SS_print(SqStack *S) {
/*
int m;
m = SS_IsEmpty(S);
*/
int m = SS_IsEmpty(S);
//if(!m) {
if(m) {
printf("stack data: Empty!\n");
return false;
} else {
//printf("stack data (from bottom to top):");
printf("stack data (from top to bottom): ");
//while(S.top != S.base) {
while(S->top != S->base) {
S->top--;
//printf("%d ", *(S->top));
printf("%d ", *S->top);
}
//printf("%d", S.base);
//printf("%d", *S.base);
//printf("%d", *S->base);
}
printf("\n");
return true;
}
int main() {
SqStack S;
//int max, b, i = 0;
//int b, i = 0;
int b;
//char ch;
char a[10];
/*
scanf("%d", &max);
SS_Init(&S, max);
*/
int temp;
scanf("%d", &temp);
SS_Init(&S, 1024);
while(1) {
scanf("%s", a);
/*
while((ch = getchar()) != ' ') {
a[i] = ch;
i++;
}
*/
if(strcmp(a, "push") == 0) {
scanf("%d", &b);
SS_push(&S, b);
}
if(strcmp(a, "pop") == 0) {
//SS_push(&S, b);
SS_pop(&S, &b);
}
if(strcmp(a, "end") == 0) {
break;
}
}
//SS_Length(S);
SS_Length(&S);
//SS_print(S);
SS_print(&S);
stack_deinit(&S);
return 0;
}
|