인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

인프런 커뮤니티 질문&답변

samkookji12님의 프로필 이미지

작성한 질문수

비전공자의 전공자 따라잡기 - 자료구조(with JavaScript)

스택, 큐 구현하기

LinkedList로 스택, 큐 구현하기 숙제

작성

·

59

0

스택 부분

class Stack {
  head=null;
  tail=null;
  length=0;

  push(value) {
   if (this.head) {
     this.tail.next = new Node(value);
     this.tail.next.prev = this.tail;
     this.tail = this.tail.next;

   } else {
     this.head = new Node(value);
     this.tail = this.head;
   }
   this.length++;
   return this.length;
  }

  pop() {
    let value = this.tail?.value;
    if (!this.tail) { // 값 존재 x 
      return null;
    }
    if (this.tail === this.head) { // 값이 하나
      this.head = null;
      this.tail = null;
    } else { // 값이 여러개
      this.tail = this.tail.prev;
      this.tail.next = null;
    }
    this.length--;
    return value;
  }
}

class Node {
  next = null;
  prev = null;
  constructor(value) {
    this.value = value;
  }
}

const stack = new Stack();

stack.push(1);
stack.push(3);
stack.push(5);
stack.push(2);
console.log(stack.push(4)); // length 리턴 5
console.log(stack.pop()); // 4
console.log(stack.pop()); // 2

console.log(stack.pop());

 

큐 부분

class Queue {
  head = null;
  tail = null;
  length = 0;

  enqueue(value) {
    if (this.head) {
      this.tail.next = new Node(value);
      this.tail.next.prev = this.tail;
      this.tail = this.tail.next;
    } else {
      this.head = new Node(value);
      this.tail = this.head;
    }
    this.length++;
    return this.length;
  }
  dequeue() {
    let value;

    if (!this.head) {
      return null;
    }
    if (this.head === this.tail) { // 한 개
      value = this.head.value;
      this.head = null;
      this.tail = null;
    } else { // 여러 개 삭제
      value = this.head.value;
      this.head = this.head.next;
      this.head.next.prev = null;
    }
    this.length--;
    return value;
  }
}

class Node {
  prev = null;
  next = null;
  constructor(value) {
    this.value = value;
  }
}

const queue = new Queue();
queue.enqueue(1); // 1
queue.enqueue(3); // 3
queue.enqueue(5); // 5
queue.enqueue(2); // 2
queue.enqueue(4); // 4
console.log(queue.enqueue(7)); // 7

console.log(queue.dequeue()); // 1
console.log(queue.dequeue()); // 3
console.log(queue.dequeue()); // 5
console.log(queue.dequeue()); // 2
console.log(queue.dequeue()); // 
console.log(queue.dequeue()); // 
console.log(queue.dequeue()); // 

 

큐 부분에서 콘솔 로그로 찍어 봤을 때 deque가 1,3,5,2 까지 진행 되고 그 이후에

this.head.next.prev = null;

^

TypeError: Cannot set properties of null (setting 'prev')
이런 에러가 발생하는데 이유가 궁금합니다.

답변 1

1

파블로님의 프로필 이미지

else { // 여러 개 삭제
      value = this.head.value;
      this.head = this.head.next;
      this.head.next.prev = null;
}

dequeue에서 위 로직을 점검해보세요. Queue 클래스에 아래 메서드를 추가하고, dequeue 실행한 뒤에 values를 호출해서 그 값을 콘솔에 출력해보면 뭐가 문제인지 쉽게 파악하실 수 있을 거예요.

values() {
	const values = [];

	let node = this.head;
	while (node !== null) {
		values.push({
			prev: node.prev?.value,
			curr: node.value,
			next: node.next?.value,
		});
		node = node.next;
	}

	return values;
}
samkookji12님의 프로필 이미지
samkookji12
질문자

감사합니다~~ 도움이 됐습니다!