class Stack {
constructor() {
this.mainQueue = []
this.subQueue = []
}
push(data) {
while (this.mainQueue.length) {
this.subQueue.push(this.mainQueue.shift())
}
this.mainQueue.push(data)
while (this.subQueue.length) {
this.mainQueue.push(this.subQueue.shift())
}
}
pop() {
return this.mainQueue.shift()
}
top() {
return this.mainQueue[0]
}
isEmpty() {
return !this.mainQueue.length
}
}
function CreateNode(data) {
this.data = data
this.next = null
}
class Queue {
constructor() {
this.head = null
this.rear = null
}
enqueue(data) {
const node = new CreateNode(data)
// queue에 아무 data가 없을때
if (!this.head) {
this.head = node
// queue에 data가 있을때
} else {
this.rear.next = node
}
this.rear = node
}
dequeue() {
if (!this.head) {
throw new Error('queue에 data가 없습니다')
}
const item = this.head.data
this.head = this.head.next
// queue에 data가 없을때
if (!this.head) {
this.rear = null
}
return item
}
peek() {
return this.head.data
}
isEmpty() {
return !this.head
}
}
const queue = new Queue()