#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#define N_STUDENTS 4
typedef struct student_info {
char name[10];
int height;
float weight;
} element;
typedef struct {
element *data; // dataÀº Æ÷ÀÎÅÍ·Î Á¤ÀǵȴÙ.
int capacity; // ÇöÀç Å©±â
int top;
} StackType;
// ½ºÅà »ý¼º ÇÔ¼ö
void init_stack(StackType *s)
{
s->top = -1;
s->capacity = 1;
s->data = (element *)malloc(s->capacity * sizeof(element));
}
// °ø¹é »óÅ °ËÃâ ÇÔ¼ö
int is_empty(StackType *s)
{
return (s->top == -1);
}
// Æ÷È »óÅ °ËÃâ ÇÔ¼ö
int is_full(StackType *s)
{
return (s->top == (s->capacity-1));
}
void push(StackType *s, element item)
{
if (is_full(s)) {
s->capacity *= 2;
s->data =
(element *)realloc(s->data, s->capacity * sizeof(element));
}
s->data[++(s->top)] = item;
}
// »èÁ¦ÇÔ¼ö
element pop(StackType *s)
{
if (is_empty(s)) {
fprintf(stderr, "½ºÅà °ø¹é ¿¡·¯\n");
exit(1);
}
else return s->data[(s->top)--];
}
int main(void)
{
int i;
element e;
StackType s;
init_stack(&s);
for (i = 0; i < N_STUDENTS; i++) {
printf("À̸§: ");
scanf("%s", e.name);
printf("Å°: ");
scanf("%d", &e.height);
printf("¸ö¹«°Ô: ");
scanf("%f", &e.weight);
push(&s,e);
}
for (i = 0; i < N_STUDENTS; i++) {
e = pop(&s);
printf("%s, %d, %f \n", e.name, e.height, e.weight);
}
free(s.data);
return 0;
}