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

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

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());

번호 제목 글쓴이 날짜 조회 수
90 브라우저의 새로고침과 종료에 대한 이벤트 황제낙엽 2017.08.11 2725
89 자바스크립트 타입 비교 테이블 + 테이블 작성 스크립트 [1] file 황제낙엽 2017.06.23 85
88 |= 비트 OR 대입 연산자 (복합대입연산자) 황제낙엽 2017.03.15 73
87 Jasmine 테스트 및 CI 구축 가이드 황제낙엽 2016.11.16 254
86 QUnit을 이용한 JavaScript 단위 테스트 file 황제낙엽 2016.11.16 36
85 멤버 연산자 황제낙엽 2014.12.30 47
84 연산자 this 황제낙엽 2014.12.30 23
83 typeof 와 instanceof의 차이, 타입 또는 클래스 구분하기 황제낙엽 2013.10.24 38
82 HTTP Content-Type 정리 황제낙엽 2013.09.30 68
81 getBoundingClientRect in FF3 file 황제낙엽 2013.01.11 36
» Stack (스택) 예제 프로그램 황제낙엽 2012.12.27 27
79 Javascript delete 황제낙엽 2012.06.11 20
78 delete 연산자에 대한 고찰 황제낙엽 2012.06.11 42
77 자바스크립트의 쉬프트 연산자 (Shift Operator) 와 음수 (Negative) 이야기 황제낙엽 2012.05.31 726
76 연산자 (===, ==, >=, <=) 황제낙엽 2012.05.30 47
75 JavaScript 재입문 황제낙엽 2012.05.29 50
74 JavaScript Touch and Gesture Events iPhone and Android 황제낙엽 2012.04.12 337
73 Javascript ArrayBuffer ? Binary handling in javascript 황제낙엽 2012.03.19 218
72 Alert 에서의 개행처리 황제낙엽 2012.03.09 80
71 자바스크립트 숫자형 체크 함수 (isFinite() 함수 와 isNaN() 함수) 황제낙엽 2011.12.13 67