Stack, Queue 알고리즘 2
정렬되지 않는 스택이 있습니다. 또다른 스택만을 이용해서 정렬을 하시오.
정렬되지 않는 스택이 있습니다. 또다른 스택만을 이용해서 정렬을 하시오.
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
public class test {
public static void sort(Stack<Integer> s1) {
// 다른 스택 생성
Stack<Integer> s2 = new Stack<>();
// 1번째 스택에 있을 경우
while(!s1.isEmpty()) {
// top 데이터 임시 저장
Integer tmp = s1.pop();
// 2번째 스택이 없고 1번째 top 보다 2번째 top이 큰 경우
while(!s2.isEmpty() && s2.peek() > tmp){
// 1번째 스택에 저장
s1.push(s2.pop());
}
// 그 다음 1번째 top데이터를 2번째 스택에 저장
s2.push(tmp);
}
// 2번째 스택에 저장된 값을 1번쨰 스택으로 옮김 (출력을 위해)
while(!s2.isEmpty()) {
s1.push(s2.pop());
}
}
public static void main(String[] args) {
System.out.println("hello world");
Stack<Integer> s1 = new Stack<>();
s1.push(1);
s1.push(5);
s1.push(6);
s1.push(3);
sort(s1);
System.out.println(s1.pop());
System.out.println(s1.pop());
System.out.println(s1.pop());
System.out.println(s1.pop());
}
}
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.