[알고리즘] 단순 연결 리스트를 이용한 스택(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 

*/

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

프로그램

+
제목 글쓴이 날짜 조회
11년 전 조회 2,674
11년 전 조회 2,904
11년 전 조회 1,278
11년 전 조회 2,149
11년 전 조회 1,667
11년 전 조회 4,296
11년 전 조회 2,214
11년 전 조회 2,119
11년 전 조회 2,366
11년 전 조회 4,336
11년 전 조회 4,288
11년 전 조회 4,078
11년 전 조회 1,759
11년 전 조회 4,138
11년 전 조회 3,395
11년 전 조회 3,877
11년 전 조회 1,387
11년 전 조회 3,191
11년 전 조회 3,184
11년 전 조회 3,250
11년 전 조회 2,238
11년 전 조회 2,705
11년 전 조회 2,029
11년 전 조회 1,825
11년 전 조회 2,440
11년 전 조회 1,752
11년 전 조회 4,623
11년 전 조회 4,472
11년 전 조회 2,060
11년 전 조회 3,282
11년 전 조회 1,676
11년 전 조회 2,487
11년 전 조회 4,089
11년 전 조회 4,408
11년 전 조회 5,307
11년 전 조회 1,682
11년 전 조회 2,259
11년 전 조회 3,668
11년 전 조회 1,845
11년 전 조회 1,853
11년 전 조회 2,430
11년 전 조회 1,730
11년 전 조회 2,956
11년 전 조회 2,472
11년 전 조회 4,554
11년 전 조회 2,994
11년 전 조회 5,246
11년 전 조회 2,049
11년 전 조회 1,941
11년 전 조회 2,597
11년 전 조회 2,558
11년 전 조회 2,128
11년 전 조회 1,717
11년 전 조회 2,385
11년 전 조회 1,896
11년 전 조회 1,886
11년 전 조회 2,077
11년 전 조회 2,009
11년 전 조회 1,632
11년 전 조회 2,824
11년 전 조회 2,803
11년 전 조회 3,932
11년 전 조회 1,942
11년 전 조회 1,699
11년 전 조회 1,680
11년 전 조회 3,625
11년 전 조회 1,805
11년 전 조회 2,139
11년 전 조회 1,660
11년 전 조회 2,302
11년 전 조회 2,278
11년 전 조회 1,689
11년 전 조회 1,862
11년 전 조회 1,704
11년 전 조회 1,660
11년 전 조회 2,335
11년 전 조회 1,779
11년 전 조회 1,695
11년 전 조회 1,853
11년 전 조회 1,824
11년 전 조회 1,541
11년 전 조회 1,721
11년 전 조회 2,110
11년 전 조회 1,771
11년 전 조회 2,016
11년 전 조회 1,763
11년 전 조회 1,716
11년 전 조회 1,950
11년 전 조회 1,693
11년 전 조회 1,705
11년 전 조회 1,785
11년 전 조회 2,682
11년 전 조회 1,737
11년 전 조회 1,740
11년 전 조회 1,539
11년 전 조회 1,994
11년 전 조회 1,732
11년 전 조회 1,542
11년 전 조회 1,985
11년 전 조회 2,200