C语言_队列的基本操作

本片博客主要内容:

创建新结点
初始化队列
入队列
出队列
返回对头元素
返回队尾元素
计算队列长度
判断队列是否为空,为空返回1,否则返回零

###1、初始化队列

void QueueInit (Queue* q)	//初始化队列
{
	QNode *cur = (QNode *)malloc (sizeof (QNode));	
	if (NULL == cur)
	{
		perror ("InitQueue::malloc >>");
		return ;
	}
	q->front = cur;
	q->rear = cur;
}

###2、创建新结点

QNode* QBuyNode ()
{
	QNode* newnode = (QNode *) malloc (sizeof (QNode));
	if (newnode == NULL)
	{
		perror ("QBuyNode :: malloc >>");
		return NULL;
	}
	newnode->data = 0;
	newnode->pNext = NULL;
	return newnode;
}

###3、入队列

void QueuePush (Queue* q, QDataType data)	//入队列
{
	QNode* cur = NULL;
	assert (q != NULL);

	cur = QBuyNode ();

	q->rear->data = data;
	q->rear ->pNext = cur;
	cur->pNext = NULL;
	q->rear = cur;
	printf ("入队操作成功!\n");
}

###4、出队列

void QueuePop (Queue *q)//出队列
{
	QNode *cur = NULL;
	QDataType ret;
	if (q->front == q->rear )
	{
		printf ("队列为空,操作失败");
		return ;
	}

	cur = q->front ->pNext;
	free (q->front);
	q->front = NULL;
	q->front = cur;
	printf ("出对操作成功!\n");

}

###5、求队列长的

int QueueSize (Queue *q)
{
	int size = 0;
	QNode *cur = q->front;
	while (cur != q->rear)
	{
		size++;
		cur = cur->pNext;
	}
	return size;
}

###8、判断队列是否为空

int IsQueueEmpty (Queue *q)
{
	return q->front == q->rear;
}

###9、返回对头元素

QDataType QueueFront (Queue *q)//返回对头元素
{
	if (q->front == q->rear)
	{
		printf ("队列为空,操作失败!!\n");
		return ERROR;
	}

	return q->front->data;
}

###10、返回队尾元素

QDataType QueueRear (Queue *q)//返回队尾元素
{
	QNode *cur = NULL;
	if (q->front == q->rear)
	{
		printf ("队列为空,操作失败!!\n");
		return ERROR;
	}
	cur = q->front->pNext;
	while (cur->pNext != q->rear)
	{
		cur = cur->pNext;
	}
	return cur->data;
}

测试结果:

这里写图片描述

头文件:

#ifndef __sqeue_h__
#define __sqeue_h__

#include <stdlib.h>
#include <assert.h>
#include <stdio.h>

#define OK 1
#define ERROR 0

typedef int QDataType;

typedef struct QNode
{
	QDataType data;
	struct QNode* pNext;
}QNode;

typedef struct 
{
	QNode *front;	//队尾指针
	QNode *rear;	//对头指针
}Queue;

QNode* QBuyNode ();	//创建新结点
void QueueInit (Queue* q);	//初始化队列
void QueuePush (Queue* q, QDataType data);	//入队列
void QueuePop (Queue *q);	//出队列
QDataType QueueFront (Queue *q);//返回对头元素
QDataType QueueRear (Queue *q);//返回队尾元素
int QueueSize (Queue *q);	//计算队列长度
int IsQueueEmpty (Queue *q);	//判断队列是否为空,为空返回1,否则返回零

#endif

测试代码:

#include "queue.h"

void test_1()
{
	Queue q ;
	QDataType ret = 0;
	int IsEmpty;
	QueueInit (&q);
	QueuePush (&q, 1);
	QueuePush (&q, 4);
	QueuePush (&q, 3);
	QueuePush (&q, 2);

	printf ("对头元素为:%d\n", QueueFront (&q));//返回对头元素
	printf ("队尾元素为:%d\n", QueueRear (&q));

	QueuePop (&q);
	QueuePop (&q);
	IsEmpty = IsQueueEmpty(&q);
	if (IsEmpty == 0)
	{
		printf ("队列不为空\n");
	}
	else
	{
		printf ("队列为空\n");
	}
	printf ("队列的长度为:%d\n", QueueSize (&q));
	QueuePop (&q);
	QueuePop (&q);
	QueuePop (&q);
	IsEmpty = IsQueueEmpty(&q);
	if (IsEmpty == 0)
	{
		printf ("队列不为空\n");
	}
	else
	{
		printf ("队列为空\n");
	}
	printf ("队列的长度为:%d\n", QueueSize (&q));
}


int main()
{
	
	test_1();

	system ("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/A__B__C__/article/details/82702003
今日推荐