알고리즘

백준 10845번, 간단한 Queue 구현 C++

이온시옥 2019. 1. 12. 14:10
반응형



https://www.acmicpc.net/problem/10845


C++로 클래스를 사용하여 구현

DataNode 클래스와 Queue 클래스를 구분하여 캡슐화 최적화로 구현.

큐에 들어가는 데이터의 타입이 변하더라도 Queue 클래스는 변할 필요가 없음.

명령어를 받는 것은 모두 메인함수에서 구현됨. (strcmp & strtok)






//

//  main.cpp

//  Queue

//

//  Created by Eon on 1/12/19.

//  Copyright © 2019 Eon. All rights reserved.

//


#include <iostream>

#include <cstring>


using namespace std;


class DataNode {

private:

    int number;

    DataNode* next;

    

public:

    DataNode(int value) : number(value), next(NULL){

        //int 타입의 값을 받는 생성자 생성. 그리고 이니셜라이져로 value로 number를 초기화하고 next의 값을 NULL로 초기화함.

    };

    

    void setNumber(int value) {

        number = value;

    }

    void setNext(DataNode* value) {

        next = value;

    }

    

    int getNumber() {

        return number;

    }

    DataNode* getNext() {

        return next;

    }

};


class Queue {

private:

    int size;

    DataNode* front;

    DataNode* back;

    

public:

    Queue() : size(0), front(NULL), back(NULL){

        

    }

    void push(int number) {

        DataNode* newNode = new DataNode(number);

        if (front == NULL) {

            front = newNode;

            back = newNode;

        }

        else {

            back->setNext(newNode);

            back = newNode;

        }

        size++;

    }

    

    int pop() {

        if (!isEmpty()) {

            DataNode* temp = front;

            int value = temp->getNumber();


            if (temp->getNext()) { //front의 next가 존재하면

                front = temp->getNext(); //front의 next를 front로 지정.

            }

            else { //front의 next가 존재하지 않으면

                front = NULL;

                back = NULL;

            }

            size--;

            delete temp;

            return value;

        }

        return -1;

    }

    

    int getSize() {

        return size;

    }

    

    bool isEmpty() {

        if (front == NULL) {

            return true;

        }

        return false;

    }

    

    int GetFrontValue() {

        if (front != NULL) {

            return front->getNumber();

        }

        return -1;

    }

    

    int GetBackValue() {

        if (back != NULL) {

            return back->getNumber();

        }

        return -1;

    }

};














int main(int argc, const char * argv[]) {

    // insert code here...

    int cnt = 0;

    cin >> cnt;

    

    char str[128];

    char* first = NULL;

    cin.ignore();

    

    Queue a_queue;

    

    for (int i=0; i<cnt; i++) {

        cin.getline(str, 128, '\n');

        

        first = strtok(str, " ");

        if (strcmp("push", first) == 0) {

            char* second = strtok(NULL, " ");

            int num = atoi(second);

            a_queue.push(num);

        }

        else if (strcmp("pop", first) == 0) {

            cout << a_queue.pop() << endl;

        }

        else if (strcmp("size", first) == 0) {

            cout << a_queue.getSize() << endl;

        }

        else if (strcmp("empty", first) == 0) {

            cout << a_queue.isEmpty() << endl;

        }

        else if (strcmp("front", first) == 0) {

            cout << a_queue.GetFrontValue() << endl;

        }

        else if (strcmp("back", first) == 0) {

            cout << a_queue.GetBackValue() << endl;

        }

    }

    

    return 0;

}






반응형