データ構造スタックTILについて


スタックデータ構造フィーチャー
  • LIFO (Last In First Out)
  • スタックデータ構造で最後に追加された値は、最初に削除されます.
  • 以上の資料構造はいつ使用されますか.
  • 関数呼管理
  • 操作中に元の状態に復帰する場合又はやり直した場合(やり直し)
  • .
  • ルーティング履歴もスタックとして処理される.
  • スタックデータ構造のbigoタグ法
  • 挿入O(1)
  • 削除
  • O(1)
  • 探索O(N)
  • アクセスO(N)
  • スタック実装で私が混同した部分(link-listを使用)


    push(3)


    1)3の値を持つノードを作成します.

    2)スタックの上部を追加したノードに向けます.

    push(4)


    1)4の値を持つノードを作成する

    2)追加したノードのnextにスタックの上部を指すノードを与える.

    3)スタックの上部を追加したノードに向けます.
    class Node {
      constructor(value) {
        this.= value;
        this.next = null;
      }
    }
    
    class Stack {
      constructor() {
        this.top = null;
      }
    
      push(value) {
        //1
        const 추가되는노드 = new Node(value);
        //2
        추가되는노드.next = this.top;
        //3
        this.top = 추가되는노드;
      }
    
      pop() {
        if (this.isEmpty()) throw new Error("this is empty");
        const temp = this.top;
        this.top = temp.next;
        return temp.value;
      }
    
      peek() {
        if (this.isEmpty()) throw new Error("this is empty");
        return this.top.value;
      }
    
      isEmpty() {
        return this.top === null;
      }
    }
    
    
    
    変数は変数を付与するのではなく、「その値」を付与します.