관리 메뉴

Storage Gonie

(1) [C++, Java] 백준 No.10845 : 큐(기본구현) 본문

알고리즘/백준풀이3. 큐

(1) [C++, Java] 백준 No.10845 : 큐(기본구현)

Storage Gonie 2019. 4. 25. 14:02
반응형

문제

풀이

# C++(직접구현)

#include <iostream>

using namespace std;

struct Queue{
    int data[10000]; // 입력의 조건을 보고 충분한 크기로 해줘야 채점시 런타임에러가 발생하지 않는다.
    int begin, end;

    // 멤버변수를 초기화 해주는 생성자(필수)
    Queue(){
        begin = 0;
        end = 0;
    }

    void push(int num)
    {
        data[end] = num;
        end += 1;
    }

    int pop()
    {
        if (empty())
            return -1;
        else
        {
            begin += 1;
            return data[begin-1];
        }
    }

    int size()
    {
        return end - begin;
    }

    bool empty()
    {
        return (size() == 0) ? 1 : 0;
    }

    int front()
    {
        if (empty())
            return -1;
        else
            return data[begin];
    }

    int back()
    {
        if (empty())
            return -1;
        else
            return data[end-1];
    }
};

int main(void)
{
    ios::sync_with_stdio(false);

    int N;
    cin >> N;

    Queue q;

    while(N--){
        string cmd;
        cin >> cmd;

        if (cmd == "push"){
            int num;
            cin >> num;
            q.push(num);
        }

        else if (cmd == "pop")
        {
            cout << q.pop() << endl;
        }

        else if (cmd == "size")
        {
            cout << q.size() << endl;
        }

        else if (cmd == "empty")
        {
            cout << q.empty() << endl;
        }

        else if (cmd == "front")
        {
            cout << q.front() << endl;
        }

        else if (cmd == "back")
        {
            cout << q.back() << endl;
        }
    }
}

# C++(STL사용)

- 주의! 
   STL의 pop() 메소드는 데이터를 빼버리는 역할로만 사용해야지 반환받은걸 사용하려 하려는 목적으로 사용하면 안된다.
   pop했을 때 나오는 데이터를 얻고자 한다면 pop() 메소드 호출 전에 front() 메소드로 데이터를 얻는 방식으로 해야 한다.

#include <iostream>
#include <queue>

using namespace std;

int main(void)
{
    ios::sync_with_stdio(false);

    int N;
    cin >> N;

    queue<int> q;

    while(N--){
        string cmd;
        cin >> cmd;

        if (cmd == "push"){
            int num;
            cin >> num;
            q.push(num);
        }

        else if (cmd == "pop")
        {
            if (!q.empty()){
                cout << q.front() << endl;
                q.pop();
            }
            else
                cout << -1 << endl;
        }

        else if (cmd == "size")
        {
            cout << q.size() << endl;
        }

        else if (cmd == "empty")
        {
            cout << q.empty() << endl;
        }

        else if (cmd == "front")
        {
            if (!q.empty())
                cout << q.front() << endl;
            else
                cout << -1 << endl;
        }

        else if (cmd == "back")
        {
            if (!q.empty())
                cout << q.back() << endl;
            else
                cout << -1 << endl;
        }
    }
}

 

# Java

import java.util.*;

public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();
        Queue<Integer> queue = new LinkedList<Integer>();
        for (int k=0; k<n; k++) {
            String line = sc.nextLine();
            String[] s = line.split(" ");
            String cmd = s[0];
            if (cmd.equals("push")) {
                int num = Integer.parseInt(s[1]);
                queue.offer(num);
            } else if (cmd.equals("front")) {
                if (queue.isEmpty()) {
                    System.out.println("-1");
                } else {
                    System.out.println(queue.peek());
                }
            } else if (cmd.equals("size")) {
                System.out.println(queue.size());
            } else if (cmd.equals("empty")) {
                if (queue.isEmpty()) {
                    System.out.println("1");
                } else {
                    System.out.println("0");
                }
            } else if (cmd.equals("pop")) {
                if (queue.isEmpty()) {
                    System.out.println("-1");
                } else {
                    System.out.println(queue.poll());
                }
            } else if (cmd.equals("back")) {
                if (queue.isEmpty()) {
                    System.out.println("-1");
                } else {
                    // 구현할 수가 없음... Queue에 없음
                }
            }
        }
    }
}

 

반응형
Comments