티스토리 뷰
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;
}
'알고리즘' 카테고리의 다른 글
백준 2339번 설탕배달 (0) | 2019.02.28 |
---|---|
백준 - 14502 연구소 (0) | 2019.02.15 |
OS - Thread & Synchronization (0) | 2019.01.12 |
OS - Kernel Compile, kernel Message, System Call, Kernel Module (0) | 2019.01.11 |
C++ 상속 기본개념 & 문법 (0) | 2019.01.05 |
- Total
- Today
- Yesterday
- 문제
- 백준
- 모바일 프로그래밍
- C++
- python
- 개발
- 환경오염
- 카카오톡
- 우분투
- eclipse
- 자바
- 서버
- 알고리즘
- 코드업
- 파이썬
- 이클립스
- se846
- 외장그래픽
- 슈어
- 산업화
- Java
- 프로그래밍
- 코딩
- CodeUp
- 아키티오
- egpu
- NAS
- Vega64
- 맥북
- 맥
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |