[연결리스트/스택/JS] 1406 에디터

이승혜·2021년 8월 11일
0

알고리즘

목록 보기
6/6

🤯 내가 다시 보려고 쓰는 글

문제 링크

https://www.acmicpc.net/problem/1406

코드(연결 리스트)

const fs = require('fs');
const stdin = (process.platform === 'linux' ? fs.readFileSync('/dev/stdin').toString() : fs.readFileSync('input.txt').toString()).split(
  '\n'
);

const getLine = (() => {
  let line = 0;
  return () => stdin[line++];
})();


class Node {
  constructor() {
    this.data = -1;
    this.next = null;
    this.pre = null;
  }
}


class LinkedList {
  constructor() {
    this.head = null;
    this.cur = null;
  }

  init() {
    this.head = new Node();
    this.cur = this.head;
  }

  next() {
    if (this.cur.next == null)
      return;

    this.cur = this.cur.next;
  }

  pre() {
    if (this.cur.pre == null)
      return;

    this.cur = this.cur.pre;
  }

  del() {
    if (this.cur.pre == null)
      return;

    let delNode = this.cur.pre;

    if (delNode.pre != null)
      delNode.pre.next = delNode.next;

    if (delNode.next != null)
      delNode.next.pre = delNode.pre;
  }

  add(data) {
    let newNode = new Node();
    newNode.data = data
    newNode.next = this.cur;
    newNode.pre = this.cur.pre;

    if (this.cur.pre != null)
      this.cur.pre.next = newNode;

    this.cur.pre = newNode;
  }

  get() {
    if (this.cur == null)
      return;

    return this.cur.data;
  }

  isBegin() {
    return this.cur.pre == null;
  }

  begin() {
    this.cur = this.head;
  }
}


const solve = () => {
  const str = getLine().split('');

  const linkedList = new LinkedList();
  linkedList.init();

  for (const c of str) {
    linkedList.add(c);
  }

  const n = Number(getLine());
  for (let i = 0; i < n; ++i) {
    const query = getLine().split(' ');

    switch (query[0]) {
      case 'L':
        linkedList.pre();
        break;
      case 'D':
        linkedList.next();
        break;
      case 'B':
        linkedList.del();
        break;
      case 'P':
        linkedList.add(query[1]);
        break;

    }
  }

  let ans = [];

  linkedList.begin();
  while (!linkedList.isBegin()) {
    linkedList.pre();
    ans.push(linkedList.get());
  }

  console.log(ans.reverse().join(''));
};

const main = () => {
  solve();
};

main();

코드(스택)

const fs = require('fs');
const stdin = (process.platform === 'linux' ? fs.readFileSync('/dev/stdin').toString() : fs.readFileSync('input.txt').toString()).split(
  '\n'
);

const getLine = (() => {
  let line = 0;
  return () => stdin[line++];
})();

const solve = () => {
  const str = getLine().split('');

  const left = [];
  const right = [];

  for (const c of str) {
    left.push(c);
  }

  const n = Number(getLine());
  for (let i = 0; i < n; ++i) {
    const query = getLine().split(' ');

    switch (query[0]) {
      case 'L':
        if (left.length == 0)
          continue;

        right.push(left.pop());
        break;
      case 'D':
        if (right.length == 0)
          continue;

        left.push(right.pop());
        break;
      case 'B':
        if (left.length == 0)
          continue;

        left.pop();
        break;
      case 'P':
        left.push(query[1]);
        break;
    }
  }

  while (right.length) {
    left.push(right.pop());
  }

  console.log(left.join(''));
};

const main = () => {
  solve();
};

main();
profile
더 높이

0개의 댓글