inflearn logo
강의

Course

Instructor

Non-majors catching up with majors - Data Structures (with JavaScript)

Implementing stacks and queues

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

123

samkookji12

8 asked

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')
이런 에러가 발생하는데 이유가 궁금합니다.

javascript 코딩-테스트 알고리즘

Answer 1

1

hestonpablo

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;
}

0

samkookji12

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

2주차 개념#12 트리 순회

0

13

2

프론트엔드 학습 수준 문의

0

21

2

백준 사이트 서비스 종료

0

36

3

잠겨버린 사물함 시간초과 관련 질문입니다.

0

17

1

스택, 큐 연결리스트로 구현 과제 완료입니다!

0

93

1

heapify 안의 bigger 삼항연산자 질문

0

116

2

linkedList prev와 tail 사용 후 o(1) 구현.

0

165

1

숙제 : LinkedList로 Stack, Queue 구현하기

0

177

1

연결리스트 숙제

0

239

1

한번에 이해 안가는 제가 비정상 일까요...?

0

234

1

우선순위 큐 질문이 있습니다!

0

154

1

[숙제] minHeap 구현, maxHeap -> minHeap , minHeap -> maxHeap

1

235

1

최소힙 remove 구현하기

0

245

1

숙제 최소힙 만들기

0

152

1

숙제 length return 하기

0

194

1

숙제 : 같은 값을 넣은경우 에러 처리

0

181

1

영상 중간에 0:10 1:23초 수정에 따른 코드 최종본

1

143

1

숙제2 연결리스트를 이용하여 큐 구현하기

0

237

1

숙제1 LinkedList로 스택 구현하기

1

251

1

연결 리스트 구현 숙제 리뷰 부탁드려봅니다

1

345

2

let current = this.head 질문 있습니다!

0

251

1

퀴즈 답안

0

469

1

최소힙의 결과값과 최대힙->최소힙 결과값이 다른게 맞나요?

0

248

1

1주차 숙제에 대한 해답 코드는 따로 제공되지 않나요??

0

495

1