ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 스택
    알고리즘 스터디 01/강의 정리 2022. 11. 21. 14:18


     restricted structure - 스택 큐 덱 등 
     특정 위치에서만 원소를 넣거나 뺄 수  있음
     원소 추가 제거가 모두 O(1)
     제일 상단이 아닌 나머지 원소들의 확인/변경이 원칙적으로 불가능
      

     배열로 구현하는 경우
     int data[] & int index 만 있으면 됨

    #include <iostream>
    
    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();
    }

    '알고리즘 스터디 01 > 강의 정리' 카테고리의 다른 글

      (1) 2022.11.21
      (0) 2022.11.21
    연결리스트  (0) 2022.11.21
    배열  (0) 2022.11.21
    기초 코드 작성요령 1&2  (0) 2022.11.21
준생e