bool Push(ElementType X, Deque D)
{if ((D->Rear + 1) % D->MaxSize == D->Front)return false;if (D->Front == D->Rear){D->Data[D->Front] = X;D->Rear = (D->Rear + 1) % D->MaxSize;return true;}D->Front = (D->Front - 1+ D->MaxSize) % D->MaxSize;D->Data[D->Front] = X;return true;
}ElementType Pop(Deque D)
{ElementType x;if (D->Front == D->Rear)return ERROR;x = D->Data[D->Front];D->Front = (D->Front +1) % D->MaxSize;return x;
}bool Inject(ElementType X, Deque D)
{if ((D->Rear + 1) % D->MaxSize == D->Front)return false;D->Data[D->Rear] = X;D->Rear = (D->Rear + 1) % D->MaxSize;return true;
}ElementType Eject(Deque D)
{ElementType x;if (D->Front == D->Rear)return ERROR;D->Rear = (D->Rear - 1+ D->MaxSize) % D->MaxSize;x = D->Data[D->Rear];return x;
}
细心的你会发现,代码中有一段其实可以去掉,效果是一样的,读者自行思考。
if (D->Front == D->Rear){D->Data[D->Front] = X;D->Rear = (D->Rear + 1) % D->MaxSize;return true;}