Skip to content

Instantly share code, notes, and snippets.

@hiroshiro
Last active August 29, 2015 14:17
Show Gist options
  • Save hiroshiro/137cdb29d8cfe8846a31 to your computer and use it in GitHub Desktop.
Save hiroshiro/137cdb29d8cfe8846a31 to your computer and use it in GitHub Desktop.
複数のスタックを持てるようにする。
#include <stdbool.h>
#include "stack.h"
static bool isStackFull(const Stack *p) {
return p->top == p->size;
}
static bool isStackEmpty(const Stack *p) {
return p->top == 0;
}
//true: 成功, false: 失敗
bool push(Stack *p, int val) {
if (isStackFull(p)) return false;
p->pBuf[p->top++] = val;
return true;
}
// true: 成功, false: 失敗
bool pop(Stack *p, int *pRet) {
if (isStackEmpty(p)) return false;
*pRet = p->pBuf[--p->top];
return true;
}
#ifndef _STACK_H_
#define _STACK_H_
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
int top;
const size_t size;
int * const pBuf;
} Stack;
bool push(Stack *p, int val);
bool pop(Stack *p, int *pRat);
#define newStack(buf) { \
0, sizeof(buf) / sizeof(int), (buf) \
}
#ifdef __cplusplus
}
#endif
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment