[알고리즘] 단순 연결 리스트를 이용한 스택(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년 전 조회 1,890
11년 전 조회 2,147
11년 전 조회 534
11년 전 조회 1,403
11년 전 조회 915
11년 전 조회 3,513
11년 전 조회 1,353
11년 전 조회 1,320
11년 전 조회 1,462
11년 전 조회 3,571
11년 전 조회 3,533
11년 전 조회 3,351
11년 전 조회 1,006
11년 전 조회 3,380
11년 전 조회 2,582
11년 전 조회 3,152
11년 전 조회 636
11년 전 조회 2,401
11년 전 조회 2,382
11년 전 조회 2,459
11년 전 조회 1,438
11년 전 조회 1,913
11년 전 조회 1,240
11년 전 조회 1,053
11년 전 조회 1,632
11년 전 조회 957
11년 전 조회 3,836
11년 전 조회 3,611
11년 전 조회 1,238
11년 전 조회 2,481
11년 전 조회 907
11년 전 조회 1,705
11년 전 조회 3,310
11년 전 조회 3,614
11년 전 조회 4,540
11년 전 조회 937
11년 전 조회 1,502
11년 전 조회 2,908
11년 전 조회 1,078
11년 전 조회 1,069
11년 전 조회 1,680
11년 전 조회 938
11년 전 조회 2,216
11년 전 조회 1,719
11년 전 조회 3,798
11년 전 조회 2,261
11년 전 조회 4,513
11년 전 조회 1,286
11년 전 조회 1,145
11년 전 조회 1,785
11년 전 조회 1,751
11년 전 조회 1,322
11년 전 조회 972
11년 전 조회 1,624
11년 전 조회 995
11년 전 조회 1,090
11년 전 조회 1,305
11년 전 조회 1,121
11년 전 조회 878
11년 전 조회 2,059
11년 전 조회 1,873
11년 전 조회 3,047
11년 전 조회 1,022
11년 전 조회 785
11년 전 조회 878
11년 전 조회 2,760
11년 전 조회 1,012
11년 전 조회 1,207
11년 전 조회 725
11년 전 조회 1,504
11년 전 조회 1,499
11년 전 조회 906
11년 전 조회 1,090
11년 전 조회 770
11년 전 조회 732
11년 전 조회 1,555
11년 전 조회 893
11년 전 조회 791
11년 전 조회 906
11년 전 조회 1,089
11년 전 조회 739
11년 전 조회 782
11년 전 조회 1,288
11년 전 조회 827
11년 전 조회 1,312
11년 전 조회 828
11년 전 조회 969
11년 전 조회 1,009
11년 전 조회 757
11년 전 조회 784
11년 전 조회 1,020
11년 전 조회 1,955
11년 전 조회 791
11년 전 조회 803
11년 전 조회 742
11년 전 조회 1,159
11년 전 조회 802
11년 전 조회 724
11년 전 조회 1,044
11년 전 조회 1,380
🐛 버그신고