슈

[알고리즘] 단순 연결 리스트를 이용한 스택(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-10-29 조회 7,919
13-10-28 조회 8,749
13-10-28 조회 3,931
13-10-28 조회 3,476
13-10-27 조회 3,096
13-10-26 조회 3,815
13-10-25 조회 3,547
13-10-24 조회 3,510
13-10-24 조회 3,254
13-10-24 조회 3,578
13-10-24 조회 4,156
13-10-23 조회 4,322
13-10-23 조회 3,583
13-10-22 조회 4,812
13-10-21 조회 3,333
13-10-20 조회 3,661
13-10-18 조회 4,587
13-10-17 조회 4,964
13-10-15 조회 4,843
13-10-11 조회 4,700
13-10-10 조회 5,309
13-10-10 조회 3,777
13-10-08 조회 4,302
13-10-03 조회 6,383
13-10-02 조회 4,246
13-10-02 조회 4,518
13-10-02 조회 4,293
13-10-02 조회 3,773
13-10-02 조회 4,029
13-10-02 조회 3,881
13-10-02 조회 4,239
13-09-29 조회 4,798
13-09-29 조회 4,246
13-09-29 조회 4,121
13-09-29 조회 6,707
13-09-29 조회 4,335
13-09-27 조회 3,646
13-09-27 조회 4,286
13-09-24 조회 3,340
13-09-24 조회 3,621
13-09-18 조회 4,036
13-09-17 조회 3,901
13-09-16 조회 8,102
13-09-16 조회 5,465
13-09-16 조회 4,429
13-09-15 조회 3,272
13-09-13 조회 4,970
13-09-13 조회 3,449
13-09-13 조회 3,742
13-09-12 조회 3,540
13-09-11 조회 6,337
13-09-10 조회 3,604
13-09-10 조회 6,196
13-09-10 조회 3,967
13-09-09 조회 4,340
13-09-09 조회 4,986
13-09-07 조회 5,091
13-09-07 조회 3,173
13-09-06 조회 3,295
13-09-06 조회 3,916
13-09-06 조회 3,276
13-09-05 조회 3,457
13-09-05 조회 9,644
13-09-04 조회 3,153
13-09-04 조회 3,417
13-09-04 조회 3,100
13-09-03 조회 3,192
13-09-03 조회 3,607
13-08-30 조회 3,436
13-08-30 조회 4,522
13-08-29 조회 4,479
13-08-28 조회 3,989
13-08-28 조회 8,202
13-08-27 조회 4,161
13-08-26 조회 4,365
13-08-26 조회 3,593
13-08-26 조회 4,886
13-08-23 조회 4,181
13-08-23 조회 5,940
13-08-22 조회 5,465
13-08-22 조회 4,693
13-08-22 조회 5,190
13-08-22 조회 4,767
13-08-22 조회 4,682
13-08-22 조회 3,666
13-08-20 조회 3,925
13-08-18 조회 8,624
13-08-18 조회 5,524
13-08-17 조회 6,163
13-08-17 조회 4,991
13-08-17 조회 4,433
13-08-14 조회 3,458
13-08-12 조회 4,464
13-08-11 조회 3,641
13-08-09 조회 4,152
13-08-08 조회 5,381
13-08-08 조회 5,129
13-08-08 조회 4,520
13-08-08 조회 9,427
13-08-08 조회 5,625