队列链式存储实现

#ifndef _LINK_QUEUE_LIST_H_
#define _LINK_QUEUE_LIST_H_

/************************************************************************/
/*                    队列链式存储实现LinkQueueNode                  */
/************************************************************************/

typedef struct LinkQueueNode
{
	ElemType data;
	LinkQueueNode* Next;
}LinkQueueNode;

typedef struct LinkQueueList
{
	LinkQueueNode* headPointer;
	LinkQueueNode* tailPointer;
}LinkQueueList;

/**初始化操作,建立一个空的队列Q。*/
Status InitQueue(LinkQueueList* Q);


/**若队列存在,则销毁它*/
Status DestoryQueue(LinkQueueList* Q);


/**将队列清空*/
Status ClearQueue(LinkQueueList* Q);


/**若队列为空返回true,否则返回false*/
bool QueueEmpty(LinkQueueList Q);


/**若队列不为空,返回队头元素*/
Status GetHead(LinkQueueList Q,ElemType *e);


/**如果队列存在,插入新元素e到队列Q中,并成为队尾元素*/
Status EnQueue(LinkQueueList* Q,ElemType e);


/**删除队列Q中队头元素,并返回这个位置的元素值e*/
Status DeQueue(LinkQueueList* Q,ElemType* e);


/**返回队列Q的元素个数*/
int QueueLength(LinkQueueList Q);


/**遍历队列元素*/
Status TraverseQueue(LinkQueueList Q);

#endif//_LINK_QUEUE_LIST_H_


#include "stdafx.h"
#include "LinkQueueList.h"

/**初始化操作,建立一个空的队列Q。*/
Status InitQueue(LinkQueueList* Q)
{
	Q->headPointer = (LinkQueueNode*)malloc(sizeof(LinkQueueNode));
	Q->headPointer->Next = NULL;
	Q->tailPointer = Q->headPointer;

	return OK;
}


/**若队列存在,则销毁它*/
Status DestoryQueue(LinkQueueList* Q)
{
	ClearQueue(Q);
	free(Q);
	Q = NULL;

	return OK;
}


/**将队列清空*/
Status ClearQueue(LinkQueueList* Q)
{
	if(QueueEmpty(*Q))
		return OK;

	LinkQueueNode* nextNode = Q->headPointer->Next;
	while(nextNode != Q->tailPointer)
	{
		LinkQueueNode* deleteNode = nextNode;
		nextNode  = deleteNode->Next;
		free(deleteNode);
		Q->headPointer->Next = nextNode;
	}

	return OK;
}


/**若队列为空返回true,否则返回false*/
bool QueueEmpty(LinkQueueList Q)
{
	return Q.headPointer->Next == NULL;
}


/**若队列不为空,返回队头元素*/
Status GetHead(LinkQueueList Q,ElemType *e)
{
	if(QueueEmpty(Q))
		return ERROR;

	*e = Q.headPointer->Next->data;

	return OK;
}


/**如果队列存在,插入新元素e到队列Q中,并成为队尾元素*/
Status EnQueue(LinkQueueList* Q,ElemType e)
{
	LinkQueueNode* newNode = (LinkQueueNode*)malloc(sizeof(LinkQueueNode));
	newNode->data = e;
	newNode->Next = Q->tailPointer->Next;
	Q->tailPointer->Next = newNode;
	Q->tailPointer = newNode;

	return OK;
}


/**删除队列Q中队头元素,并返回这个位置的元素值e*/
Status DeQueue(LinkQueueList* Q,ElemType* e)
{
	if(QueueEmpty(*Q))
		return ERROR;
	*e = Q->headPointer->Next->data;
	
	LinkQueueNode* deleteNode = Q->headPointer->Next;
	Q->headPointer->Next = deleteNode->Next;
	free(deleteNode);

	return OK;
}


/**返回队列Q的元素个数*/
int QueueLength(LinkQueueList Q)
{
	int count = 0;
	if(QueueEmpty(Q))
		return count;
	
	LinkQueueNode* nextNode = Q.headPointer;
	while(nextNode != Q.tailPointer)
	{
		count++;
		nextNode  = nextNode->Next;
	}

	return count;
}


/**遍历队列元素*/
Status TraverseQueue(LinkQueueList Q)
{
	if(QueueEmpty(Q))
		return ERROR;

	int index = 0;
	LinkQueueNode* nextNode = Q.headPointer->Next;
	while(nextNode != NULL)
	{
		std::cout<<"第"<<index++<<"个元素值为:"<<nextNode->data<<std::endl;
		nextNode  = nextNode->Next;
	}

	return OK;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值