JS) stack,Queue

6538 ワード

1. stack


2. Queue


1. stack

  • データが順次積む構造
  • .
  • が最初に入ったデータが最も遅く現れた.
  • スタックは初めてアウトバウンドし、最近追加されたアイテムは最後に追加されました.
  • プリングストン
  • Stack例
    !)Webブラウザアクセス履歴
    !!)元に戻す(ctrl+Z)
    !!!)逆シーケンス文字列の作成
  • EX)
    //스택을 만드는 클래스 
    class Makestack {
      constructor() {
        this.storage = {};
        this.top = 0; //스택을 분별하기 위한 키값
      } 
    
      push(element) {
        this.storage[this.top] = element;
        this.top++; //스택이 추가되면 카운트가 하나씩 없어짐
      }	
      pop() {
        if (Object.keys(this.storage).length === 0) {
          return 'none stack'
        }
    
        const result = this.storage[Object.keys(this.storage).length-1];
        delete this.storage[Object.keys(this.storage).length-1];
        this.top--;//스택이 삭제되면 카운트도 -1되어 다음 스택의 카운트에 영향을 주지 않아야함
        return result;
      }
    }
    

    2. Queue

  • データが順次積む構造
  • .
  • 先入力データのフォーマット
  • スタックとは異なるFirstin Fist Out形式を形成する.
  • Queue例
    !)
  • データを処理する能力のみを提供
    EX)
    //큐을 만드는 클래스 
    class MakeQueue {
      constructor(){
        this.storage =[]
      }
    
      inputdata(element){
        this.storage.push(element)//맨끝에 데이터가 추가된다.
      }
    
      outputdata(){
        this.storage.shift//가장 앞의 데이터가 빠져나간다. 
      }
    }