4-1 Deque (25分)
A "deque" is a data structure consisting of a list of items, on which the following operations are possible:
- Push(X,D): Insert item X on the front end of deque D.
- Pop(D): Remove the front item from deque D and return it.
- Inject(X,D): Insert item X on the rear end of deque D.
- Eject(D): Remove the rear item from deque D and return it.Write routines to support the deque that take O(1) time per operation.
Format of functions:
Deque CreateDeque();
int Push( ElementType X, Deque D );
ElementType Pop( Deque D );
int Inject( ElementType X, Deque D );
ElementType Eject( Deque D );
where Deque
is defined as the following:
typedef struct Node *PtrToNode;
struct Node {
ElementType Element;
PtrToNode Next, Last;