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

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

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

번호 제목 글쓴이 날짜 조회 수
246 [Gemini] ajax 와 fetch 의 차이 황제낙엽 2025.01.04 3419
245 [검토 완료] 디바이스 카메라로 촬영한 사진을 서버에 업로드하기 샘플 황제낙엽 2024.11.21 3988
244 브라우저에서 이미지를 편집(crop 등) 할 수 있는 오픈소스 Cropper.js 황제낙엽 2024.11.16 3842
243 [ChatGPT] json data 의 정렬 [1] 황제낙엽 2024.07.23 3357
242 DOMContentLoaded 이벤트와 window.onload 이벤트 황제낙엽 2024.07.15 3673
241 fetch() 사용 예제들 황제낙엽 2024.07.09 3382
240 [Copilot] Vanilla JavaScript에서 외부 스크립트 파일에 정의된 함수들을 로드 황제낙엽 2024.06.21 3759
239 [Copilot] ES6 모듈(module) 문법 황제낙엽 2024.06.21 3812
238 [Copilot] JSON 객체 내부 데이터를 순회하면서 조회하는 코드 황제낙엽 2024.06.02 3272
237 [Copilot] JavaScript로 서버에 특정 값을 POST 방식으로 전달하고 응답을 기다리지 않고 바로 종료 황제낙엽 2024.05.31 3620
236 json 데이터 내의 변수명에 prefix 로 type 표현하기 황제낙엽 2024.04.15 3363
235 fetch() 함수 사용 예제 file 황제낙엽 2023.11.23 3607
234 현재 document 의 host 와 port 를 얻는 방법 황제낙엽 2023.10.03 4211
233 (Bard) FileReader 로 여러개의 파일을 read 하는 법 file 황제낙엽 2023.08.23 3665
232 How to build a file upload service with vanilla JavaScript file 황제낙엽 2023.08.22 3715
231 (Bard) JavaScript로 JSON 배열을 작성하는 방법 황제낙엽 2023.08.21 3674
230 모바일 브라우저에서 file input element 를 이용하여 여러장의 이미지를 서버에 전송하려 할때 [1] 황제낙엽 2023.08.21 3704
229 navigator.mediaDevices 황제낙엽 2023.08.21 3799
228 Barcode Detection API 황제낙엽 2023.08.06 3859
227 정규식을 이용한 이메일 검증 스크립트 file 황제낙엽 2023.06.25 2895