Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- storage
- algorithm
- 자바
- 레드햇
- 설치
- 리눅스
- 재귀
- 스프링
- 아마존
- 스토리지
- 하둡
- linux
- AWS
- recursive
- data
- Amazon
- redhat
- docker
- 도커
- java
- Spring
- big data
- 자료구조
- rhcsa
- sort
- 알고리즘
- 빅데이터
- Data Structure
- hadoop
- Redshift
Archives
- Today
- Total
Developer MJ
[Java] Queue 본문
먼저 집어 넣은 데이터가 먼저 나오는 FIFO (First In First Out)구조
N(2<= N <=100)개의 수를 Queue에 넣은 후 하나씩 꺼내 화면에 출력
import java.util.Scanner;
class Solution {
static final int MAX_N = 100;
static int front;
static int rear;
static int queue[] = new int[MAX_N];
static void queueInit()
{
front = 0;
rear = 0;
}
static boolean queueIsEmpty()
{
return (front == rear);
}
static boolean queueIsFull()
{
if ((front + 1) % MAX_N == rear)
{
return true;
}
else
{
return false;
}
}
static boolean queueEnqueue(int value)
{
if (queueIsFull())
{
System.out.print("queue is full!");
return false;
}
queue[front] = value;
front++;
if (front >= MAX_N)
{
front = MAX_N;
}
return true;
}
static Integer queueDequeue()
{
if (queueIsEmpty())
{
System.out.print("queue is empty!");
return null;
}
Integer value = new Integer(queue[rear]);
rear++;
if (rear >= MAX_N)
{
rear = MAX_N;
}
return value;
}
public static void main(String arg[]) throws Exception {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int test_case = 1; test_case <= T; test_case++)
{
int N = sc.nextInt();
queueInit();
for (int i = 0; i < N; i++)
{
int value = sc.nextInt();
queueEnqueue(value);
}
System.out.print("#" + test_case + " ");
while (!queueIsEmpty())
{
Integer value = queueDequeue();
if (value != null)
{
System.out.print(value.intValue() + " ");
}
}
System.out.println();
}
sc.close();
}
}
'Programming > Code' 카테고리의 다른 글
[Java] 계수정렬(Counting Sort) (0) | 2019.04.18 |
---|---|
[Java] 삽입 정렬 (Insertion Sort) (0) | 2019.04.15 |
[Java] Stack (0) | 2019.02.13 |
[Java] BlobSize Algorithm 문제 (0) | 2017.10.12 |
[Java] N_Queens Algorithm 문제 (0) | 2017.10.12 |