#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define STACK_SIZE 100
/**** STACK ****/
/* external variables */
int contents[STACK_SIZE];
int *top_ptr = &contents[0];
void make_empty(void) {
*top_ptr = 0;
}
bool is_empty(void) {
return top_ptr == &contents[0];
}
bool is_full(void) {
return top_ptr == &contents[STACK_SIZE];
}
void stack_underflow(void) {
printf("stack underflow.\n");
exit(EXIT_FAILURE);
}
void stack_overflow(void) {
printf("stack overflow.\n");
exit(EXIT_FAILURE);
}
void push(int i) {
if (is_full())
stack_overflow();
else
*top_ptr++ = i;
}
int pop(void) {
if (is_empty())
stack_underflow();
else
return *--top_ptr;
}
stack(포인터 버전)
2016. 11. 7. 21:15