// 배열로 구현하는 스택
3
2
1
Pop 3
Stack contents : Top ----> Bottom
2
1
Push 4, 5, 6
Stack contents : Top ----> Bottom
6
5
4
2
1
Now stack is full, push 7
Stack overflow.
Stack contents : Top ----> Bottom
6
5
4
2
1
Initialize stack
Stack contents : Top ----> Bottom
Now stack is empty
Pop
Stack underflow.
Push 7, 8
Stack contents : Top ----> Bottom
8
7
// 스택 최대크기 5
define('MAX', 5);
$top = 0;
$stack = array();
function init_stack() {
Global $top;
$top = -1;
}
function push($t) {
Global $top, $stack;
if ($top >= MAX - 1) {
println('Stack overflow.');
return -1;
}
$stack[++$top] = $t;
}
function pop() {
Global $top, $stack;
if ($top < 0) {
println('Stack underflow.');
return -1;
}
return $stack[$top--];
}
function print_stack() {
Global $top, $stack;
println('Stack contents : Top ----> Bottom');
for ($i = $top; $i >= 0; $i--)
println($stack[$i]);
}
init_stack();
println('Push 1, 2, 3');
push(1);
push(2);
push(3);
print_stack();
println();
$i = pop();
println("Pop ". $i);
print_stack();
println();
println('Push 4, 5, 6');
push(4);
push(5);
push(6);
print_stack();
println();
println('Now stack is full, push 7');
push(7);
print_stack();
println();
println('Initialize stack');
init_stack();
print_stack();
println();
println('Now stack is empty');
println('Pop');
pop();
println('Push 7, 8');
push(7);
push(8);
print_stack();
println();
function println($val='') {
echo $val.'<br />';
}
/* output
Push 1, 2, 3
Stack contents : Top ----> Bottom3
2
1
Pop 3
Stack contents : Top ----> Bottom
2
1
Push 4, 5, 6
Stack contents : Top ----> Bottom
6
5
4
2
1
Now stack is full, push 7
Stack overflow.
Stack contents : Top ----> Bottom
6
5
4
2
1
Initialize stack
Stack contents : Top ----> Bottom
Now stack is empty
Pop
Stack underflow.
Push 7, 8
Stack contents : Top ----> Bottom
8
7
*/
댓글 3개
게시글 목록
| 번호 | 제목 |
|---|---|
| 6256 | |
| 6255 | |
| 6254 | |
| 19430 | |
| 6253 | |
| 6252 | |
| 27670 | |
| 6249 | |
| 6246 | |
| 6242 | |
| 6241 | |
| 19429 |
JavaScript
자바스크립트로 이미지 롤링 관련 문의드립니다.
|
| 6238 | |
| 6237 | |
| 19426 | |
| 19413 | |
| 19411 | |
| 6232 | |
| 6227 | |
| 6226 | |
| 6224 | |
| 19408 | |
| 6223 | |
| 6221 | |
| 6220 | |
| 27663 | |
| 6213 | |
| 6212 | |
| 27661 | |
| 27659 | |
| 6209 | |
| 6203 | |
| 6200 | |
| 27654 | |
| 6194 | |
| 6188 | |
| 6180 | |
| 6173 | |
| 6150 | |
| 19407 | |
| 6145 | |
| 6141 | |
| 6138 | |
| 6131 | |
| 27650 | |
| 6128 | |
| 6125 | |
| 6122 | |
| 27648 | |
| 6119 | |
| 6114 | |
| 6109 | |
| 19402 |
JavaScript
위지윅 에디터 만들때 선택영역 풀리는 문제
4
|
| 6108 | |
| 27647 | |
| 6106 | |
| 27642 | |
| 27637 | |
| 6105 | |
| 6101 | |
| 6095 | |
| 6093 | |
| 6091 | |
| 6088 | |
| 27635 | |
| 27634 | |
| 6087 | |
| 27628 | |
| 6083 | |
| 6077 | |
| 24562 | |
| 27614 | |
| 6072 | |
| 6067 | |
| 6065 | |
| 19400 | |
| 19399 |
기타
IE 구분하기
|
| 19398 |
기타
js, css 캐싱 팁
|
| 19397 | |
| 6062 | |
| 27608 | |
| 6057 | |
| 27606 | |
| 19396 |
JavaScript
롤링 배너 이해하는데 도움이 될것 같아 올립니다
|
| 6054 | |
| 27600 | |
| 6048 | |
| 6040 | |
| 6031 | |
| 6028 | |
| 6024 | |
| 27594 | |
| 27593 | |
| 6023 | |
| 6018 | |
| 19395 | |
| 19394 |
PHP
게시판 페이지 관련 글입니다.
|
| 19390 | |
| 19388 |
JavaScript
멀티 체크박스입니다.
1
|
| 19386 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기