0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

paizaラーニング レベルアップ問題集 スタック・キューメニュー JavaScript キュー実装編

Posted at

STEP: 1 キュー実装編 step 1

キューから要素を削除するプログラムを実装します。

解答例

キューは先出しですので、shift()と使います。

const fs = require("fs");
const input = fs.readFileSync("/dev/stdin", "utf-8").trim();
const lines = input.split("\n");

const [Q] = lines[0].split(" ").map(Number);

//空の配列A
let A = [];

//クエリを順に処理
for (let i = 1; i <= Q; i++) {
  const [query, X] = lines[i].split(" ");
  if (query === "1") {
    A.push(X);
  } else {
    A.shift();
  }
  //各クエリの処理が終わったあと、配列 A の各要素の値を半角スペース区切りで出力
  console.log(A.join(" "));
}

FINAL問題 キュー実装編 step 2

キューを完成させます。

解答例

shiftは先頭の要素を返します。

const fs = require("fs");
const input = fs.readFileSync("/dev/stdin", "utf-8").trim();
const lines = input.split("\n");

const [Q] = lines[0].split(" ").map(Number);

//空の配列A
let A = [];

//クエリを順に処理
for (let i = 1; i <= Q; i++) {
  const [query, X] = lines[i].split(" ");
  if (query === "1") {
    A.push(X);
  } else {
    console.log(A.shift());//先頭を削除して出力
  }
  //各クエリの処理が終わったあと、配列 A の各要素の値を半角スペース区切りで出力
  console.log(A.join(" "));
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?