69,371
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<stdlib.h>
#define STACK_SIZE 4
typedef int KEY;
typedef int DATA;
typedef struct {
KEY key;
DATA data;
} CELL;
static CELL stack[STACK_SIZE];
static CELL *sp;
void init(void){
sp=stack+STACK_SIZE;
}
void push(CELL cell){
if(sp<=stack) {
fprintf(stderr,"stackover\n");
exit(EXIT_FAILURE);
}
①
}
CELL pop(void){
if(sp>=stack+STACK_SIZE) {
fprintf(stderr,"stacknull\n");
exit(EXIT_FAILURE);
}
②
}
void disp(void){
int i;
for(i=0;i<STACK_SIZE;i++)
if(i>=sp-stack)
printf("%d",stack[i].key);
else
printf("-- ");
putchar('\n');
}
main(){
char buff[4];
KEY key;
CELL cell, result;
init();
while(1){
printf("I: push, O: pop?");
gets(buff);
key=atoi(&buff[1]);
switch(buff[0]) {
case'I': case'i':
cell.key=key;
push(cell);
break;
case'O': case'o':
result=pop();
break;
default:
return 0;
}
disp();
}
}
void push(CELL cell){
if(sp<=stack) {
fprintf(stderr,"stackover\n");
exit(EXIT_FAILURE);
}
//①
*--sp=cell;
}
CELL pop(void){
if(sp>=stack+STACK_SIZE) {
fprintf(stderr,"stacknull\n");
exit(EXIT_FAILURE);
}
//②
return *sp++;
}
I: push, O: pop?i
-- -- -- 0
I: push, O: pop?i
-- -- 00
I: push, O: pop?i
-- 000
I: push, O: pop?
I: push, O: pop?i
-- -- -- 0
I: push, O: pop?i
-- -- 00
I: push, O: pop?i
-- 000
I: push, O: pop?o
-- -- 00
I: push, O: pop?o
-- -- -- 0
I: push, O: pop?o
-- -- -- --
I: push, O: pop?