Skip to content

Instantly share code, notes, and snippets.

@chomado
Last active August 29, 2015 14:04
Show Gist options
  • Save chomado/4b5b90001f4a6a5ca524 to your computer and use it in GitHub Desktop.
Save chomado/4b5b90001f4a6a5ca524 to your computer and use it in GitHub Desktop.
Stack実装 配列を使って. (だから要素数は有限, 静的)
#include <stdio.h>
#include <stdlib.h>
#define MAX 256
/* must keep the length of the stack (in the array implementation) */
typedef struct {
size_t size; // スタックのサイズ(要素数)
int items[MAX]; // スタック本体みたいな感じ.要素が入る配列
} Stack;
void init (Stack *stckPtr); // スタック初期化の関数
int isEmpty (const Stack *stckPtr); // スタックが空: 非0, 空じゃない: 0
void push (Stack *stckPtr, int value); // 値valueをpushする関数
int pop (Stack *stckPtr); // popする関数
int peek (const Stack *stckPtr); // popせず,ただ一番上の要素を観測するだけ
void printStack(const Stack *stckPtr); // スタックの現在の状態を表示させる関数
/********** functions **********/
void init(Stack *stckPtr)
{
int i;
stckPtr->size = 0;
for (i=0; i<MAX; i++) {
stckPtr->items[i] = 0;
}
}
int isEmpty(const Stack *stckPtr)
{
return stckPtr->size == 0;
}
void push(Stack *stckPtr, int value)
{
if (stckPtr->size == MAX) { // もう容量がいっぱい(MAX)だったら
fputs("ERROR: stack overflow\n", stderr);
exit(EXIT_FAILURE);
}
else {
stckPtr->items[stckPtr->size++] = value;
printf(":pushed %d onto the stack\n", value);
}
}
int pop(Stack *stckPtr)
{
int value;
if (isEmpty(stckPtr)) { // スタックがすでに空だったら
fputs("ERROR: the stack is empty! (underflow)\n", stderr);
exit(EXIT_FAILURE);
}
else {
value = stckPtr->items[stckPtr->size - 1];
stckPtr->size--;
return value;
}
}
int peek(const Stack *stckPtr)
{
int value;
if (isEmpty(stckPtr)) { // スタックがすでに空だったら
fputs("ERROR: the stack is empty! (underflow)\n", stderr);
exit(EXIT_FAILURE);
}
else {
value = stckPtr->items[stckPtr->size - 1];
return value;
}
}
void printStack(const Stack *stckPtr)
{
printf("\nStack: [");
int i;
for (i = 0; i < stckPtr->size-1; i++) {
printf("%d_", stckPtr->items[i]);
}
printf("%d]\n\n", stckPtr->items[stckPtr->size-1]);
}
/*********** main() function ***********/
int main(void)
{
Stack *stack;
init(stack);
push(stack, 0);
push(stack, 1);
push(stack, 2);
printStack(stack);
printf(":peek %d\n", peek(stack));
printf(":poped %d from the top\n", pop(stack));
printf(":peek %d\n", peek(stack));
printStack(stack);
push(stack, 3);
push(stack, 4);
push(stack, 5);
printf(":peek %d\n", peek(stack));
printStack(stack);
printf(":poped %d from the top\n", pop(stack));
printf(":poped %d from the top\n", pop(stack));
printf(":peek %d\n", peek(stack));
printStack(stack);
push(stack, 6);
push(stack, 7);
push(stack, 8);
printStack(stack);
return 0;
}
@chomado
Copy link
Author

chomado commented Jul 24, 2014

実行結果

% gcc stackByArray.c && ./a.out
:pushed 0 onto the stack
:pushed 1 onto the stack
:pushed 2 onto the stack

Stack: [0_1_2]

:peek 2
:poped 2 from the top
:peek 1

Stack: [0_1]

:pushed 3 onto the stack
:pushed 4 onto the stack
:pushed 5 onto the stack
:peek 5

Stack: [0_1_3_4_5]

:poped 5 from the top
:poped 4 from the top
:peek 3

Stack: [0_1_3]

:pushed 6 onto the stack
:pushed 7 onto the stack
:pushed 8 onto the stack

Stack: [0_1_3_6_7_8]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment