본문 바로가기
SWE/코테

백준_10866_덱 / queue, string

by S나라라2 2018. 10. 10.
반응형

허헣 쉬운거 풀고싶어서 이거 찾아 풀었음


근데 나름 string 공부하기에 좋았던 것 같음. c++은 버전마다 표준문법?이 있어서 참 어려운 것 같음 ㅠㅠ 어떤 버전은 지원하고 어떤 버전은 지원 안하고,,,


stl 에서 제공하는 queue사용하지 않고 struct 만들어서 sll로 구현했음. 매우 쉬움. string 함수들은 c+14버전으로 작성했음. (stringstream은 아닌가..?)


#include<iostream>
#include<malloc.h>
#include<sstream>
#include<vector>
#include<string> //string.compare

using namespace std;

struct node {
	int data;
	struct node *next;
	struct node *pre;
};
struct node *head=0;
struct node *last=0;
int num_node=0;

// return 1 : 덱이 비어있음
//        0 :      비어있지 않음
int isEmpty() {
	if (head==0) {
		return 1;
	}
	return 0;
}

// from=1 : 덱의 앞에 넣는다
// from=0 : 덱의 뒤에 넣는다
void push(int x, int from) {
	struct node *new_one = (struct node*)malloc(sizeof(struct node));
	new_one->data = x;
	new_one->next = 0;
	new_one->pre = 0;

	num_node++; // 전체 노드의 갯수 증가

	// sll이 비어있는 경우
	if (head == 0) {
		head = new_one;
		last = new_one;
		return;
	}

	// sll이 비어있지 않은 경우
	if (from==1) {  // 앞에서 넣기
		new_one->next = head;
		head->pre = new_one;

		head = new_one;
	}
	else {         // 뒤에서 넣기
		last->next = new_one;
		new_one->pre = last;

		last = new_one;
	}
}

// pop_front, pop_back
// 수를 빼고 출력
// return -1 : 덱이 비어있는 경우
//
// from=1 : 앞에서 값을 뺌
// from=0 : 뒤에서 값을 뺌
int pop(int from) {
	
	// 비어있는 경우
	if (isEmpty()) {
		printf("-1\n");
		return -1;
	}

	num_node--; // 전체 노드의 갯수 감소
	
	// sll 하나만 존재하는 경우
	if (head==last) {
		int temp = head->data;
		struct node *temp_p = head;
		
		head = 0;
		last = 0;
		
		free(temp_p);
		printf("%d\n",temp);
		return temp;
	}

	if (from==1) { // 앞에서 값을 뺌
		int temp = head->data;
		struct node *temp_p = head;
		head = head->next;
		head->pre = 0;
		free(temp_p);

		printf("%d\n", temp);
		return temp;
	}
	else {  // 뒤에서 값을 뺌
		int temp = last->data;
		struct node *temp_p = last;
		last = last->pre;
		last->next = 0;
		free(temp_p);

		printf("%d\n", temp);
		return temp;
	}
}

// front, back
// 수를 출력
// return -1 : 덱이 비어있는 경우
// from=1 : 가장 앞의 값을 출력
// from=0 : 가장 뒤의 값을 출력
int print(int from) {
	// 비어있는 경우
	if (isEmpty()) {
		printf("-1\n");
		return -1;
	}

	if (from == 1) { // 앞에서 값을 출력
		printf("%d\n", head->data);
		return head->data;
	}
	else {  // 뒤에서 값을 뺌
		printf("%d\n", last->data);
		return last->data;
	}
}


int main(void) {

	int n;
	cin >> n;
	
	int nn = n+1;
	while (nn-- > 0) {
		// 한 줄씩 입력 받음
		string line;
		getline(cin, line);

		// 띄어쓰기로 나눔
		stringstream split_stream(line);
		string word;
		while (split_stream >> word) {

			if (word.compare("push_back")==0) {
				int num;
				split_stream >> num;
				push(num, 0);
			}
			else if (word.compare("push_front")==0) {
				int num;
				split_stream >> num;
				push(num, 1);
			}
			else if (word.compare("back")==0) {
				print(0);
			}
			else if (word.compare("front")==0) {
				print(1);
			}
			else if (word.compare("pop_back")==0) {
				pop(0);
			}
			else if (word.compare("pop_front")==0) {
				pop(1);
			}
			else if (word.compare("empty")==0) {
				printf("%d\n", isEmpty());
			}
			else if (word.compare("size")==0) {
				printf("%d\n", num_node);
			}
		}
	}

	return 0;
}
반응형