728x90
반응형
[알고리즘] Queue
Queue
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 | public class Queue { public static int front = -1; public static int rear = -1; public static int[] q = new int[5]; public static boolean isFull() { return rear == q.length -1; } public static boolean isEmpty() { return front == rear; } public static void enQueue(int item) { if(isFull()) { System.out.println("가득 찼음"); } else { System.out.println(item + "추가"); q[++rear] = item; } } public static int deQueue() { if(isEmpty()) { System.out.println("비어 있음"); return -1; } else { System.out.println("삭제"); return q[++front]; } } public static void main(String[] args) { enQueue(1); enQueue(2); enQueue(3); deQueue(); deQueue(); deQueue(); deQueue(); } } | cs |
원형 Queue
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 | public class 원형Queue { public static int front = 0; public static int rear = 0; public static int[] q = new int[5]; public static boolean isFull() { return (rear+1) % q.length == front; } public static boolean isEmpty() { return front == rear; } public static void enQueue(int item) { if(isFull()) { System.out.println("가득 찼음"); } else { System.out.println(item + "추가"); rear++; if(rear == q.length) { rear = 0; } q[rear] = item; } } public static int deQueue() { if(isEmpty()) { System.out.println("비어 있음"); return -1; } else { System.out.println("삭제"); front++; if ( front == q.length ) { front = 0; } return q[front]; } } public static void main(String[] args) { enQueue(1); enQueue(2); enQueue(3); deQueue(); deQueue(); deQueue(); deQueue(); } } | cs |
연결 Queue
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 | import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; public class 연결Queue { public static Node front = null; public static Node rear = null; //public static boolean isFull() { //} public static boolean isEmpty() { return front == null; } public static void enQueue(int item) { Node newNode = new Node(item); if (isEmpty()) { // 공백 큐 였으면 front = newNode; rear = newNode; } else { rear.link = newNode; rear = newNode; } } public static int deQueue() { if (isEmpty()) { System.out.println("Queue underflow Error~"); return -1; } else { int data = front.data; front = front.link; if(isEmpty()) { // 공백 큐라면 rear = null; } return data; } } public static void main(String[] args) { Queue<Integer> q = new LinkedList<Integer>(); enQueue(1); enQueue(2); enQueue(3); System.out.println(deQueue()); System.out.println(deQueue()); System.out.println(deQueue()); System.out.println(deQueue()); } } class Node { Node link; // 다음 노드의 주소를 저장할 변수 int data; public Node(int data) { this.data = data; } } | cs |
728x90
반응형
'Algorithm > 개념 정리' 카테고리의 다른 글
[알고리즘] 백트래킹(Backtracking) (0) | 2019.02.17 |
---|---|
[알고리즘] 백트래킹을 활용한 부분 집합 & 순열 (0) | 2019.02.12 |
[알고리즘] 순열 & 조합 (0) | 2019.01.26 |
[알고리즘] 후위표기법 (1) | 2019.01.21 |
[C++] 배열 사이즈 구하기 (0) | 2018.11.12 |