포스트

자료구조

자료구조

📌 1. 스택

LIFO (last input first out)

1
2
3
4
5
6
7
8
9
10
#define MAX 100
int stack[MAX] = { 0 };
int stack_index = 0;

void push(int new){
  stack[stack_index++] = new;
}
int pop(){
  return stack[--stack_index];
}

📌 2. 큐

FIFO (first input first out)

1
2
3
4
5
6
7
8
9
10
#define MAX 100
int queue[MAX] = { 0 };
int front = 0, rear = 0;

void enqueue(int new){
  queue[rear++] = new;
}
int dequeue(){
  return queue[front++];
}

📌 3. 트리

1) 배열 기반 트리
노드의 위치 규칙

  • 부모 노드가 i일 떄,
    • 왼쪽 자식 : 2i + 1
    • 오른쪽 자식 : 2i + 2