바킹독님의 실전알고리즘배우기 5강듣고 요약

바킹독의 실전 알고리즘 5강 링크

image
image

스택의 성질

image

스택의 구현

#include <bits/stdc++.h>
using namespace std;

const int MX = 1000005;
int dat[MX];
int pos = 0;

void push(int x){
  dat[pos++] = x;
}

void pop(){
  pos--;
}

int top(){
  return dat[pos-1];
}

void test(){
  push(5); push(4); push(3);
  cout << top() << '\n'; // 3
  pop(); pop();
  cout << top() << '\n'; // 5
  push(10); push(12);
  cout << top() << '\n'; // 12
  pop();
  cout << top() << '\n'; // 10
}

int main(void) {
	test();
}

스택은 구현이 쉽다.
그래도 물론 STL의 stack을 이용하는 것이 좋다.

STL stack

image
STL stack의 기본적인 함수로는 push(), pop(), top(), size(), empty()가 있다.
stack이 비어있는데 pop()이나 top()을 호출하면 runtime error가 발생한다.

연습문제

BOJ 10828번