<?
/*
큐(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();
?>
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 430 | 19년 전 | 4104 | ||
| 429 | 19년 전 | 3305 | ||
| 428 | 19년 전 | 4132 | ||
| 427 | 19년 전 | 3474 | ||
| 426 | 19년 전 | 3106 | ||
| 425 | 19년 전 | 3408 | ||
| 424 | 19년 전 | 2439 | ||
| 423 | 19년 전 | 2867 | ||
| 422 | 19년 전 | 2353 | ||
| 421 | 19년 전 | 3708 | ||
| 420 | 19년 전 | 4743 | ||
| 419 | 19년 전 | 3803 | ||
| 418 |
|
19년 전 | 1752 | |
| 417 | 19년 전 | 2750 | ||
| 416 | 19년 전 | 2392 | ||
| 415 | 19년 전 | 2607 | ||
| 414 | 19년 전 | 4411 | ||
| 413 |
|
19년 전 | 2601 | |
| 412 | 19년 전 | 3048 | ||
| 411 |
|
19년 전 | 2996 | |
| 410 |
|
19년 전 | 3689 | |
| 409 |
|
19년 전 | 3637 | |
| 408 |
|
19년 전 | 1860 | |
| 407 | 19년 전 | 2230 | ||
| 406 | 19년 전 | 2784 | ||
| 405 | 19년 전 | 2452 | ||
| 404 | 19년 전 | 4304 | ||
| 403 | 19년 전 | 3305 | ||
| 402 |
NeoGenesis
|
19년 전 | 4093 | |
| 401 | 19년 전 | 2610 | ||
| 400 |
|
19년 전 | 2500 | |
| 399 | 19년 전 | 2957 | ||
| 398 | 19년 전 | 2488 | ||
| 397 | 19년 전 | 2780 | ||
| 396 | 19년 전 | 2482 | ||
| 395 | 19년 전 | 3187 | ||
| 394 | 19년 전 | 1725 | ||
| 393 | 19년 전 | 2994 | ||
| 392 | 19년 전 | 2257 | ||
| 391 | 19년 전 | 2177 | ||
| 390 | 19년 전 | 2287 | ||
| 389 | 19년 전 | 2646 | ||
| 388 | 19년 전 | 2182 | ||
| 387 | 19년 전 | 4486 | ||
| 386 |
|
19년 전 | 2693 | |
| 385 |
|
19년 전 | 2484 | |
| 384 | 19년 전 | 3020 | ||
| 383 | 19년 전 | 3056 | ||
| 382 | 19년 전 | 3110 | ||
| 381 |
|
19년 전 | 2633 | |
| 380 |
|
19년 전 | 3021 | |
| 379 | 19년 전 | 2551 | ||
| 378 | 19년 전 | 2208 | ||
| 377 | 19년 전 | 2780 | ||
| 376 | 19년 전 | 2470 | ||
| 375 |
|
19년 전 | 2564 | |
| 374 | 19년 전 | 3821 | ||
| 373 | 19년 전 | 3270 | ||
| 372 | 19년 전 | 4989 | ||
| 371 |
세은아빠2
|
19년 전 | 2415 | |
| 370 | 19년 전 | 4502 | ||
| 369 | 19년 전 | 3109 | ||
| 368 | 19년 전 | 2907 | ||
| 367 | 19년 전 | 3726 | ||
| 366 | 19년 전 | 2656 | ||
| 365 | 19년 전 | 3739 | ||
| 364 | 19년 전 | 4016 | ||
| 363 | 19년 전 | 3442 | ||
| 362 | 19년 전 | 3483 | ||
| 361 | 19년 전 | 4108 | ||
| 360 |
hwatta
|
19년 전 | 2366 | |
| 359 | 19년 전 | 5109 | ||
| 358 | 19년 전 | 3657 | ||
| 357 | 19년 전 | 2601 | ||
| 356 |
sdesign1s
|
19년 전 | 2280 | |
| 355 | 20년 전 | 2757 | ||
| 354 | 20년 전 | 3031 | ||
| 353 | 20년 전 | 2796 | ||
| 352 |
|
20년 전 | 5776 | |
| 351 |
|
20년 전 | 2706 | |
| 350 |
|
20년 전 | 4294 | |
| 349 |
hwatta
|
20년 전 | 2183 | |
| 348 | 20년 전 | 7307 | ||
| 347 | 20년 전 | 2414 | ||
| 346 | 20년 전 | 3505 | ||
| 345 | 20년 전 | 4311 | ||
| 344 | 20년 전 | 2653 | ||
| 343 | 20년 전 | 3922 | ||
| 342 | 20년 전 | 3069 | ||
| 341 | 20년 전 | 4094 | ||
| 340 |
|
20년 전 | 5143 | |
| 339 |
|
20년 전 | 4236 | |
| 338 | 20년 전 | 5873 | ||
| 337 | 20년 전 | 2045 | ||
| 336 |
|
20년 전 | 3325 | |
| 335 |
|
20년 전 | 3540 | |
| 334 |
|
20년 전 | 2932 | |
| 333 |
hwatta
|
20년 전 | 2437 | |
| 332 | 20년 전 | 4656 | ||
| 331 | 20년 전 | 2274 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기