Last active
August 29, 2015 14:17
-
-
Save hiroshiro/137cdb29d8cfe8846a31 to your computer and use it in GitHub Desktop.
複数のスタックを持てるようにする。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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