슈

[알고리즘] 단순 연결 리스트를 이용한 스택(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-01 조회 3,892
14-07-31 조회 3,988
14-07-31 조회 4,849
14-07-30 조회 4,528
14-07-30 조회 4,345
14-07-30 조회 4,077
14-07-30 조회 4,162
14-07-29 조회 6,639
14-07-29 조회 4,500
14-07-28 조회 4,845
14-07-27 조회 5,095
14-07-27 조회 4,267
14-07-26 조회 4,139
14-07-26 조회 4,142
14-07-25 조회 3,585
14-07-25 조회 4,140
14-07-25 조회 4,656
14-07-23 조회 4,601
14-07-23 조회 3,773
14-07-22 조회 4,223
14-07-21 조회 3,719
14-07-21 조회 4,734
14-07-19 조회 4,094
14-07-18 조회 3,465
14-07-18 조회 5,235
14-07-17 조회 9,559
14-07-17 조회 4,191
14-07-17 조회 3,854
14-07-16 조회 4,055
14-07-16 조회 3,609
14-07-16 조회 4,140
14-07-15 조회 3,680
14-07-14 조회 6,278
14-07-13 조회 4,133
14-07-12 조회 4,540
14-07-12 조회 3,926
14-07-11 조회 5,363
14-07-10 조회 4,247
14-07-09 조회 5,648
14-07-08 조회 4,897
14-07-08 조회 5,572
14-07-08 조회 4,367
14-07-08 조회 5,532
14-07-06 조회 4,522
14-07-06 조회 3,882
14-07-05 조회 4,150
14-07-04 조회 8,532
14-07-04 조회 4,038
14-07-04 조회 3,552
14-07-04 조회 3,731
14-07-03 조회 3,872
14-07-03 조회 4,257
14-07-03 조회 4,701
14-07-02 조회 5,053
14-07-02 조회 3,943
14-07-02 조회 4,208
14-07-01 조회 3,970
14-07-01 조회 3,565
14-06-28 조회 5,210
14-06-28 조회 4,485
14-06-28 조회 6,248
14-06-27 조회 4,005
14-06-27 조회 3,859
14-06-27 조회 4,910
14-06-27 조회 5,572
14-06-26 조회 4,985
14-06-26 조회 6,141
14-06-26 조회 4,448
14-06-26 조회 6,526
14-06-26 조회 3,667
14-06-25 조회 3,958
14-06-24 조회 5,394
14-06-24 조회 4,011
14-06-24 조회 6,020
14-06-24 조회 6,159
14-06-24 조회 4,401
14-06-24 조회 5,108
14-06-20 조회 4,173
14-06-20 조회 4,468
14-06-20 조회 3,975
14-06-19 조회 5,195
14-06-18 조회 5,010
14-06-18 조회 4,141
14-06-18 조회 8,471
14-06-12 조회 5,620
14-06-12 조회 6,271
14-06-11 조회 5,186
14-06-10 조회 4,094
14-06-06 조회 5,642
14-06-05 조회 5,506
14-06-04 조회 5,102
14-05-29 조회 4,846
14-05-28 조회 3,991
14-05-27 조회 5,026
14-05-26 조회 5,538
14-05-26 조회 4,339
14-05-23 조회 5,763
14-05-21 조회 4,660
14-05-19 조회 7,615
14-05-18 조회 5,035