1. 程式人生 > >C語言_佇列的基本操作

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;
}