: νμ μ μΆ (Last-in First out)
β μ€νμ μΆμ μλ£ν
1. is_sull(s): μ€νμ΄ κ°λ μ°¨ μλ μ§ νμΈ(top==μ€ν μ¬μ΄μ¦)
2. is_empty(s): μ€νμ΄ λΉμ΄ μλ μ§ νμΈ (top==-1)
3. push(s, item): μ€ν 맨 μμ itemμ μΆκ°, topμ μ¦κ°μν€κ³ λ£κΈ°
4. pop(s): μ€ν 맨 μμ μμλ₯Ό μ κ±°ν΄μ λ°ν, λ°ννκ³ topμ κ°μ
5. peek(s): μ€ν 맨 μμ μμλ₯Ό μ κ±°νμ§ μκ³ λ°ν
#include <stdio.h>
#include <stdlib.h>
#define MAX_STACK_SIZE 100
typedef int element;
element stack[MAX_STACK_SIZE];
int top = -1;
//곡백 μν κ²μΆ ν¨μ
int is_empty() {
return (top == -1);
}
//ν¬ν μν κ²μΆ ν¨μ
int is_full() {
return (top == MAX_STACK_SIZE - 1);
}
//μ½μ
ν¨μ
void push(element item) {
if (is_sull()) {
fprintf(stderr, "μ€ν ν¬ν μλ¬\n");
return;
}
else {
stack[++top] = item;
}
}
//μμ ν¨μ
element pop() {
if (is_empty()) {
fprintf(stderr, "μ€ν 곡백 μλ¬\n");
exit(1);
}
else {
return stack[top--];
}
}
//νΌν¬ ν¨μ
element peek() {
if (is_empty()) {
fprintf(stderr, "μ€ν 곡백 μλ¬\n");
exit(1);
}
else {
return stack[top];
}
}
int main(void)
{
push(1);
push(2);
push(3);
printf("%d\n", pop());
printf("%d\n", pop());
printf("%d\n", pop());
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define MAX_STACK_SIZE 100
#define MAX_STRING 100
typedef struct {
int student_no;
char name[MAX_STACK_SIZE];
char address[MAX_STRING];
}element;
element stack[MAX_STACK_SIZE];
int top = -1;
//곡백 μν κ²μΆ ν¨μ
int is_empty() {
return (top == -1);
}
//ν¬ν μν κ²μΆ ν¨μ
int is_full() {
return (top == MAX_STACK_SIZE - 1);
}
//μ½μ
ν¨μ
void push(element item) {
if (is_sull()) {
fprintf(stderr, "μ€ν ν¬ν μλ¬\n");
return;
}
else {
stack[++top] = item;
}
}
//μμ ν¨μ
element pop() {
if (is_empty()) {
fprintf(stderr, "μ€ν 곡백 μλ¬\n");
exit(1);
}
else {
return stack[top--];
}
}
//νΌν¬ ν¨μ
element peek() {
if (is_empty()) {
fprintf(stderr, "μ€ν 곡백 μλ¬\n");
exit(1);
}
else {
return stack[top];
}
}
int main(void)
{
element ie = { 20190001, "Hong", "Seoul" };
element oe;
push(ie);
oe = pop();
printf("νλ²: %d\n", oe.student_no);
printf("μ΄λ¦: %s\n", oe.name);
printf("μ£Όμ: %s\n", oe.address);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define MAX_STACK_SIZE 100
typedef int element;
typedef struct {
element data[MAX_STACK_SIZE];
int top;
}StackType;
//μ€ν μ΄κΈ°ν ν¨μ
void init_stack(StackType* s) {
s->top = -1;
}
//곡백 μν κ²μΆ ν¨μ
int is_empty(StackType* s) {
return (s->top == -1);
}
//ν¬ν μν κ²μΆ ν¨μ
int is_full(StackType* s) {
return (s->top == MAX_STACK_SIZE - 1);
}
//μ½μ
ν¨μ
void push(StackType* s, element item) {
if (is_sull(s)) {
fprintf(stderr, "μ€ν ν¬ν μλ¬\n");
return;
}
else {
s->data[++(s->top)] = item;
}
}
//μμ ν¨μ
element pop(StackType* s) {
if (is_empty(s)) {
fprintf(stderr, "μ€ν 곡백 μλ¬\n");
exit(1);
}
else {
return s->data[(s->top)--];
}
}
//νΌν¬ ν¨μ
element peek(StackType* s) {
if (is_empty(s)) {
fprintf(stderr, "μ€ν 곡백 μλ¬\n");
exit(1);
}
else {
return s->data[s->top];
}
}
int main(void)
{
StackType s;
init_stack(&s);
push(&s, 1);
push(&s, 2);
push(&s, 3);
printf("%d\n", pop(&s));
printf("%d\n", pop(&s));
printf("%d\n", pop(&s));
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define MAX_STACK_SIZE 100
typedef int element;
typedef struct {
element* 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_sull(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)--];
}
}
//νΌν¬ ν¨μ
element peek(StackType* s) {
if (is_empty(s)) {
fprintf(stderr, "μ€ν 곡백 μλ¬\n");
exit(1);
}
else {
return s->data[s->top];
}
}
int main(void)
{
StackType s;
init_stack(&s);
push(&s, 1);
push(&s, 2);
push(&s, 3);
printf("%d\n", pop(&s));
printf("%d\n", pop(&s));
printf("%d\n", pop(&s));
free(s.data);
return 0;
}
: μμ€μ½λλ μ½κ² νμ΄μ΄ cμλ£κ΅¬μ‘° μ± μ°Έκ³
: μΌμͺ½ κ΄νΈ λ§λλ©΄ μ€νμ μ½μ , μ€λ₯Έμͺ½ κ΄νΈ λ§λλ©΄ μ°¨λ‘λ‘ νλμ© κΊΌλ΄μ κ²μ¬. λ§μ§λ§μ μ€νμ κ΄νΈ λ¨μμμΌλ©΄, μ§μ΄ λ§μ§ μλ κ²μ΄λ―λ‘ μ€λ₯
: μ«μ(νΌμ°μ°μ)λ μ€νμ μ μ₯. μ°μ°μ λμ€λ©΄ μ€νμμ μ«μ κΊΌλ΄μ κ³μ° ν, κ²°κ΄κ° λ€μ μ€νμ μ μ₯
: μ°μ°μλ₯Ό μ€νμ μ μ₯.
: νΌμ°μ°μλ μΆλ ₯.
: μ°μ μμ μκ±°λ κ°μ μ°μ°μκ° ν° μ°μ°μ μμ μ¬ μ μμ. (ex> κ° μ€νμ λ€μ΄μκ³ , +μ μ€νμ λ£λ κ²½μ°: μ λΉΌμ μΆλ ₯ ν, +μ λ£μ΄μ€μΌ ν¨),
: μΌμͺ½ κ΄νΈλ 무쑰건 μ€νμ.
: μ€λ₯Έμͺ½ κ΄νΈ-> μΌμͺ½ κ΄νΈ μμ λ λκΉμ§ μ€νμ μλ μ°μ°μλ€ pop
& μ€ν μλ£κ΅¬μ‘°λ νμ
μ μΆλ‘, λμ€μ λ€μ΄κ° κ²μ΄ κ°μ₯ λ¨Όμ λμ€λ μλ£κ΅¬μ‘°μ΄λ€. pushμμλ topμ μ¦κ°μν€κ³ itemλ£κΈ°, popμμλ λ°ν ν top μ¦κ°μν€κΈ°. μμ§ λ§μ!!
ꡬ쑰체 μμ μ€ν ν¬μΈν°(λμ ν λΉ)λ₯Ό μ§μ ν κ²½μ°κ° λ¨Έλ¦ΏμμΌλ‘ μ κ·Έλ €μ§μ§ μμλ€. ꡬ쑰체 μμ μ€ν ν¬μΈν°λ λμ ν λΉν λ©λͺ¨λ¦¬λ₯Ό κ°λ¦¬ν€κ³ μλ κ²μ΄λ€. μ¦, μ€νμ κ°λ¦¬ν€κ³ μλ κ²! μ½λ λΆμν λ λ¨Έλ¦ΏμμΌλ‘ 그리면μ μκ°ν΄λ³΄μ