본문 바로가기

데이터구조/소스코드

OJ 1161

반응형
  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
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* stack[100];
int top = -1;

char* queue[100];
int front = 0;
int rear = 0;

void enqueue(char* _data)
{
	queue[rear] = _data;
	rear = (rear + 1) % 100;
}

char* dequeue()
{
	if (front == rear)
	{
		return 0;
	}
	char *temp = queue[front];
	front = (front + 1) % 100;
	return temp;
}

void push(char* _data)
{


	top++;
	stack[top] = _data;
	return;
}

char *pop()
{
	if (top == -1)
	{
		return 0;
	}

	char *temp = stack[top];
	top--;
	return temp;
}

// 1161
int main(void)
{
	char name[100];
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		scanf("%s", name);
		char *new_one = (char *)malloc(strlen(name) + 1);
		strcpy(new_one, name);
		push(new_one);
	}

	while (top > -1)
	{
		enqueue(pop());
	}

	while (front != rear)
	{
		push(dequeue());
	}

	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		int where;
		scanf("%d", &where);

		while (top > where-2)
		{
			enqueue(pop());
		}

		char *temp;
		while ((temp = dequeue()) != 0)
		{
			push(temp);
		}
	}

	char *temp;
	while ((temp = pop()) != 0)
	{
		printf("%s\n", temp);
		free(temp);
	}

	return 0;
}
반응형

'데이터구조 > 소스코드' 카테고리의 다른 글

Priority Queue, Min Heap  (0) 2016.05.16
BST를 이용한 영어/한글 단어관리 프로그램 소스  (0) 2016.05.09
OJ 1160  (0) 2016.04.25
OJ 1165  (0) 2016.04.25
OJ 1165  (0) 2016.04.25