linuxC/C++数据结构:队列详解

结构定义:

//类型定义 
typedef int dataType;

typedef struct queue
{
	dataType pData;
	struct queue * pNext;
}QUEUE;

操作定义:

#ifndef _QUEUELINKMAIN_H_
#define _QUEUELINKMAIN_H_

//常量定义
#define SIZE 3
enum E_RESULT
{
	ERROR = -1,
	OK
};

//类型定义 
typedef int dataType;

typedef struct queue
{
	dataType pData;
	struct queue * pNext;
}QUEUE;

//全局变量声明 
//函数声明 
/*
函数名: createQueue
函数功能:创建队列
函数参数:无
函数返回值:成功时,返回队列的首地址;失败时,返回NULL。
*/
QUEUE * createQueue ( void );

/*
函数名:enQueue
函数功能:入队列
函数参数:
	QUEUE * pQueue : 指向队列的首地址
	dataType newData : 入队列元素
函数返回值:成功时,返回0;失败时,返回-1.
*/
int enQueue ( QUEUE * pQueue, dataType newData );

/*
函数名:deQueue 
函数功能:出队列
函数参数:
	QUEUE * pQueue : 指向队列的首地址
	dataType * pData : 保存出队列元素
函数返回值:成功时,返回0;失败时,返回-1.
*/
int deQueue ( QUEUE * pQueue, dataType *newData );

/*
函数名:getTopValue
函数功能:获取队列顶元素
函数参数:
	QUEUE * pQueue : 指向队列的首地址
	dataType * pData : 保存出队列元素
函数返回值:成功时,返回0;失败时,返回-1.
*/
int getTopValue ( QUEUE * pQueue, dataType * pData );

/*
函数名: destroyQueue
函数功能:销毁队列
函数参数:QUEUE * pQueue : 指向队列的首地址
函数返回值:无
*/
void destroyQueue ( QUEUE * pQueue );


#endif //_STACK_H_

操作方法实现:

//链式队列
#include 
#include 
#include 
#include "QueueLinkMain.h"


/*
函数名: createQueue
函数功能:创建队列
函数参数:无
函数返回值:成功时,返回队列的首地址;失败时,返回NULL。
*/
QUEUE * createQueue ( void )
{
	QUEUE * pQueue = NULL;
	
	pQueue = ( QUEUE * ) malloc( sizeof(QUEUE) );
	if ( NULL != pQueue )
	{
		memset( pQueue, 0, sizeof (QUEUE) );
		pQueue->pNext = NULL;
	}
	return pQueue;
}

/*
函数名:enQueue
函数功能:入队列
函数参数:
	QUEUE * pQueue : 指向队列的首地址
	dataType newData : 入队列元素
函数返回值:成功时,返回0;失败时,返回-1.
*/
int enQueue ( QUEUE * pQueue, dataType newData ) 
{
	QUEUE *pNew = NULL,*pTail=NULL;
	
	if ( NULL == pQueue)
	{
		return ERROR;
	}
	
	//新建结点并且赋值 pNew
	pNew = ( QUEUE * ) malloc ( sizeof ( QUEUE ) );
	if ( NULL ==  pNew )
	{
		return ERROR;
	}
	memset( pNew, 0, sizeof ( QUEUE ) );
	pNew->pData = newData;	
	pNew->pNext = NULL;
	if(pQueue->pNext == NULL)
	{
		pQueue->pNext = pNew;
	}else
	{
		pTail = pQueue->pNext;
		while(NULL != pTail->pNext)
		{
			pTail = pTail->pNext;			
		}
		pTail->pNext=pNew;
	}	
	
	return OK;	
}

/*
函数名:deQueue 
函数功能:出队列
函数参数:
	QUEUE * pQueue : 指向队列的首地址
	dataType * pData : 保存出队列元素
函数返回值:成功时,返回0;失败时,返回-1.
*/
int deQueue ( QUEUE * pQueue, dataType *newData ) //头删
{
	QUEUE *pNew = NULL,*pFront=NULL;
	if ( NULL == pQueue)
	{
		return ERROR;
	}
	if(NULL != pQueue->pNext)
	{
		pFront = pQueue->pNext;
		*newData = pFront->pData;
		pQueue->pNext = pFront->pNext;
		
		free(pFront);
		pFront=NULL;
	}else
	{
		printf("队列为空\n");
		return ERROR;
	}
	return OK;
}

/*
函数名:getTopValue
函数功能:获取队列顶元素
函数参数:
	QUEUE * pQueue : 指向队列的首地址
	dataType * pData : 保存出队列元素
函数返回值:成功时,返回0;失败时,返回-1.
*/
int getTopValue ( QUEUE * pQueue, dataType * pData )//保存首结点的元素
{	
	QUEUE *pTail=NULL;
	if ( NULL == pQueue)
	{
		return ERROR;
	}
	
	pTail = pQueue->pNext;
	while(NULL != pTail->pNext)
	{
		pTail = pTail->pNext;			
	}
	*pData = pTail->pData;
	
	return OK;
}
/*
函数名: destroyQueue
函数功能:销毁队列
函数参数:QUEUE * pQueue : 指向队列的首地址
函数返回值:无
*/
void destroyQueue ( QUEUE * pQueue )
{
	QUEUE * pDel = NULL;
	
	if ( NULL ==  pQueue )
	{
		return ;
	}		
	
	while ( NULL !=  pQueue->pNext )
	{
		//pDel指向被销毁的结点
		pDel =  pQueue->pNext; 
		//保护pDel后的所有结点
		pQueue->pNext = pDel->pNext;
		//释放pDel
		free ( pDel );
		pDel = NULL;		
	}
	
	free(  pQueue );
	pQueue = NULL;
	return ;
}

测试方法代码:

#include 
#include "QueueLinkMain.h"

int main()
{
	QUEUE *  pQueue = NULL;
	dataType data = 0;
	//创建队列
	pQueue = createQueue ( );
	if ( NULL == pQueue )
	{
		printf("创建栈失败\r\n");
		return -1;
	}
	
	//入队列
	enQueue ( pQueue, 1 );
	enQueue ( pQueue, 2 );
	enQueue ( pQueue, 3 );
	enQueue ( pQueue, 4 );
	//获取队列顶元素
	if ( OK == getTopValue ( pQueue, &data ))
	{
		printf("队列顶元素是:%d\r\n", data);
	}
	//出队列
	if ( OK == deQueue ( pQueue, &data ))
	{
		printf("1出队列元素是:%d\r\n", data);
	}
	if ( OK == deQueue ( pQueue, &data ))
	{
		printf("2出队列元素是:%d\r\n", data);
	}
	if ( OK == deQueue ( pQueue, &data ))
	{
		printf("3出队列元素是:%d\r\n", data);
	}
	if ( OK == deQueue ( pQueue, &data ))
	{
		printf("4出队列元素是:%d\r\n", data);
	}
	if ( OK == deQueue ( pQueue, &data ))
	{
		printf("5出队列元素是:%d\r\n", data);
	}
	//销毁队列
	destroyQueue ( pQueue );
	pQueue = NULL;
	return 0;
}

测试输出:

linuxC/C++数据结构:队列详解_第1张图片

你可能感兴趣的:(数据结构)