数据结构(八)队列(链式)实现的功能

头文件

#ifndef  LINKQUEUE_H
#define LINKQUEUE_H

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

#define SUCCESS  10000
#define FAILURE 10001
#define TRUE  10002
#define FALSE 10003

struct QueueNode     //表示队列结点的信息
{
	int data;
	struct Queue *next;
};
typedef struct QueueNode node;

struct QueueInfo      //表示队列的信息
{
	node *front;
	node *rear;
};
typedef struct QueueInfo Queue;

int InitQueue(Queue *q);
int push(Queue *q, int e);
int EmptyQueue(Queue q);
int GetTop(Queue q);
int pop(Queue *q);
int ClearQueue(Queue *q);
int DestroyQueue(Queue *q);

#endif

调用子函数,函数文件

#include"LinkQueue.h"
/*
函数描述:链式队列初始化
函数参数:队列的地址
函数返回值:成功返回SUCCESS,失败返回FAILURE;
*/
int InitQueue(Queue *q)
{
	if(NULL == q)
	return FAILURE;

	q -> front = (node*)malloc(sizeof(node)*1);
	if(NULL == q -> front)
	{
		return FAILURE;
	}

	q -> rear = q -> front;
	q -> front -> next = NULL;

	return SUCCESS;
}
/*
函数描述:输入的元素进队(从队尾进,相当于尾插法)
*/
int push(Queue *q, int e)
{
	if(NULL == q)
	return FAILURE;
	if(q -> front == NULL)
	return FAILURE;

	node *n = q ->rear;
	q -> rear = (node*)malloc(sizeof(node)*1);
	n -> next = q -> rear;
	q -> rear -> data = e;
	q -> rear -> next = NULL;
	return SUCCESS;
}
/*
函数描述:判断队列是否为空
*/
int EmptyQueue(Queue q)
{
	return (q.front == q.rear) ? TRUE :FALSE;
}
/*
函数描述:获取队头元素
*/
int GetTop(Queue q)
{
	return (q.rear == q.front) ? FAILURE : q.front -> next ->data;
}
/*
函数描述:让队列里的元素出队(从队头出)
*/
int pop(Queue *q)
{
	if(NULL ==q)
	return FAILURE;
	if(q -> front == q -> next)  //空对
	{
		return FAILURE;
	}
	node *n = q -> front -> next;
	if(!(n -> next))  //只有一个结点
	{
		q -> rear = q -> front;
	}
	q -> front -> next = n -> next;
	int e = n -> data;
	free(n);
	return e;
}
/*
函数描述:清空队列
*/
int ClearQueue(Queue *q)
{
	if(NULL == q)
	return FAILURE;
	node *n = q -> front -> next;
	while(n)
	{
		q -> front -> next = n -> next;
		free(n);
		n = q -> front -> next;
	}
	q ->  rear = q -> front;
	return SUCCESS;
}
/*
函数描述:清除队列
*/
int DestoryQueue(Queue *q)
{
	if(NULL == q)
	return FAILURE;
	
	free(q -> front);
	q -> front = q -> rear = NULL;
	return SUCCESS;
}

主函数执行文件

#include"LinkQueue.h"
#include<time.h>

int main()
{
	srand(time(NULL));
	Queue queue;
	int ret, i, num;

	ret = InitQueue(&queue);
	if(SUCCESS == ret)
	{
		printf("初始化成功!\n");
	}
	else
	{
		printf("初始化失败!\n");
	}

	ret = EmptyQueue(queue);
	if(TRUE == ret)
	{
		printf("队列是空!\n");
	}
	else
	{
		printf("队列不是空!\n");
	}
	for(i = 0; i < 6; i++)
	{
		num =rand () %10;
		ret = push(&queue, num);
		if(FAILURE == ret)
		{
			printf("%d进队失败!\n", num);
		}
		else
		{
			printf("%d进队成功!\n", num);
		}
	}
	ret = GetTop(queue);
	if (FAILURE == ret)
	{
		printf("没有队头\n");
	}
	else
	{
		printf("队头是%d\n", ret);
	}

	ret = EmptyQueue(queue);
	if (TRUE == ret)
	{
		printf("队列为空!\n");
	}
	else
	{
		printf("队列不为空!\n");
	}

	printf("队列长度%d\n", LengthQueue(queue));

	for (i = 0; i < 5; i++)
	{
		ret = pop(&queue);
		if (FAILURE == ret)
		{
			printf("出队失败!\n");
		}
		else
		{
			printf("%d出队成功!\n", ret);
		}
	}

	ret = ClearQueue(&queue);
	if (FAILURE == ret)
	{
		printf("清空失败!\n");
	}
	else
	{
		printf("清空成功!\n");
	}

	ret = EmptyQueue(queue);
	if (TRUE == ret)
	{
		printf("队列为空!\n");
	}
	else
	{
		printf("队列不为空!\n");
	}

	ret = DestroyQueue(&queue);
	if (SUCCESS == ret)
	{
		printf("销毁成功!\n");
	}
	else
	{
		printf("销毁失败!\n");
	}

	for (i = 0; i < 10; i++)
	{
		num = rand() % 10;
		ret = push(&queue, num);
		if (SUCCESS == ret)
		{
			printf("%d进队成功!\n", num);
		}
		else
		{
			printf("%d进队失败!\n", num);
		}
	}

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值