<?
/*
큐(queue)는 컴퓨터의 기본적인 자료 구조의 한가지로,
먼저 집어 넣은 데이터가 먼저 나오는 FIFO (First In First Out)구조로 저장하는 형식을 말한다
http://terms.naver.com/entry.nhn?docId=834442&cid=42344&categoryId=42344
http://ko.wikipedia.org/wiki/%ED%81%90_(%EC%9E%90%EB%A3%8C_%EA%B5%AC%EC%A1%B0)
*/
// 시작노드와 끝노드만 남기고 나머지 노드 삭제
function clear_queue() {
global $head, $tail;
$t = new dnode;
$s = new dnode;
$t = $head->next;
while ($t != $tail) {
$s = $t;
$t = $t->next;
$s = null;
}
$head->next = $tail;
$tail->prev = $head;
}
// 마지막 노드 앞에 새 노드 삽입
function put($k) {
global $head, $tail;
$t = new dnode;
$t->key = $k;
$tail->prev->next = $t;
$t->prev = $tail->prev;
$tail->prev = $t;
$t->next = $tail;
return $k;
}
// 큐에 시작노드 다음 노드의 key 값 가져오기
function get() {
global $head, $tail;
$t = new dnode;
$i = 0;
$t = $head->next;
if ($t == $tail) {
printf('<br /> Queue underflow.');
return -1;
}
$i = $t->key;
$head->next = $t->next;
$t->next->prev = $head;
$t = null;
return $i;
}
// 큐에 저장된 key 값 보여주기
function print_queue() {
global $head, $tail;
$t = $head->next;
printf('<br /> Queue contents : Front ----> Rear<br />');
while ($t != $tail) {
printf('%2d', $t->key);
$t = $t->next;
}
}
// 노드정의
class dnode {
public $key = 0,
$prev = null,
$next = null;
}
$head = new dnode; // 시작노드
$tail = new dnode; // 끝노드
$head->prev = $head;
$head->next = $tail;
$tail->prev = $head;
$tail->next = $tail;
printf('<br />Put 1, 2, 3, 4, 5, 6');
put(1);
put(2);
put(3);
put(4);
put(5);
put(6);
print_queue();
echo ('<br /><br />Get');
$i = get();
printf('<br /> getting value is %d', $i);
print_queue();
printf('<br /><br />Put 7, 8, 9, 1');
put(7);
put(8);
put(9);
put(1);
print_queue();
printf('<br /><br />Put 2');
put(2);
print_queue();
printf('<br /><br />Initialize queue');
clear_queue();
print_queue();
printf('<br /><br />Now queue is empty');
echo ('<br />Get');
$i = get();
printf('<br /> getting value is %d', $i);
print_queue();
?>
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 130 | 20년 전 | 3972 | ||
| 129 | 20년 전 | 2913 | ||
| 128 | 20년 전 | 3649 | ||
| 127 | 20년 전 | 3460 | ||
| 126 | 20년 전 | 3733 | ||
| 125 | 20년 전 | 8565 | ||
| 124 | 20년 전 | 2589 | ||
| 123 | 20년 전 | 3735 | ||
| 122 | 20년 전 | 3194 | ||
| 121 | 20년 전 | 2595 | ||
| 120 | 20년 전 | 2653 | ||
| 119 | 20년 전 | 2568 | ||
| 118 | 20년 전 | 2854 | ||
| 117 |
|
20년 전 | 3040 | |
| 116 | 20년 전 | 5291 | ||
| 115 | 20년 전 | 3901 | ||
| 114 | 20년 전 | 4950 | ||
| 113 | 20년 전 | 6196 | ||
| 112 | 20년 전 | 7317 | ||
| 111 | 20년 전 | 18422 | ||
| 110 | 20년 전 | 6868 | ||
| 109 | 20년 전 | 2868 | ||
| 108 | 20년 전 | 4127 | ||
| 107 |
prosper
|
20년 전 | 2477 | |
| 106 |
prosper
|
20년 전 | 4302 | |
| 105 |
아우겐나이스
|
20년 전 | 2896 | |
| 104 | 20년 전 | 2249 | ||
| 103 | 20년 전 | 2466 | ||
| 102 | 20년 전 | 2227 | ||
| 101 | 20년 전 | 2556 | ||
| 100 | 20년 전 | 1736 | ||
| 99 | 20년 전 | 1558 | ||
| 98 | 20년 전 | 1609 | ||
| 97 | 20년 전 | 2113 | ||
| 96 | 20년 전 | 1863 | ||
| 95 | 20년 전 | 2362 | ||
| 94 | 20년 전 | 3549 | ||
| 93 | 20년 전 | 1547 | ||
| 92 | 20년 전 | 1745 | ||
| 91 | 20년 전 | 3161 | ||
| 90 | 20년 전 | 2334 | ||
| 89 | 20년 전 | 3158 | ||
| 88 | 20년 전 | 2859 | ||
| 87 | 20년 전 | 3284 | ||
| 86 | 20년 전 | 5109 | ||
| 85 | 20년 전 | 2509 | ||
| 84 | 20년 전 | 4812 | ||
| 83 | 20년 전 | 2491 | ||
| 82 | 20년 전 | 3106 | ||
| 81 | 20년 전 | 7610 | ||
| 80 | 20년 전 | 3808 | ||
| 79 | 20년 전 | 3198 | ||
| 78 | 20년 전 | 4680 | ||
| 77 | 20년 전 | 2886 | ||
| 76 | 20년 전 | 6206 | ||
| 75 | 20년 전 | 4462 | ||
| 74 | 20년 전 | 5771 | ||
| 73 | 20년 전 | 3617 | ||
| 72 | 20년 전 | 5956 | ||
| 71 | 20년 전 | 3098 | ||
| 70 | 20년 전 | 2827 | ||
| 69 | 20년 전 | 2623 | ||
| 68 | 20년 전 | 2432 | ||
| 67 | 20년 전 | 2641 | ||
| 66 | 20년 전 | 2660 | ||
| 65 | 20년 전 | 3779 | ||
| 64 | 20년 전 | 2813 | ||
| 63 | 20년 전 | 2445 | ||
| 62 | 20년 전 | 2251 | ||
| 61 | 20년 전 | 3050 | ||
| 60 | 20년 전 | 3122 | ||
| 59 | 20년 전 | 2501 | ||
| 58 | 20년 전 | 2582 | ||
| 57 | 20년 전 | 2955 | ||
| 56 | 20년 전 | 2300 | ||
| 55 | 20년 전 | 2746 | ||
| 54 | 20년 전 | 2112 | ||
| 53 | 20년 전 | 2337 | ||
| 52 | 20년 전 | 2685 | ||
| 51 |
prosper
|
20년 전 | 2336 | |
| 50 |
prosper
|
20년 전 | 2155 | |
| 49 | 20년 전 | 2163 | ||
| 48 | 20년 전 | 2317 | ||
| 47 | 20년 전 | 1917 | ||
| 46 | 20년 전 | 1917 | ||
| 45 | 20년 전 | 2124 | ||
| 44 | 20년 전 | 2350 | ||
| 43 | 21년 전 | 4564 | ||
| 42 |
prosper
|
21년 전 | 2702 | |
| 41 |
prosper
|
21년 전 | 2104 | |
| 40 | 21년 전 | 2169 | ||
| 39 | 21년 전 | 2135 | ||
| 38 | 21년 전 | 2404 | ||
| 37 | 21년 전 | 2552 | ||
| 36 | 21년 전 | 1766 | ||
| 35 | 21년 전 | 4062 | ||
| 34 | 21년 전 | 3839 | ||
| 33 | 21년 전 | 2981 | ||
| 32 |
prosper
|
21년 전 | 2889 | |
| 31 | 21년 전 | 5263 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기