슈

[알고리즘] 단순 연결 리스트를 이용한 스택(stack)

/*

연결 리스트, 링크드 리스트(linked list)는 각 노드가 데이터와 포인터를 가지고 한 줄로 연결되어 있는 방식으로 

데이터를 저장하는 자료 구조이다. 

이름에서 말하듯이 데이터를 담고 있는 노드들이 연결되어 있는데, 

노드의 포인터가 다음이나 이전의 노드와의 연결을 담당하게 된다. 

연결 리스트의 종류로는 단순 연결 리스트, 이중 연결 리스트 등이 있다. 

*/

// 단순 연결 리스트를 이용한 스택의 구현

 

class Node {

    public $key;

    public $next = Node;

} 

 

$head = new Node;

$tail = new Node;

 

function init_stack() {

    Global $head, $tail;

 

    $head->next = $tail;

    $tail->next = $tail;

}

 

function clear_stack() {

    Global $head, $tail;

    $t = Node;

    $s = Node;

 

    $t = $head->next;

    while ($t != $tail) {

        $s = $t;

        $t = $t->next;

        $s = null;

    }

    $head->next = $tail;

}

 

function push($k) {

    Global $head, $tail;

    $t = new Node;

   

    if ($t == NULL) {

        println("   Out of memory...");        

    }

  

    $t->key = $k;

    $t->next = $head->next;

    $head->next = $t;

}

 

function pop() {

    Global $head, $tail;

    $t = new Node;

 

    if ($head->next == $tail)  /* if empty */ {

        println("  Stack underflow.");

        return -1;

    }

    $t = $head->next;

    $i = $t->key;

    $head->next = $t->next;

    $t = null;

    

    return $i;

}

 

function print_stack() {

    Global $head, $tail;

    $t = new Node;

 

    $t = $head->next;

    println("  Stack contents : Top ----> Bottom");

    while ($t != $tail) {

        println($t->key);

        $t = $t->next;

    }

}

 

init_stack();

 

println('Push 1, 2, 3');

push(1);

push(2);

push(3);

print_stack();

 

$i = pop();

println('Pop '.$i);

print_stack();

 

println('Push 4, 5, 6');

push(4);

push(5);

push(6);

print_stack();

 

println('Initialize stack');

clear_stack();

print_stack();

 

println('Now stack is empty,');

println('Pop');

$i = pop();

print_stack();

 

function println($str=''){

    echo $str.'<br />';

} 

 

/* output

Push 1, 2, 3

Stack contents : Top ----> Bottom
3
2
1
Pop 3
Stack contents : Top ----> Bottom
2
1
Push 4, 5, 6
Stack contents : Top ----> Bottom
6
5
4
2
1
Initialize stack
Stack contents : Top ----> Bottom
Now stack is empty,
Pop
Stack underflow.

Stack contents : Top ----> Bottom 

*/

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

프로그램

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,290
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,443
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,768
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,954
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,709
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