<?
/*
큐(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();
?>
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 230 | 20년 전 | 2094 | ||
| 229 | 20년 전 | 3155 | ||
| 228 | 20년 전 | 3344 | ||
| 227 | 20년 전 | 2419 | ||
| 226 | 20년 전 | 5499 | ||
| 225 | 20년 전 | 2542 | ||
| 224 | 20년 전 | 2985 | ||
| 223 | 20년 전 | 4219 | ||
| 222 | 20년 전 | 2616 | ||
| 221 | 20년 전 | 2305 | ||
| 220 | 20년 전 | 3693 | ||
| 219 | 20년 전 | 2090 | ||
| 218 | 20년 전 | 3589 | ||
| 217 | 20년 전 | 2500 | ||
| 216 | 20년 전 | 2940 | ||
| 215 | 20년 전 | 2250 | ||
| 214 | 20년 전 | 3356 | ||
| 213 | 20년 전 | 2941 | ||
| 212 | 20년 전 | 3085 | ||
| 211 | 20년 전 | 2171 | ||
| 210 | 20년 전 | 1932 | ||
| 209 | 20년 전 | 2383 | ||
| 208 | 20년 전 | 2016 | ||
| 207 | 20년 전 | 1695 | ||
| 206 | 20년 전 | 1869 | ||
| 205 | 20년 전 | 3968 | ||
| 204 | 20년 전 | 1691 | ||
| 203 | 20년 전 | 2032 | ||
| 202 | 20년 전 | 2369 | ||
| 201 | 20년 전 | 1846 | ||
| 200 | 20년 전 | 2976 | ||
| 199 | 20년 전 | 2019 | ||
| 198 | 20년 전 | 2101 | ||
| 197 | 20년 전 | 3673 | ||
| 196 | 20년 전 | 3013 | ||
| 195 | 20년 전 | 2129 | ||
| 194 | 20년 전 | 10274 | ||
| 193 | 20년 전 | 2271 | ||
| 192 | 20년 전 | 1620 | ||
| 191 | 20년 전 | 2685 | ||
| 190 | 20년 전 | 2305 | ||
| 189 | 20년 전 | 1709 | ||
| 188 | 20년 전 | 1505 | ||
| 187 | 20년 전 | 1929 | ||
| 186 | 20년 전 | 1755 | ||
| 185 | 20년 전 | 1802 | ||
| 184 | 20년 전 | 2387 | ||
| 183 | 20년 전 | 1596 | ||
| 182 | 20년 전 | 1501 | ||
| 181 | 20년 전 | 1642 | ||
| 180 | 20년 전 | 2741 | ||
| 179 | 20년 전 | 1829 | ||
| 178 | 20년 전 | 1880 | ||
| 177 | 20년 전 | 2008 | ||
| 176 | 20년 전 | 1817 | ||
| 175 | 20년 전 | 1888 | ||
| 174 | 20년 전 | 1716 | ||
| 173 | 20년 전 | 2074 | ||
| 172 | 20년 전 | 1799 | ||
| 171 | 20년 전 | 2584 | ||
| 170 | 20년 전 | 2301 | ||
| 169 | 20년 전 | 2581 | ||
| 168 | 20년 전 | 1515 | ||
| 167 | 20년 전 | 1603 | ||
| 166 | 20년 전 | 2175 | ||
| 165 | 20년 전 | 1663 | ||
| 164 | 20년 전 | 3793 | ||
| 163 | 20년 전 | 2691 | ||
| 162 | 20년 전 | 2114 | ||
| 161 | 20년 전 | 2822 | ||
| 160 | 20년 전 | 1759 | ||
| 159 | 20년 전 | 1637 | ||
| 158 | 20년 전 | 2589 | ||
| 157 | 20년 전 | 1516 | ||
| 156 | 20년 전 | 1776 | ||
| 155 | 20년 전 | 3256 | ||
| 154 | 20년 전 | 1926 | ||
| 153 | 20년 전 | 1647 | ||
| 152 | 20년 전 | 4981 | ||
| 151 | 20년 전 | 4603 | ||
| 150 | 20년 전 | 3540 | ||
| 149 | 20년 전 | 3817 | ||
| 148 | 20년 전 | 7102 | ||
| 147 | 20년 전 | 3571 | ||
| 146 | 20년 전 | 2651 | ||
| 145 | 20년 전 | 2642 | ||
| 144 | 20년 전 | 7180 | ||
| 143 | 20년 전 | 4626 | ||
| 142 | 20년 전 | 1914 | ||
| 141 | 20년 전 | 3275 | ||
| 140 | 20년 전 | 1992 | ||
| 139 | 20년 전 | 1616 | ||
| 138 | 20년 전 | 2333 | ||
| 137 | 20년 전 | 1810 | ||
| 136 | 20년 전 | 1476 | ||
| 135 | 20년 전 | 1816 | ||
| 134 | 20년 전 | 2994 | ||
| 133 | 20년 전 | 2475 | ||
| 132 | 20년 전 | 1722 | ||
| 131 | 20년 전 | 1669 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기