js401-reading

Stacks and Queues

common terms

stack concepts

Push O(1)

Algorithm push(value)
node = new Node(value)
node.next = top
top = node

Pop o(1)

algorithm pop()
Node temp = top
top = top.next
temp.next = null
return temp.value

Peek o(1)

Algorithm peek()
return top.value

IsEmpty o(1)

Algorithm isEmpty()
return top === null

Queue

Enqueue o(1)

Algorithm enqueue(value)
node = newNode(value)
rear.next = node
rear = node

Dequeue o(1)

Algorithm dequeue()
Node temp = front
front = front.next
temp.next = null
return temp.value

Peek o(1)

algorithm peek()
return front.value

isEmpty o(1)

Algorithm isEmpty()
return front = null