Home 10845번(큐)
Post
Cancel

10845번(큐)

첫 시도

  • java에서 큐는 리스트로 구현되어 있다
  • LinkedList를 사용해서 구현

해결

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
101
102
103
104
105
106
107
108
109
110
111
112
113
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;

public class Main {
    public static void main(String[] ars) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        int N = Integer.parseInt(br.readLine());
        LinkedListQueue q = new LinkedListQueue();

        for (int i = 0; i < N; i++) {
            String[] input = br.readLine().split(" ");
            String commend = input[0];
            switch (commend){
                case "push":
                    int num = Integer.parseInt(input[1]);
                    q.push(num);
                    break;
                case "pop":
                    sb.append(q.pop()).append("\n");
                    break;
                case "size":
                    sb.append(q.size()).append("\n");
                    break;
                case "empty":
                    sb.append(q.empty()).append("\n");
                    break;
                case "front":
                    sb.append(q.front()).append("\n");
                    break;
                case "back":
                    sb.append(q.back()).append("\n");
                    break;
            }
        }
        System.out.println(sb.toString());


    }

}

class Node{
    int data;
    Node next;

    public Node(int data) {
        this.data = data;
        this.next = null;
    }
}

class LinkedListQueue{
    Node head;
    Node tail;
    int size;

    public LinkedListQueue() {
        this.head = null;
        this.tail = null;
        this.size = 0;
    }
    public void push(Integer x){
        Node nNode = new Node(x);
        if(size == 0){
            head = nNode;
        }
        else{
            tail.next = nNode;
        }
        tail = nNode;
        size++;
    }

    public int pop(){
        int result = -1;
        if(size != 0){
            result = head.data;
            head = head.next;
            size--;
        }
        return result;
    }

    public int size(){
        return size;
    }

    public int empty(){
        int result = 1;
        if(size != 0){
            result = 0;
        }
        return result;
    }

    public int front(){
        int result = -1;
        if(size != 0){
            result = head.data;
        }
        return result;
    }
    public int back(){
        int result = -1;
        if(size != 0){
            result = tail.data;
        }
        return result;
    }

}

참고

This post is licensed under CC BY 4.0 by the author.