#include <stdio.h>
#define SIZE 5
int stack[SIZE];
int sp;
void push(int value);
int pop(void);
int main(void)
{
sp = 0;
int resp, data;
while(1){
printf("1:push, 2:pop, 0:end : ");
if(!resp) break;
switch(resp){
push(data);
break;
case 2: pop();
break;
}
}
for(int i=0; i<sp; i++){
printf("stack[%d]=%d \n", i
, stack
[i
]); }
return 0;
}
void push(int value)
{
if(sp >= SIZE){
}else{
stack[sp++] = value;
}
}
int pop(void)
{
if(sp<= 0){
printf("スタックが空で取り出せませんでした。\n"); return 0;
}else{
return stack[--sp];
}
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNkZWZpbmUgU0laRSA1CmludCBzdGFja1tTSVpFXTsKaW50IHNwOwoKdm9pZCBwdXNoKGludCB2YWx1ZSk7CgppbnQgcG9wKHZvaWQpOwoKaW50IG1haW4odm9pZCkKCnsKICBzcCA9IDA7CiAgaW50IHJlc3AsIGRhdGE7CgogIHdoaWxlKDEpewogICAgcHJpbnRmKCIxOnB1c2gsIDI6cG9wLCAwOmVuZCA6ICIpOwogICAgc2NhbmYoIiVkIiwgJnJlc3ApOwoKICAgIGlmKCFyZXNwKSBicmVhazsKCiAgICBzd2l0Y2gocmVzcCl7CgogICAgICBjYXNlIDE6IHByaW50ZigicHVzaCA6ICIpOyBzY2FuZigiJWQiLCAmZGF0YSk7CiAgICAgICAgICAgICAgcHVzaChkYXRhKTsKICAgICAgICAgICAgICBicmVhazsKICAgICAgY2FzZSAyOiBwb3AoKTsKICAgICAgICAgICAgICBicmVhazsKICAgIH0KICAgIHByaW50Zigic3A9JWRcbiIsc3ApOwogIH0KICBwcmludGYoIlxuIik7CiAgZm9yKGludCBpPTA7IGk8c3A7IGkrKyl7CiAgICBwcmludGYoInN0YWNrWyVkXT0lZCBcbiIsIGksIHN0YWNrW2ldKTsKICB9CgogIHJldHVybiAwOwp9CgoKICB2b2lkIHB1c2goaW50IHZhbHVlKQogICAgewogICAgaWYoc3AgPj0gU0laRSl7CiAgICBwcmludGYoIuOCueOCv+ODg+OCr+OBjOa6gOadr+OBp+WFpeOCiuOBvuOBm+OCk+OBp+OBl+OBn1xuIik7CiAgICB9ZWxzZXsKICAgICAgc3RhY2tbc3ArK10gPSB2YWx1ZTsKCiAgICB9IAogIH0KICAKICBpbnQgcG9wKHZvaWQpCiAgewogICAgaWYoc3A8PSAwKXsKCiAgICAgIHByaW50Zigi44K544K/44OD44Kv44GM56m644Gn5Y+W44KK5Ye644Gb44G+44Gb44KT44Gn44GX44Gf44CCXG4iKTsKICAgICAgcmV0dXJuIDA7CiAgICB9ZWxzZXsKICAgICAgcmV0dXJuIHN0YWNrWy0tc3BdOwogICAgfQogIH0K
1:push, 2:pop, 0:end : push : sp=1
1:push, 2:pop, 0:end : push : sp=2
1:push, 2:pop, 0:end : push : sp=3
1:push, 2:pop, 0:end : push : sp=4
1:push, 2:pop, 0:end : push : sp=5
1:push, 2:pop, 0:end : push : スタックが満杯で入りませんでした
sp=5
1:push, 2:pop, 0:end :
stack[0]=1
stack[1]=2
stack[2]=3
stack[3]=4
stack[4]=5