슈

[알고리즘] 이중연결리스트를 이용한 큐(queue)

· 2014-10-04 (토) 14:41:39 · 5038

<?

/* 

큐(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();

?> 

댓글을 작성하시려면 로그인이 필요합니다.

프로그램

8,187건
+
제목 글쓴이 날짜 조회
14-08-26 조회 7,429
14-08-25 조회 4,227
14-08-23 조회 3,990
14-08-22 조회 4,483
14-08-22 조회 1.1만
14-08-22 조회 4,032
14-08-22 조회 5,292
14-08-22 조회 4,240
14-08-22 조회 4,133
14-08-21 조회 4,291
14-08-21 조회 3,849
14-08-21 조회 3,691
14-08-20 조회 4,032
14-08-20 조회 3,696
14-08-19 조회 4,494
14-08-19 조회 3,724
14-08-19 조회 4,052
14-08-19 조회 3,823
14-08-19 조회 7,569
14-08-19 조회 3,672
14-08-18 조회 3,939
14-08-18 조회 4,464
14-08-18 조회 3,839
14-08-17 조회 8,248
14-08-17 조회 6,527
14-08-17 조회 7,952
14-08-17 조회 3,748
14-08-16 조회 4,301
14-08-16 조회 4,277
14-08-16 조회 4,341
14-08-14 조회 4,085
14-08-14 조회 3,636
14-08-13 조회 6,415
14-08-13 조회 5,975
14-08-13 조회 5,367
14-08-13 조회 4,582
14-08-13 조회 4,465
14-08-13 조회 5,843
14-08-13 조회 3,753
14-08-12 조회 4,119
14-08-12 조회 5,490
14-08-12 조회 6,360
14-08-12 조회 4,235
14-08-12 조회 3,901
14-08-12 조회 4,417
14-08-11 조회 3,753
14-08-11 조회 4,434
14-08-11 조회 3,751
14-08-11 조회 4,444
14-08-11 조회 4,197
14-08-11 조회 5,270
14-08-11 조회 6,994
14-08-11 조회 6,639
14-08-11 조회 6,748
14-08-11 조회 4,655
14-08-11 조회 4,692
14-08-11 조회 5,315
14-08-08 조회 3,973
14-08-08 조회 4,617
14-08-08 조회 6,769
14-08-07 조회 5,346
14-08-07 조회 3,677
14-08-07 조회 4,387
14-08-06 조회 4,195
14-08-06 조회 3,810
14-08-06 조회 6,094
14-08-06 조회 4,641
14-08-06 조회 4,550
14-08-06 조회 4,176
14-08-06 조회 6,447
14-08-06 조회 5,721
14-08-06 조회 4,058
14-08-06 조회 6,609
14-08-06 조회 4,091
14-08-05 조회 3,651
14-08-05 조회 3,955
14-08-05 조회 3,719
14-08-05 조회 3,743
14-08-05 조회 4,075
14-08-04 조회 4,941
14-08-04 조회 4,120
14-08-04 조회 3,710
14-08-03 조회 3,571
14-08-03 조회 5,670
14-08-03 조회 5,082
14-08-03 조회 5,332
14-08-02 조회 4,591
14-08-01 조회 5,715
14-08-01 조회 6,421
14-08-01 조회 7,593
14-08-01 조회 8,863
14-08-01 조회 5,586
14-08-01 조회 4,847
14-08-01 조회 4,781
14-08-01 조회 4,163
14-08-01 조회 4,141
14-08-01 조회 4,256
14-08-01 조회 3,937
14-08-01 조회 4,841
14-08-01 조회 5,225