链队列基本操作

 

最近因为过年间隔了几天,今天继续学习

 

队列基本操作比较简单,分析就省略了。

唯一一点犯错的就是

bool DestoryQueue(PQueue &Q) /*DestoryQueue*/
{
if (IsEmpty(Q))
{
cout << "The Queue is empty!" << endl;
return false;
}
PQueueNode P = NULL;


while (!IsEmpty(Q))
{
if (Q->front->next== Q->rear)//这个地方之前错写过P==Q->rear 
Q->rear = Q->front;
P = Q->front->next;
Q->front->next = P->next;
delete P;
}

return true;
}

 

while里的if判断条件写成了P==Q->rear

内部循环最后有了释放 导致了错误。

可能是昨天喝了点酒的缘故。。。总之这队列这些代码喝了酒的确印象不太深刻 ,反正就是写了下来

 

 

typedef struct QueueNode
{
	int data;
	struct QueueNode *next;
}QueueNode,*PQueueNode;

typedef struct QueueL
{
	PQueueNode front;//Queue head 头部
	PQueueNode rear;//Queue rear	尾部
}Queue,*PQueue;

void InitQueue(PQueue &Q)				/*InitQueue*/
{
	PQueueNode NewNode = new QueueNode;
	NewNode->data = 0;
	NewNode->next = NULL;
	Q->front = NewNode;
	Q->rear = NewNode;
}


bool IsEmpty(const PQueue &Q)			/*VerifyQueue*/
{
	if (Q->front == Q->rear)
		return true;
	return false;
}


bool EnterQueue(PQueue &Q,int x)		/*EnterQueue*/
{
	/*Is Full?  NO verify */
	PQueueNode NewNode = new QueueNode;
	if (!NewNode) return false;   /*溢出*/
	NewNode->data = x;
	NewNode->next = NULL;
	Q->rear->next = NewNode;
	Q->rear = NewNode;
	return true;

}


bool DeleteQueue(PQueue &Q, int *x)			/*DeleteQueue*/
{
	if (IsEmpty(Q)) 
	{
		cout << "The Queue is empty!" << endl;
		return false;
	}
	PQueueNode P = Q->front->next;
	*x = P->data;

	if (P == Q->rear)				/* only one element*/
	{
	Q->front->next = NULL;
	Q->rear = Q->front;
	}
	else
	{
		Q->front->next = P->next;
	}
		
	delete P;
	return true;
}

bool DestoryQueue(PQueue &Q)		/*DestoryQueue*/
{
	if (IsEmpty(Q))
	{
		cout << "The Queue is empty!" << endl;
		return false;
	}
	PQueueNode P = NULL;

	while (!IsEmpty(Q))
	{
		if (Q->front->next== Q->rear)//这个地方之前错写过P==Q->rear 
			Q->rear = Q->front;
		P = Q->front->next;
		Q->front->next = P->next;
		delete P;
	}
	
	return true;
}

bool PrintQ(const PQueue &Q)
{
	if (IsEmpty(Q))
	{
		cout << "Queue is Empty!" << endl;
		return false;
	}
	PQueueNode P = Q->front->next;
	while (P)
	{
		cout << P->data << endl;
		P = P->next;
	}
	return true;
}

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值