스택

- 제일 나중에 들어온 원소가 제일 먼저 나가는 구조 ( LIFO - Last In First Out )

- 알고리즘보다는 자료구조에 가까운 개념이다.

  -> 알고리즘은 이러한 스택이나 큐를 이용해서 문제를 해결하는 방법이다.

 

#include<iostream>
#include<stack>

using namespace std;

int main(){
  stack<int> s;
  s.push(7);
  s.push(5);
  s.push(4);
  s.pop();
  s.push(6);
  s.pop();

  while(!s.empty()){
    cout << s.top() << " ";
    s.pop();
  }
}

- STL 라이브러리 문법을 이용해 사용한다.

- 직접 구현해 볼 수 있지만 너무 간단하므로 생략하기로 한다.

 


- 들어온 것이 먼저 나가는 구조 ( FIFO - First In First Out )

 

#include<iostream>
#include<queue>

using namespace std;

int main(){
  queue<int> q;
  q.push(7);
  q.push(5);
  q.push(4);
  q.pop();
  q.push(6);
  q.pop();

  while(!q.empty()){
    cout << q.front() << " ";
    q.pop();
  }
}

- STL 라이브러리 문법을 이용해 사용한다.

- 스택과 마찬가지로 직접 구현해 볼 수 있지만 너무 간단하므로 생략하기로 한다.

'알고리즘' 카테고리의 다른 글

깊이 우선 탐색 ( DFS )  (0) 2022.02.20
너비 우선 탐색 ( BFS )  (0) 2022.02.15
계수 정렬 ( Counting Sort )  (0) 2022.02.15
힙 정렬 ( Heap sort )  (0) 2022.02.11
C++ STL sort() 함수 다루기  (0) 2022.02.10

+ Recent posts