队列

数据结构

队列

使用链表实现队列

/*************************************************************************************
 *
 *  file name:  1.c
 *  author   : 	lu.ciana.598393@gmail.com
 *  date     :  2024/04/26
 *  function : 	队
 *  note	 :  none
 *  CopyRight (c)   2024    lu.ciana.598393@gmail.com   All Right Reserved
 *
 ************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef int DataType_t;
//创建空的循环队列,并对管理结构体进行初始化:容量、队尾下标、队首下标、首地址
typedef struct  LinkedQueue
{
	DataType_t 			data;
	struct LinkQueue	*next;
}LQueue_t;


LQueue_t *LinkQueue_Creat(unsigned int size)
{
	LQueue_t *Head = (LQueue_t *)calloc(1,sizeof(LQueue_t));
	if(NULL == Head)
	{
		perror("calloc memory for Head is failed");
		exit(-1); //程序异常终止
	}

	Head ->next = NULL;
	return Head;
}


LQueue_t * LStack_NewNode(DataType_t data)
{
	//1.创建一个新结点并对新结点申请内存
	LQueue_t *New = (LQueue_t *)calloc(1,sizeof(LQueue_t));
	if (NULL == New)
	{
		perror("Calloc memory for NewNode is Failed");
		return NULL;
	}

	//2.对新结点的数据域和指针域(2个)进行初始化
	New->data = data;
	New->next = NULL;

	return New;
}

/**
  * @name:		LinkQueue_IsEmpty
  * @brief  	判断顺序队是否为空
  * @param  
  				@Head
  * @retval 	bool
  * @date 		2024/04/26
  * @note   	补充 注意 说明
  */

bool LinkQueue_IsEmpty(LQueue_t *Head)
{
	return (Head->next == NULL)?true:false;
}
/**
  * @name:		LinkQueue_Enqueue
  * @brief  	入队
  * @param  
  				@Head
				@Data
  * @retval 	bool
  * @date 		2024/04/26
  * @note   	补充 注意 说明
  */
bool LinkQueue_Enqueue(LQueue_t *Head,DataType_t Data)
{
		LQueue_t *PHead = Head;

	LQueue_t *New = LStack_NewNode(Data);	
	//如果申请内存失败
	if (NULL == New)
	{
		perror("Calloc memory for NewNode is Failed");
		return false;
	}
	New->next = Head->next;
    Head->next = New;
	
	return true;
}
/**
  * @name:		LinkQueue_Dequeue
  * @brief  	出队
  * @param  
  				@Head
  * @retval 	bool
  * @date 		2024/04/26
  * @note   	补充 注意 说明
  */
DataType_t LinkQueue_Dequeue(LQueue_t *Head)
{
	if(LinkQueue_IsEmpty(Head))
	{
		printf("LinkQueue is Empty\n");
		return;
	}
	//对双向循环链表的头结点的地址进行备份
	LQueue_t *PHead = Head->next;
	// 	链表内只有一个节点
	if(PHead->next == NULL)
	{
		Head->next = NULL;
		free(PHead);
		return true;
	}
    Head->next = PHead->next;
    PHead->next = NULL;
	free(PHead);
	return true;
}
/**
  * @name:		LinkQueue_Print
  * @brief  	遍历
  * @param  
  				@Head
  * @retval 	void
  * @date 		2024/04/26
  * @note   	补充 注意 说明
  */
void LinkQueue_Print(LQueue_t *Head)
{
	//对单向循环链表的头结点的地址进行备份
	LQueue_t *Phead = Head;
	
	//判断当前链表是否为空,为空则直接退出
	if (Head->next == NULL)
	{
		printf("current linkeflist is empty!\n");
		return false;
	}

	//从首结点开始遍历
	while(Phead->next)
	{
		//把头结点的直接后继作为新的头结点
		Phead = Phead->next;

		//输出头结点的直接后继的数据域
		printf("data = %d\n",Phead->data);

		//判断是否到达尾结点,尾结点的next指针是指向首结点的地址
		if (Phead->next == NULL)
		{
			break;
		}	
	}
	return true;
}
//调用代码调试
int main()
{
	int i;
	LQueue_t * Head = LinkQueue_Creat(32);
		LinkQueue_Enqueue(Head,1);
		LinkQueue_Enqueue(Head,2);
		LinkQueue_Dequeue(Head);
		LinkQueue_Print(Head);

}

使用顺序表实现队列

/*************************************************************************************
 *
 *  file name:  1.c
 *  author   : 	lu.ciana.598393@gmail.com
 *  date     :  2024/04/26
 *  function : 	顺序队
 *  note	 :  none
 *  CopyRight (c)   2024    lu.ciana.598393@gmail.com   All Right Reserved
 *
 ************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef int DataType_t;
//创建空的循环队列,并对管理结构体进行初始化:容量、队尾下标、队首下标、首地址
typedef struct  LinkcularQueue
{
	DataType_t 		*Addr;
	unsigned int 	 Size;
	int 			 Rear;
	int 			 Front;
}LinkQueue_t;

LinkQueue_t *LinkQueue_Creat(unsigned int size)
{
	LinkQueue_t *Head = (LinkQueue_t *)calloc(1,sizeof(LinkQueue_t));
	Head ->Addr = (DataType_t *)calloc(size,sizeof(DataType_t));
	Head ->Size = size;
	Head ->Front = 0;
	Head ->Rear = 0;
	return Head;
}

/**
  * @name:	LinkQueue_IsFull
  * @brief  	判断顺序队是否已满
  * @param  
  			@Head
  * @retval 	bool
  * @date 	2024/04/26
  * @note   	补充 注意 说明
  */
bool LinkQueue_IsFull(LinkQueue_t *Head)
{
	return (Head->Front == (Head->Rear+1)%(Head->Size))?true:false;
}

/**
  * @name:	LinkQueue_IsEmpty
  * @brief  	判断顺序队是否为空
  * @param  
  			@Head
  * @retval 	bool
  * @date 	2024/04/26
  * @note   	补充 注意 说明
  */

bool LinkQueue_IsEmpty(LinkQueue_t *Head)
{
	return (Head->Front == Head->Rear)?true:false;
}
/**
  * @name:	LinkQueue_Enqueue
  * @brief  	入队
  * @param  
  			@Head
				@Data
  * @retval 	bool
  * @date 	2024/04/26
  * @note   	补充 注意 说明
  */
bool LinkQueue_Enqueue(LinkQueue_t *Head,DataType_t Data)
{
	if(LinkQueue_IsFull(Head))
	{
		printf("LinkQueue is Full\n");
		return;
	}
	
	Head->Addr[Head->Rear] = Data;
	Head->Rear = (Head->Rear+1) % Head->Size;
}
/**
  * @name:	LinkQueue_Dequeue
  * @brief  	出队
  * @param  
  			@Head
  * @retval 	bool
  * @date 	2024/04/26
  * @note   	补充 注意 说明
  */
DataType_t LinkQueue_Dequeue(LinkQueue_t *Head)
{
	DataType_t temp;
	if(LinkQueue_IsEmpty(Head))
	{
		printf("LinkQueue is Empty\n");
		return;
	}
	temp = Head->Addr[Head->Front];
	Head->Front = (Head->Front+1) % Head->Size;
	return temp;
}
/**
  * @name:	LinkQueue_Print
  * @brief  	遍历
  * @param  
  			@Head
  * @retval 	void
  * @date 	2024/04/26
  * @note   	补充 注意 说明
  */
void LinkQueue_Print(LinkQueue_t *Head)
{
	for(int i=0;i<Head->Rear;i++)
	{
		printf("第%d个元素是%d\n",i,Head->Addr[i]);
	}
}

int main()
{
	int i;
	LinkQueue_t * Head = LinkQueue_Creat(32);
		LinkQueue_Enqueue(Head,1);
		LinkQueue_Enqueue(Head,2);
		LinkQueue_Print(Head);
		// LinkQueue_Dequeue(Head);
		//printf("%d",LinkQueue_IsEmpty(Head));
}
posted @ 2024-04-26 22:02  luxiaolim  阅读(10)  评论(0编辑  收藏  举报