Skip to content

Instantly share code, notes, and snippets.

@hiroshiro
Last active August 29, 2015 14:17
Show Gist options
  • Save hiroshiro/da603aa98e8df9743006 to your computer and use it in GitHub Desktop.
Save hiroshiro/da603aa98e8df9743006 to your computer and use it in GitHub Desktop.
古典的なスタック実装
#include <stdbool.h>
#include "stack.h"
int buf[16];
int top = 0;
static bool isStackFull(void) {
return top == sizeof(buf) / sizeof(int);
}
static bool isStackEmpty(void) {
return top == 0;
}
//true: 成功, false: 失敗
bool push(int val) {
if (isStackFull()) return false;
buf[top++] = val;
return true;
}
// true: 成功, false: 失敗
bool pop(int *pRet) {
if (isStackEmpty()) return false;
*pRet = buf[--top];
return true;
}
#ifndef _STACK_H_
#define _STACK_H_
bool push(int val);
bool pop(int *pRat);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment