6-1-Deque-函数题

解题代码

Deque CreateDeque() {
	PtrToNode node = (PtrToNode)malloc(sizeof(struct Node));
	node->Next = NULL;
	node->Last = NULL;
	Deque A = (Deque)malloc(sizeof(struct DequeRecord));
	A->Front = node;
	A->Rear = node;
	return A;
}
int Push(ElementType X, Deque D) {
	PtrToNode node = (PtrToNode)malloc(sizeof(struct Node));
	if (node)
	{
		node->Element = X;
		if (D->Front == D->Rear) {
			node->Last = D->Front;
			node->Next = NULL;
			D->Front->Next = node;
			D->Rear = node;
		}
		else {
			node->Last = D->Front;
			node->Next = D->Front->Next;
			D->Front->Next = node;
			node->Next->Last = node;
		}
		return 1;
	}
	else return 0;
}
ElementType Pop(Deque D) {
	PtrToNode T = D->Front->Next;
	if (!T) return ERROR;
	else if(D->Front->Next==D->Rear){
		D->Front->Next = NULL;
		D->Rear = D->Front;
		ElementType temp = T->Element;
		free(T);
		return temp;
	}
	else {
		D->Front->Next = T->Next;
		T->Next->Last = D->Front;
		ElementType temp = T->Element;
		free(T);
		return temp;
	}
}
int Inject(ElementType X, Deque D) {
	PtrToNode node = (PtrToNode)malloc(sizeof(struct Node));
	if (node) {
		node->Element = X;
		node->Next = NULL;
		node->Last = D->Rear;
		D->Rear->Next = node;
		D->Rear = node;
		return 1;
	}
	else return 0;
}
ElementType Eject(Deque D) {
	PtrToNode T = D->Rear;
	if (D->Front == D->Rear) return ERROR;
	else {
		ElementType temp = T->Element;
		if (D->Front->Next == D->Rear) {
			D->Front->Next = NULL;
			D->Rear = D->Front;
		}
		else {
			D->Rear = D->Rear->Last;
			D->Rear->Next = NULL;
		}
		free(T);
		return temp;
	}
}

测试结果

在这里插入图片描述

问题整理

1.别忘记了return。
2.及时更新rear。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值