Deque CreateDeque()
{
Deque p;
p=(Deque)malloc(sizeof (struct DequeRecord));
p->Front=(PtrToNode)malloc(sizeof (struct Node));
p->Rear=(PtrToNode)malloc(sizeof (struct Node));
p->Front->Last=NULL;
p->Rear=p->Front;
p->Rear->Next=NULL;
return p;
}
int Push(ElementType X,Deque D)
{
struct Node*tmp;
tmp=(struct Node*)malloc(sizeof(struct Node));
if(!tmp)return 0;
tmp->Element=X;
if(D->Front==D->Rear)
{
D->Front->Next=tmp;
tmp->Last=D->Front;
D->Rear=tmp;
D->Rear->Next=NULL;
return 1;
}
tmp->Next=D->Front->Next;
tmp->Last=D->Front;
D->Front->Next->Last=tmp;
D->Front->Next=tmp;
return 1;
}
ElementType Pop(Deque D)
{
if(D->Front==D->Rear)
return ERROR;
int num=D->Front->Next->Element;
struct Node *tmp=D->Front->Next;
if(D->Front->Next==D->Rear)
{
D->Rear=D->Front;
D->Rear->Next=NULL;
free(tmp);
return num;
}
D->Front->Next->Next->Last=D->Front;
D->Front->Next=D->Front->Next->Next;
free(tmp);
return num;
}
int Inject(ElementType X,Deque D)
{
struct Node* tmp;
tmp=(struct Node*)malloc(sizeof(struct Node));
if(!tmp)return 0;
tmp->Element=X;
if(D->Front==D->Rear)
{
D->Front->Next=tmp;
tmp->Last=D->Front;
D->Rear=tmp;
return 1;
}
D->Rear->Next=tmp;
tmp->Last=D->Rear;
tmp->Next=NULL;
D->Rear=tmp;
return 1;
}
ElementType Eject(Deque D)
{
if(D->Front==D->Rear)
return ERROR;
int num=D->Rear->Element;
struct Node*tmp=D->Rear;
D->Rear=D->Rear->Last;
D->Rear->Next=NULL;
free(tmp);
return num;
}
3-1 Deque
最新推荐文章于 2024-09-30 13:21:56 发布