2. 문제 요약: 간단한 큐 구현
3. 문제 접근법: 큐가 무엇인지 알면 된다. C를 사용할 경우, 배열을 매우 크게 잡거나 연결 리스트를 구현하자.
Ruby(2.5) 코드
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
|
def push(queue, x)
queue << x
end
def pop(queue)
if queue.any?
puts queue.shift(1)
else
puts -1
end
end
def size(queue)
puts queue.length
end
def empty(queue)
if queue.any?
puts 0
elsif
puts 1
end
end
def front(queue)
if queue.any?
puts queue[0]
elsif
puts -1
end
end
def back(queue)
if queue.any?
puts queue[-1]
elsif
puts -1
end
end
data = Array.new()
t = gets.to_i
t.times do
args = gets.split(" ")
case args[0]
when "push" then
push(data, args[1])
when "pop" then
pop(data)
when "front" then
front(data)
when "back" then
back(data)
when "empty" then
empty(data)
when "size" then
size(data)
else
end
end
| cs |
댓글 없음:
댓글 쓰기