슈

[알고리즘] 단순 연결 리스트를 이용한 스택(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,188건
+
제목 글쓴이 날짜 조회
12-08-29 조회 4,181
12-08-07 조회 3,842
12-08-03 조회 3,653
12-07-31 조회 3,759
12-07-30 조회 3,709
12-07-16 조회 3,911
12-07-15 조회 3,747
12-07-07 조회 3,606
12-07-06 조회 3,805
12-07-03 조회 3,765
12-06-29 조회 3,382
12-06-22 조회 3,755
12-06-17 조회 4,012
12-05-21 조회 3,937
12-05-02 조회 3,457
12-05-02 조회 4,248
12-05-02 조회 3,372
12-04-30 조회 3,570
12-04-30 조회 3,254
12-03-17 조회 4,123
12-03-10 조회 3,449
12-03-08 조회 3,974
12-03-03 조회 3,687
12-02-27 조회 5,106
12-02-17 조회 4,394
12-02-16 조회 4,678
12-02-03 조회 3,342
12-01-31 조회 3,252
12-01-24 조회 3,250
12-01-17 조회 3,956
12-01-05 조회 3,497
11-12-30 조회 3,230
11-12-23 조회 4,785
11-12-08 조회 3,556
11-12-07 조회 4,513
11-11-27 조회 3,942
11-11-25 조회 4,268
11-11-23 조회 3,711
11-11-22 조회 3,569
11-11-09 조회 3,278
11-10-23 조회 3,219
11-10-19 조회 3,175
11-10-08 조회 2,857
11-10-04 조회 2,858
11-10-04 조회 5,316
11-09-01 조회 4,395
11-08-13 조회 2,568
11-08-06 조회 3,251
11-07-11 조회 3,647
11-06-27 조회 3,004
11-06-21 조회 2,669
11-06-13 조회 2,476
11-06-07 조회 2,486
11-05-26 조회 2,443
11-04-20 조회 2,776
11-03-21 조회 2,872
11-03-18 조회 2,564
11-03-11 조회 2,583
11-02-07 조회 2,409
11-01-18 조회 2,732
11-01-12 조회 2,457
11-01-06 조회 2,549
10-12-10 조회 2,805
10-12-09 조회 2,658
10-12-08 조회 5,090
10-11-29 조회 2,924
10-11-28 조회 2,828
10-11-28 조회 2,659
10-11-19 조회 2,854
10-11-04 조회 2,728
10-10-21 조회 2,454
10-10-19 조회 3,412
10-10-12 조회 2,737
10-10-07 조회 2,574
10-09-30 조회 3,527
10-09-17 조회 3,939
10-09-17 조회 3,886
10-05-31 조회 3,595
09-12-31 조회 3,729
09-07-23 조회 5,056
08-09-16 조회 3,254
08-09-06 조회 3,000
08-09-04 조회 3,421
08-08-16 조회 4,451
08-08-15 조회 3,316
08-08-14 조회 4,533
08-08-12 조회 4,002
08-07-23 조회 2,818
08-07-10 조회 3,766
08-07-04 조회 2,997
08-06-27 조회 7,632
08-06-19 조회 4,945
08-05-27 조회 5,548
08-05-25 조회 5,382
08-05-23 조회 3,463
08-05-23 조회 4,629
08-05-21 조회 4,976
08-05-20 조회 4,092
08-05-20 조회 3,314
08-04-18 조회 4,389