[알고리즘] 단순 연결 리스트를 이용한 스택(stack)

· 2014-09-24 (수) 18:35:58 · 4660 · 1

/*

연결 리스트, 링크드 리스트(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 

*/

|

댓글 1개

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

프로그램

8,188건
+
제목 글쓴이 날짜 조회
14-09-19 조회 3,470
14-09-19 조회 3,610
14-09-19 조회 3,955
14-09-19 조회 3,721
14-09-19 조회 3,996
14-09-19 조회 3,650
14-09-19 조회 3,688
14-09-19 조회 3,817
14-09-19 조회 3,572
14-09-19 조회 3,577
14-09-19 조회 3,708
14-09-19 조회 4,603
14-09-19 조회 3,624
14-09-18 조회 3,620
14-09-18 조회 3,495
14-09-18 조회 3,942
14-09-18 조회 3,671
14-09-18 조회 3,492
14-09-18 조회 3,835
14-09-18 조회 4,236
14-09-18 조회 3,827
14-09-18 조회 3,856
14-09-18 조회 3,848
14-09-18 조회 4,620
14-09-18 조회 3,786
14-09-18 조회 3,833
14-09-18 조회 5,220
14-09-18 조회 5,136
14-09-18 조회 3,934
14-09-18 조회 4,469
14-09-18 조회 3,920
14-09-18 조회 3,896
14-09-18 조회 4,088
14-09-18 조회 4,665
14-09-17 조회 3,701
14-09-16 조회 4,661
14-09-16 조회 4,887
14-09-16 조회 3,636
14-09-15 조회 3,784
14-09-15 조회 3,418
14-09-13 조회 4,432
14-09-12 조회 4,313
14-09-12 조회 3,712
14-09-12 조회 3,607
14-09-11 조회 3,320
14-09-11 조회 4,061
14-09-11 조회 4,297
14-09-10 조회 3,236
14-09-10 조회 3,420
14-09-03 조회 3,654
14-09-03 조회 3,594
14-09-03 조회 3,828
14-09-03 조회 5,370
14-09-01 조회 5,155
14-09-01 조회 4,553
14-09-01 조회 4,131
14-08-30 조회 4,119
14-08-30 조회 5,205
14-08-29 조회 4,351
14-08-28 조회 5,658
14-08-28 조회 6,420
14-08-28 조회 6,587
14-08-27 조회 6,649
14-08-27 조회 6,758
14-08-27 조회 5,346
14-08-27 조회 2만
14-08-27 조회 4,076
14-08-26 조회 3,969
14-08-26 조회 3,955
14-08-26 조회 3,707
14-08-26 조회 3,848
14-08-26 조회 3,672
14-08-26 조회 3,413
14-08-26 조회 3,927
14-08-26 조회 3,809
14-08-26 조회 3,658
14-08-26 조회 3,773
14-08-26 조회 3,844
14-08-26 조회 3,629
14-08-26 조회 3,836
14-08-26 조회 3,803
14-08-26 조회 3,782
14-08-26 조회 3,847
14-08-26 조회 3,585
14-08-26 조회 3,592
14-08-26 조회 3,776
14-08-26 조회 3,473
14-08-26 조회 3,548
14-08-26 조회 3,343
14-08-26 조회 3,364
14-08-26 조회 3,514
14-08-26 조회 4,077
14-08-26 조회 3,433
14-08-26 조회 3,682
14-08-26 조회 3,621
14-08-26 조회 3,386
14-08-26 조회 3,411
14-08-26 조회 3,449
14-08-26 조회 3,473
14-08-26 조회 4,107