Deque
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.
1.CreateDeque()
Deque CreateDeque()
{
Deque d;
d=(Deque)malloc(sizeof(struct DequeRecord));
d->Front=(PtrToNode)malloc(sizeof(struct Node));
d->Front->Last=NULL;
d->Rear=d->Front;
d->Rear->Next=NULL;
return d;
}
2.Push( ElementType X, Deque D )
int Push( ElementType X, Deque D )
{
struct Node* temporary;
temporary=(struct Node*)malloc(sizeof(struct Node));
if(!temporary)
return 0;
temporary->Element=X;
if(D->Front==D->Rear){
D->Front->Next=temporary;
temporary->Last=D->Front;
D->Rear=temporary;
temporary->Next=NULL;
return 1;
}
temporary->Next=D->Front->Next;
D->Front->Next->Last=temporary;
D->Front->Next=temporary;
temporary->Last=D->Front;
return 1;
}
3.ElementType Pop( Deque D )
ElementType Pop( Deque D )
{
if(D->Front==D->Rear)
return ERROR;
int temporary=D->Front->Next->Element;
struct Node* t=D->Front->Next;
if(D->Front->Next==D->Rear){
D->Rear=D->Front;
D->Rear->Next=NULL;
free(t);
return temporary;
}
D->Front->Next->Next->Last=D->Front;
D->Front->Next=D->Front->Next->Next;
free(t);
return temporary;
}
int Inject( ElementType X, Deque D )
{
struct Node* temporary;
temporary=(struct Node*)malloc(sizeof(struct Node));
if(!temporary)
return 0;
temporary->Element=X;
if(D->Front==D->Rear){
D->Front->Next=temporary;
temporary->Last=D->Front;
D->Rear=temporary;
return 1;
}
D->Rear->Next=temporary;
temporary->Last=D->Rear;
temporary->Next=NULL;
D->Rear=temporary;
return 1;
}
4.ElementType Eject( Deque D )
ElementType Eject( Deque D )
{
if(D->Front==D->Rear)
return ERROR;
int temporary=D->Rear->Element;
struct Node* t=D->Rear;
D->Rear=D->Rear->Last;
D->Rear->Next=NULL;
free(t);
return temporary;
}
这是pta平台的一道函数题,要求补全函数,pta,
顺便问下,谁tm买小米??