[πŸ“—cμ–Έμ–΄ μ‰½κ²Œ ν’€μ–΄μ“΄ 자료ꡬ쑰4] μŠ€νƒ (stack)

μ•ˆμ§€μˆ˜Β·2023λ…„ 2μ›” 7일
0

πŸ‘‘ μŠ€νƒμ΄λž€?

: ν›„μž…μ„ μΆœ (Last-in First out)

  • μŠ€νƒ 상단: μž…μΆœλ ₯ μ΄λ£¨μ–΄μ§€λŠ” λΆ€λΆ„ (top)
  • μŠ€νƒ ν•˜λ‹¨: μš”μ†Œκ°€ κ°€μž₯ λ¨Όμ € μŒ“μ΄λŠ” λΆ€λΆ„
  • 곡백 μŠ€νƒ: μŠ€νƒμ— μš”μ†Œκ°€ ν•˜λ‚˜λ„ 없을 λ•Œ

βœ” μŠ€νƒμ˜ 좔상 μžλ£Œν˜•
1. is_sull(s): μŠ€νƒμ΄ 가득 μ°¨ μžˆλŠ” 지 확인(top==μŠ€νƒ μ‚¬μ΄μ¦ˆ)
2. is_empty(s): μŠ€νƒμ΄ λΉ„μ–΄ μžˆλŠ” 지 확인 (top==-1)
3. push(s, item): μŠ€νƒ 맨 μœ„μ— item을 μΆ”κ°€, top을 μ¦κ°€μ‹œν‚€κ³  λ„£κΈ°
4. pop(s): μŠ€νƒ 맨 μœ„μ˜ μš”μ†Œλ₯Ό μ œκ±°ν•΄μ„œ λ°˜ν™˜, λ°˜ν™˜ν•˜κ³  top을 κ°μ†Œ
5. peek(s): μŠ€νƒ 맨 μœ„μ˜ μš”μ†Œλ₯Ό μ œκ±°ν•˜μ§€ μ•Šκ³  λ°˜ν™˜

πŸ‘‘ μŠ€νƒμ˜ κ΅¬ν˜„

1. μ „μ—­ λ³€μˆ˜λ‘œ κ΅¬ν˜„

#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;
}

2. μŠ€νƒμ˜ μš”μ†Œλ₯Ό ꡬ쑰체둜 ν•˜μ—¬ κ΅¬ν˜„

#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;
}

3. κ΄€λ ¨ 데이터λ₯Ό ν•¨μˆ˜μ˜ λ§€κ°œλ³€μˆ˜λ‘œ 전달 (μ—¬λŸ¬ 개의 μŠ€νƒ λ§Œλ“€κΈ°μ— 용이)

#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자료ꡬ쑰 μ±… μ°Έκ³ 

1. κ΄„ν˜Έ 검사 문제

: μ™Όμͺ½ κ΄„ν˜Έ λ§Œλ‚˜λ©΄ μŠ€νƒμ— μ‚½μž…, 였λ₯Έμͺ½ κ΄„ν˜Έ λ§Œλ‚˜λ©΄ μ°¨λ‘€λ‘œ ν•˜λ‚˜μ”© κΊΌλ‚΄μ„œ 검사. λ§ˆμ§€λ§‰μ— μŠ€νƒμ— κ΄„ν˜Έ λ‚¨μ•„μžˆμœΌλ©΄, 짝이 λ§žμ§€ μ•ŠλŠ” κ²ƒμ΄λ―€λ‘œ 였λ₯˜

2. ν›„μœ„ ν‘œκΈ° μˆ˜μ‹μ˜ 계산

: 숫자(ν”Όμ—°μ‚°μž)λŠ” μŠ€νƒμ— μ €μž₯. μ—°μ‚°μž λ‚˜μ˜€λ©΄ μŠ€νƒμ—μ„œ 숫자 κΊΌλ‚΄μ„œ 계산 ν›„, 결괏값 λ‹€μ‹œ μŠ€νƒμ— μ €μž₯

3. μ€‘μœ„ ν‘œκΈ°μ‹-> ν›„μœ„ ν‘œκΈ°μ‹

: μ—°μ‚°μžλ₯Ό μŠ€νƒμ— μ €μž₯.
: ν”Όμ—°μ‚°μžλŠ” 좜λ ₯
.
: μš°μ„ μˆœμœ„ μž‘κ±°λ‚˜ 같은 μ—°μ‚°μžκ°€ 큰 μ—°μ‚°μž μœ„μ— 올 수 μ—†μŒ. (ex> κ°€ μŠ€νƒμ— λ“€μ–΄μžˆκ³ , +을 μŠ€νƒμ— λ„£λŠ” 경우: 을 λΉΌμ„œ 좜λ ₯ ν›„, +을 λ„£μ–΄μ€˜μ•Ό 함),
: μ™Όμͺ½ κ΄„ν˜ΈλŠ” 무쑰건 μŠ€νƒμ—.
: 였λ₯Έμͺ½ κ΄„ν˜Έ-> μ™Όμͺ½ κ΄„ν˜Έ μ‚­μ œλ  λ•ŒκΉŒμ§€ μŠ€νƒμ— μžˆλŠ” μ—°μ‚°μžλ“€ pop

β­• TIL (Today I learned)

& μŠ€νƒ μžλ£Œκ΅¬μ‘°λŠ” ν›„μž…μ„ μΆœλ‘œ, λ‚˜μ€‘μ— λ“€μ–΄κ°„ 것이 κ°€μž₯ λ¨Όμ € λ‚˜μ˜€λŠ” μžλ£Œκ΅¬μ‘°μ΄λ‹€. pushμ—μ„œλŠ” top을 μ¦κ°€μ‹œν‚€κ³  itemλ„£κΈ°, popμ—μ„œλŠ” λ°˜ν™˜ ν›„ top μ¦κ°€μ‹œν‚€κΈ°. μžŠμ§€ 말자!!
ꡬ쑰체 μ•ˆμ— μŠ€νƒ 포인터(동적 ν• λ‹Ή)λ₯Ό μ§€μ •ν•œ κ²½μš°κ°€ λ¨Έλ¦Ώμ†μœΌλ‘œ 잘 그렀지지 μ•Šμ•˜λ‹€. ꡬ쑰체 μ•ˆμ˜ μŠ€νƒ ν¬μΈν„°λŠ” 동적할당할 λ©”λͺ¨λ¦¬λ₯Ό 가리킀고 μžˆλŠ” 것이닀. 즉, μŠ€νƒμ„ 가리킀고 μžˆλŠ” 것! μ½”λ“œ 뢄석할 λ•Œ λ¨Έλ¦Ώμ†μœΌλ‘œ κ·Έλ¦¬λ©΄μ„œ μƒκ°ν•΄λ³΄μž

profile
μ§€μˆ˜μ˜ μ·¨μ€€, 개발일기

0개의 λŒ“κΈ€