프로그래밍 알고리즘

[정올 1697] 큐(queue)

꾸준한사람 2023. 1. 5. 04:29
반응형

http://www.jungol.co.kr/bbs/board.php?bo_table=pbank&wr_id=970&sca=99&sfl=wr_hit&stx=1697 

 

JUNGOL

 

www.jungol.co.kr

#include <stdio.h>

struct Node {
	Node* pPrev, *pNext;
	int my;
	Node() {}
	Node(Node* prev, Node* next, int n) : pPrev(prev), pNext(next), my(n) {
		prev->pNext = this; next->pPrev = this;
	}
} H, T;
int N, cnt;
Node* pCur = &H;

void enque(int n) {
	new Node(T.pPrev, &T, n);
	cnt++;
}

int deque() {
	Node* tmp = H.pNext;
	if (tmp == &T) return -1;
	cnt--;
	H.pNext = tmp->pNext;
	tmp->pNext->pPrev = &H;
	int r = tmp->my;
	delete tmp;
	return r;
}

int main(void) {
	char c;
	H.pNext = &T;
	T.pPrev = &H;
	scanf("%d", &N);
	while (N--) {
		scanf(" %c", &c);
		if (c == 'o') {
			int r = deque();
			if (r == -1) 	printf("empty\n");
			else			printf("%d\n", r);	
		}
		else if (c == 'c') {
			printf("%d\n", cnt);
		}
		else if (c == 'i') {
			int n;
			scanf(" %d", &n);
			enque(n);
		}
	}

	return 0;
}
반응형