udemy 에서 Master the Coding interview: Data Structures & Algorithms 강의를 수강한 내용을 정리하는 포스팅입니다.
Stack
https://en.wikipedia.org/wiki/Stack_(abstract_data_type)
Stack (abstract data type) - Wikipedia
From Wikipedia, the free encyclopedia Jump to navigation Jump to search Abstract data type Similar to a stack of plates, adding or removing is only possible at the top. Simple representation of a stack runtime with push and pop operations. In computer scie
en.wikipedia.org
Queue
https://en.wikipedia.org/wiki/Queue_(abstract_data_type)
Queue (abstract data type) - Wikipedia
Abstract data type QueueAlgorithm Average Worst caseSpace O(n) O(n)Search O(n) O(n)Insert O(1) O(1)Delete O(1) O(1) Representation of a FIFO (first in, first out) queue In computer science, a queue is a collection of entities that are maintained in a seque
en.wikipedia.org
-스택은 선입후출(FILO) 의 형태를 가진 데이터 구조다.
-큐는 선입선출(FIFO)의 형태를 가진 데이터 구조다.
- 스택과 큐는 각각 해당 자료구조의 최종점 또는 시작점에서 데이터를 빠르게 가져오기위해 사용하며 조회 를 제외한 삽입, 삭제의 시간복잡도가 O(1)이다.
스택과 큐를 자바스크립트 코드로 구현 하면 아래와 같이 구현할 수 있다.
(큐의 경우 연결리스트를 활용)
//STACK class Stack { constructor(){ this.array = []; } peek() { return this.array[this.array.length-1]; } push(value){ this.array.push(value); return this; } pop(){ this.array.pop(); return this; } } const myStack = new Stack(); myStack.peek(); myStack.push('google'); myStack.push('udemy'); myStack.push('discord'); myStack.peek(); myStack.pop(); myStack.pop(); myStack.pop(); //QUEUE class Node { constructor(value) { this.value = value; this.next = null; } } class Queue { constructor(){ this.first = null; this.last = null; this.length = 0; } peek() { return this.first; } enqueue(value){ const newNode = new Node(value); if (this.length === 0) { this.first = newNode; this.last = newNode; } else { this.last.next = newNode; this.last = newNode; } this.length++; return this; } dequeue(){ if (!this.first) { return null; } if (this.first === this.last) { this.last = null; } const holdingPointer = this.first; this.first = this.first.next; this.length--; return this; } } const myQueue = new Queue(); myQueue.peek(); myQueue.enqueue('Joy'); myQueue.enqueue('Matt'); myQueue.enqueue('Pavel'); myQueue.peek(); myQueue.dequeue(); myQueue.dequeue(); myQueue.dequeue(); myQueue.peek();
'TIL > ZTM Data Structures and Algorithms' 카테고리의 다른 글
ZTM Data Structures & Algorithms - Graph (0) | 2021.08.28 |
---|---|
ZTM Data Structures & Algorithms - Tree (0) | 2021.08.28 |
ZTM Data Structures & Algorithms - Linked List (0) | 2021.08.22 |
ZTM Data Structures & Algorithms - Hash Tables (0) | 2021.07.26 |
ZTM Data Structures & Algorithms - Array (0) | 2021.07.25 |
댓글