# Stack & Queue

** Updated:**

## 1. Stack Pseudocode

```
algorithm IsEmpty(S):
if S.top == 0 then
return True
else
return False
algorithm Pop(S):
if IsEmpty(S) then
throw StackUnderflow
else
S.top = S.top - 1
return S[S.top+1]
algorithm Push(S, x):
if S.top == S.size then
throw StackOverflow
else
S.top += 1
S[S.top] = x
```

## 2. Queue

```
algorithm Enqueue(Q, x):
if (Q.tail == Q.length and Q.head == 1) or
(Q.head == Q.tail + 1) then
throw QueueFullException
Q[Q.tail] = x
if Q.tail == Q.length --> wrap around
Q.tail = 1
else
Q.tail += 1
algorithm Dequeue(Q):
if Q.head == Q.tail then
throw QueueEmptyException
x = Q[Q.head]
if Q.head == Q.length
Q.head = 1
else
Q.head += 1
return x
```

## Leave a comment