반응형

 

 

 

1. Stack

- 선언

stack <char> s1;

 

- push

s1.push('c');

 

- pop

s1.pop();

 

- 스택 가장 위의 값

s1.top();

 

- 예제

#include<string>
#include <iostream>
#include <stack>
using namespace std;

bool solution(string s)
{
    bool answer = true;
    stack <char> s1;
    
    for(int i=0; i<s.length(); i++){
        if(s[i] == '('){
            s1.push(s[i]);
        } else if(s[i] == ')'){
            if(s1.empty() || s1.top() == ')'){
                answer = false;
                break;
            }
            s1.pop();
        }
    }
    
    if(!s1.empty()){
        answer = false;
    }
    
    
    return answer;
}

 

 

 

 

 

 

2. Queue

- 선언

Queue<int> q;

 

- push

q.push(1);

 

- pop

q.pop();

 

- 큐 맨 앞의 값

q.front();

 

- 큐 맨 뒤의 값

q.back();

 

- 비었는지 체크

q.empty();

 

- 큐에 몇 개의 값이 들었는지

q.size();

 

 

 

 

 

반응형

+ Recent posts