슈

[알고리즘] 단순 연결 리스트를 이용한 스택(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건
+
제목 글쓴이 날짜 조회
13-12-07 조회 3,146
13-12-06 조회 2,660
13-12-06 조회 3,207
13-12-06 조회 2,779
13-12-06 조회 2,997
13-12-06 조회 2,686
13-12-06 조회 3,420
13-12-06 조회 3,749
13-12-06 조회 2,767
13-12-06 조회 2,725
13-12-06 조회 3,812
13-12-05 조회 2,759
13-12-05 조회 3,354
13-12-05 조회 3,212
13-12-05 조회 3,621
13-12-05 조회 3,530
13-12-04 조회 8,342
13-12-03 조회 3,497
13-12-02 조회 2,784
13-12-02 조회 2,633
13-11-30 조회 2,573
13-11-29 조회 2,829
13-11-28 조회 3,044
13-11-27 조회 3,636
13-11-27 조회 3,023
13-11-27 조회 3,407
13-11-27 조회 2,558
13-11-27 조회 2,731
13-11-27 조회 4,777
13-11-26 조회 4,034
13-11-25 조회 3,532
13-11-25 조회 5,332
13-11-23 조회 2,704
13-11-23 조회 4,804
13-11-23 조회 4,357
13-11-23 조회 4,119
13-11-23 조회 4,061
13-11-22 조회 3,075
13-11-22 조회 3,755
13-11-22 조회 3,602
13-11-22 조회 3,156
13-11-22 조회 5,571
13-11-21 조회 2,802
13-11-21 조회 3,295
13-11-20 조회 3,661
13-11-19 조회 4,092
13-11-19 조회 3,098
13-11-19 조회 3,726
13-11-19 조회 3,073
13-11-19 조회 4,836
13-11-18 조회 3,120
13-11-18 조회 2,944
13-11-18 조회 3,256
13-11-18 조회 4,998
13-11-18 조회 4,401
13-11-18 조회 6,863
13-11-18 조회 4,085
13-11-18 조회 4,499
13-11-18 조회 3,870
13-11-18 조회 3,926
13-11-18 조회 3,192
13-11-16 조회 3,224
13-11-15 조회 4,462
13-11-15 조회 3,240
13-11-14 조회 2,988
13-11-14 조회 3,748
13-11-14 조회 4,116
13-11-13 조회 3,229
13-11-13 조회 3,276
13-11-13 조회 3,452
13-11-13 조회 4,332
13-11-13 조회 3,166
13-11-12 조회 3,485
13-11-12 조회 5,704
13-11-07 조회 3,221
13-11-07 조회 3,195
13-11-06 조회 3,149
13-11-05 조회 3,864
13-11-05 조회 3,636
13-11-05 조회 6,671
13-11-05 조회 3,611
13-11-04 조회 3,213
13-11-04 조회 3,232
13-11-04 조회 3,014
13-11-02 조회 3,677
13-11-02 조회 3,311
13-11-01 조회 3,567
13-11-01 조회 2,944
13-11-01 조회 3,079
13-10-31 조회 3,149
13-10-31 조회 3,539
13-10-31 조회 3,935
13-10-31 조회 3,122
13-10-30 조회 3,428
13-10-30 조회 7,697
13-10-30 조회 5,530
13-10-30 조회 3,399
13-10-29 조회 3,230
13-10-29 조회 4,401
13-10-29 조회 3,665