【数据结构-队列】C语言实现链队基本操作

C语言实现链队基本操作

  • 基本操作
    • 定义链式队列存储结构
    • 初始化
    • 求长度
    • 入队
    • 出队
    • 取头元素
    • 遍历
    • 整合测试代码

基本操作

定义链式队列存储结构

//队列的链式存储结构 
typedef struct QNode 
{
	SElemType data;
	struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{
	QueuePtr front;   //对头指针 
	QueuePtr rear;    //队尾指针      这里需要重点理解——指向QNode类型指针 
}LinkQueue; 

初始化

//初始化 
Status InitQueue(LinkQueue *Q)
{
	(*Q).front = (*Q).rear = (QNode *)malloc(sizeof(QNode));
	if(!(*Q).front) return ERROR;    //动态开辟空间失败 返回ERROR 
	((*Q).front)->next = NULL;   //注意这里的front、rear和next三个成员在哪里 
	return OK;
}

求长度

//求长度 ,思路:逐个结点遍历计数 
int QueueLength(LinkQueue Q)
{
	int len;
	QNode *tmp_node = Q.front->next;   //指向首元结点
	while(tmp_node->next !=NULL)
	{
		len++;
		tmp_node = tmp_node->next;
	}
	return len;
}

入队

//入队(尾插)
Status EnQueue(LinkQueue *Q,SElemType e)
{
	QNode *tmp_node = (QNode *)malloc(sizeof(QNode));    //需入队新结点 
	tmp_node->data = e;    //输入数据 
	tmp_node->next = NULL;     //由于是插尾,所以next置空 
	(*Q).rear->next = tmp_node;   //与前一个结点连接 
	(*Q).rear = tmp_node;       //队尾指针指向新结点 
}

出队

//出队(头取) 
Status DeQueue(LinkQueue *Q,SElemType *e)
{
	if((*Q).front->next == NULL) return ERROR;    //空队列 
	QNode *tmp_node  =(*Q).front;        //tmp_node指向对头 
	*e = Q->front->next->data;           //取首元结点数据值 
	(*Q).front = (*Q).front->next;    //对头指针指向下一个结点 
	free(tmp_node);       //释放已出队结点内存空间 
	return OK;
}

取头元素

//取头元素
SElemType GetElem(LinkQueue Q)
{
//	if(Q.front->next == NULL) return ERROR;   //空队列
//	return (Q.front->data);      //这里有个小bug,ERROR和返回的data=0是无法判断的   所以改为下面的方式
	if(Q.front != Q.rear) return (Q.front->next->data); 
}

遍历

//遍历
Status show(LinkQueue Q)
{
	if(Q.front == Q.rear) return ERROR;
	QNode *tmp_node = Q.front->next;
	while(tmp_node!=NULL)
	{
		printf("%d ",tmp_node->data);
		tmp_node = tmp_node->next;
	}
	return OK;
}

整合测试代码

#include 
#include 

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

typedef int Status;
typedef int SElemType;

//队列的链使存储结构 
typedef struct QNode 
{
	SElemType data;
	struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{
	QueuePtr front;   //对头指针 
	QueuePtr rear;    //队尾指针      这里需要重点理解——指向QNode类型指针 
}LinkQueue; 

//初始化 
Status InitQueue(LinkQueue *Q)
{
	(*Q).front = (*Q).rear = (QNode *)malloc(sizeof(QNode));
	if(!(*Q).front) return ERROR;    //动态开辟空间失败 返回ERROR 
	((*Q).front)->next = NULL;   //注意这里的front、rear和next三个成员在哪里 
	return OK;
}

//求长度 ,思路:逐个结点遍历计数 
int QueueLength(LinkQueue Q)
{
	int len;
	QNode *tmp_node = Q.front->next;   //指向首元结点
	while(tmp_node->next !=NULL)
	{
		len++;
		tmp_node = tmp_node->next;
	}
	return len;
}

//入队(尾插)
Status EnQueue(LinkQueue *Q,SElemType e)
{
	QNode *tmp_node = (QNode *)malloc(sizeof(QNode));    //需入队新结点 
	tmp_node->data = e;    //输入数据 
	tmp_node->next = NULL;     //由于是插尾,所以next置空 
	(*Q).rear->next = tmp_node;   //与前一个结点连接 
	(*Q).rear = tmp_node;       //队尾指针指向新结点 
}

//出队(头取) 
Status DeQueue(LinkQueue *Q,SElemType *e)
{
	if((*Q).front->next == NULL) return ERROR;    //空队列 
	QNode *tmp_node  =(*Q).front;        //tmp_node指向对头 
	*e = Q->front->next->data;           //取首元结点数据值 
	(*Q).front = (*Q).front->next;    //对头指针指向下一个结点 
	free(tmp_node);       //释放已出队结点内存空间 
	return OK;
}

//取头元素
SElemType GetElem(LinkQueue Q)
{
//	if(Q.front->next == NULL) return ERROR;   //空队列
//	return (Q.front->data);      //这里有个小bug,ERROR和返回的data=0是无法判断的   所以改为下面的方式
	if(Q.front != Q.rear) return (Q.front->next->data); 
}

//遍历
Status show(LinkQueue Q)
{
	if(Q.front == Q.rear) return ERROR;
	QNode *tmp_node = Q.front->next;
	while(tmp_node!=NULL)
	{
		printf("%d ",tmp_node->data);
		tmp_node = tmp_node->next;
	}
	return OK;
}

int main()
{
	SElemType e;
	int i;
	LinkQueue Q;
	printf("------------初始化队列-------------\n");
	InitQueue(&Q);
	printf("队列初始化完成!\n");
	printf("\n------------入队-----------------\n");
	for(i=0;i<5;i++)
	{
		EnQueue(&Q,i);
		printf("%d ");
	}
	printf("依次入队成功!\n");
	printf("\n----------获取头元素-------------\n");
	printf("%d\n",GetElem(Q));
	printf("\n----------遍历队列---------------\n");
	if(!show(Q)) printf("空队列\n");
	printf("\n\n------------出队-----------------\n");
	DeQueue(&Q,&e);
	printf("%d 出队成功~\n",e);
	printf("\n----------遍历队列---------------\n");
	if(!show(Q)) printf("空队列\n");
}

你可能感兴趣的:(数据结构,C语言,数据结构,队列,链表)