Algorithm 2 Double Ended Queue

Require: QQ is an array of length Q.lengthQ.length indexed from 11 with two attributes Q.headQ.head and Q.tailQ.tail initialized to be 11

Ensure: Implementation of deque operations that handle overflow and underflow

1: // Inserts elements to the front of deque.

2:procedure HeadEnqueue(Q,xQ, x)

3:if QueueFull(QQ) then

4:error "overflow"

5:else

6:if Q.head==1Q.head == 1 then

7:Q.head=Q.lengthQ.head = Q.length

8:else

9:Q.head=Q.head1Q.head = Q.head - 1

10:Q[Q.head]=xQ[Q.head] = x

11:end if

12:end if

13:end procedure

14:

15: // Inserts elements to the back of deque.

16:procedure TailEnqueue(Q,xQ, x)

17:if QueueFull(QQ) then

18:error "overflow"

19:else

20:Q[Q.tail]=xQ[Q.tail] = x

21:if Q.tail==Q.lengthQ.tail == Q.length then

22:Q.tail=1Q.tail = 1

23:else

24:Q.tail=Q.tail+1Q.tail = Q.tail + 1

25:end if

26:end if

27:end procedure

28:

29: // Deletes elements from the front of deque.

30:procedure HeadDequeue(QQ)

31:if QueueEmpty(QQ) then

32:error "underflow"

33:else

34:x=Q[Q.head]x = Q[Q.head]

35:if Q.head==Q.lengthQ.head == Q.length then

36:Q.head=1Q.head = 1

37:else

38:Q.head=Q.head+1Q.head = Q.head + 1

39:end if

40:return xx

41:end if

42:end procedure

43:

44: // Deletes elements from the back of deque.

45:procedure TailDequeue(QQ)

46:if QueueEmpty(QQ) then

47:error "underflow"

48:else

49:if Q.tail==1Q.tail == 1 then

50:Q.tail=Q.lengthQ.tail = Q.length

51:else

52:Q.tail=Q.tail1Q.tail = Q.tail - 1

53:end if

54:return Q[Q.tail]Q[Q.tail]

55:end if

56:end procedure