일반 Stack (스택) 예제 프로그램

황제낙엽 2012.12.27 03:19 조회 수 : 387

sitelink1  
sitelink2  
sitelink3  
sitelink4 http://1 
extra_vars4 ko 
extra_vars5 ko 
extra_vars6 sitelink1 

/**
    스택(Stack) 자료구조 입니다.
    스택 생성시 스택의 크기를 정해놓고 생성을 하며, 그 이상의 데이터를 넣을 수 없습니다.
    
    push    : 스택에 데이터 넣기
    pop     : 스택에서 데이터 빼기
    reset   : 스택 초기화
    isFull  : 스택이 꽉 찼는지 여부
    isEmpty : 스택이 비어있는지 여부
    count   : 스택에 넣어진 데이터의 개수
 */
function Stack(capacity){
    this.capacity = capacity+1;
    this.buffer = new Array(this.capacity);
    this.topIndex = -1;
};
Stack.prototype.push = function(obj){
    if( false==this.isFull() ){
        this.topIndex++;
        this.buffer[this.topIndex] = obj;
        return true;
    }
    return false;
};
Stack.prototype.pop = function(){
    if( false==this.isEmpty() ){
        ret = this.buffer[this.topIndex];
        this.buffer[this.topIndex] = undefined;
        this.topIndex--;
        return ret;
    }
    return null;
};
Stack.prototype.reset = function(){
    this.topIndex = -1;
}
Stack.prototype.isFull = function(){
    if( (this.topIndex+1)>=this.capacity ){
        return true;
    }
    return false;
}
Stack.prototype.isEmpty = function(){
    if( this.topIndex==-1 ){
        return true;
    }
    return false;
}
Stack.prototype.count = function(){
    return (this.topIndex+1);
}
Stack.prototype.toString = function(){
    str = "";
    str += "capacity : "+this.capacity;
    str += ", count : "+this.count();
    str += ", topIndex : "+this.topIndex;
    str += ", [";
    for(index=this.topIndex; index>-1; index--){
        str += this.buffer[index];
        if( index>0 ){
            str += ", ";
        }
    }
    str += "]";
    return str;
}
/**
    스택 예제코드
 */
var stack = new Stack(8);
 
console.log(stack.toString());
console.log("push 'A': "+stack.push("A"));
console.log("push 'B': "+stack.push("B"));
console.log("push 'C': "+stack.push("C"));
console.log("push 'D': "+stack.push("D"));
console.log("push 'E': "+stack.push("E"));
console.log("push 'F': "+stack.push("F"));
console.log("push 'G': "+stack.push("G"));
console.log("push 'H': "+stack.push("H"));
console.log("push 'I': "+stack.push("I"));
console.log("push 'J': "+stack.push("J"));
console.log("push 'K': "+stack.push("K"));
console.log(stack.toString());
console.log("pop : "+stack.pop());
console.log("pop : "+stack.pop());
console.log(stack.toString());
console.log("push '1': "+stack.push("1"));
console.log("push '2': "+stack.push("2"));
console.log("push '3': "+stack.push("3"));
console.log(stack.toString());
stack.reset();
console.log(stack.toString());

번호 제목 글쓴이 날짜 조회 수
166 XMLHttpRequest.setRequestHeader 황제낙엽 2013.09.30 407
165 HTTP Content-Type 정리 황제낙엽 2013.09.30 431
164 getBoundingClientRect in FF3 file 황제낙엽 2013.01.11 409
163 연속해서 스트림 받기 (flush data from servlet to jsp with ajax) 황제낙엽 2013.01.04 2814
» Stack (스택) 예제 프로그램 황제낙엽 2012.12.27 387
161 상속과 Super 로의 접근 황제낙엽 2012.09.18 380
160 inherits() 를 이용한 상속 황제낙엽 2012.07.18 412
159 Javascript delete 황제낙엽 2012.06.11 421
158 delete 연산자에 대한 고찰 황제낙엽 2012.06.11 438
157 자바스크립트의 쉬프트 연산자 (Shift Operator) 와 음수 (Negative) 이야기 황제낙엽 2012.05.31 1052
156 연산자 (===, ==, >=, <=) 황제낙엽 2012.05.30 391
155 XMLHttpRequest 의 이벤트 onreadystatechange 황제낙엽 2012.05.30 673
154 JavaScript 재입문 황제낙엽 2012.05.29 372
153 진행 상황 추적하기(XMLHttpRequest.readyState) file 황제낙엽 2012.05.23 637
152 JavaScript Touch and Gesture Events iPhone and Android 황제낙엽 2012.04.12 750
151 Javascript ArrayBuffer ? Binary handling in javascript 황제낙엽 2012.03.19 724
150 오류:호출자(서버 응용 프로그램이 아닌 서버)가 사용될 수 없어서 사라졌습니다. file 황제낙엽 2012.03.14 2190
149 Alert 에서의 개행처리 황제낙엽 2012.03.09 397
148 env.js 사용시 부족한 부분 file 황제낙엽 2012.02.15 334
147 Rhino 와 env.js 를 사용해서 자바 서버에서 javascript 를 구동해보자 file 황제낙엽 2012.02.15 398