LinkQueue

LinkQueue.c 

/***************************************
author:
mail:
desc:链表队列的线性实现函数

**************************************/

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

/*
return:
	OK:表示初始化成功
	OVERFLOW:表示初始化失败,内存未开成功
parm:
	*Q:表示要开辟的空间的链表的地址

*/
int InitQueue(LinkQueue *Q)
{ 
	Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode));
	if(!Q->front)
		exit(OVERFLOW);
	Q->front->next=NULL;
	return OK;
}

/*
return :
	无
parm:
	将Q清为空队列 

*/

void ClearQueue(LinkQueue *Q)
{
	QueuePtr p,q;
	Q->rear=Q->front;
	p=Q->front->next;
	Q->front->next=NULL;
	while(p)
	{
		 q=p;
		 p=p->next;
		 free(q);
	}
}

/*
return :
	无
parm:
	*Q:将销毁Q队列
	 
*/

void DestroyQueue(LinkQueue *Q)
{
	while(Q->front)
	{
		 Q->rear=Q->front->next;
		 free(Q->front);
		 Q->front=Q->rear;
	}
}

/*
return :
	TRUE:Q为空队列
	FALSE:Q不为空队列
parm:
	判断Q是否为空
	
*/

int QueueEmpty(LinkQueue Q)
{
	if(Q.front==Q.rear)
	return TRUE;
	else FALSE;
}

/*
return :
	i:队列中元素的个数
parm:
	Q:队列
	
*/

int QueueLength(LinkQueue Q)
{ 
	int i=0;
	QueuePtr p;
	p=Q.front;
	while(Q.rear!=p)
	{
		 i++;
		 p=p->next;
	}
	return i;
}

/*
return :
	OK:队列不空,返回Q的队头元素
	ERROR:空队列 
parm:
	Q:队列
	*e:队头元素
	
*/

int GetHead(LinkQueue Q, QElemType e)
{ 
	QueuePtr p;
	if(Q.front==Q.rear)
		return ERROR;
	p=Q.front->next;
	e=p->data;
	return e;
}

/*
return :
    OK:分配空间成功
    OVERFLOW:分配空间失败
parm:
	Q:队列
	e :需要入队的元素
	
*/

int EnQueue(LinkQueue *Q,QElemType e)
{ 
	QueuePtr s=(QueuePtr)malloc(sizeof(QNode));
	if(!s) /* 存储分配失败 */
		exit(OVERFLOW);
	s->data=e;
	s->next=NULL;
	Q->rear->next=s;	/* 把拥有元素e的新结点s赋值给原队尾结点的后继,*/
	Q->rear=s;		/* 把当前的s设置为队尾结点,rear指向s*/
	return OK;
}

/*
return :
    OK:元素出队成功
    ERROR: 元素出队失败
parm:
	Q:队列
	*e:出队列的元素
*/

int DeQueue(LinkQueue *Q,QElemType *e)
{ 
	QueuePtr p;
	if(Q->front==Q->rear)
	     return ERROR;
	p=Q->front->next;
	*e=p->data;
	Q->front->next=p->next;
	if(Q->rear==p)
		Q->rear=Q->front;
	free(p);
	return OK;
}

/*
return :
    无
parm:
	Q:访问队列中的元素

*/

void QueueTraverse(LinkQueue Q)
{
	QueuePtr p;
	p=Q.front->next;
	while(p)
	{
		 visit(p->data);
		 p=p->next;
	}
	printf("\n");
}

LinkQueue.h

#ifndef LINKQUEUE_H
#define LINKQUEUE_H
#include <stdio.h>
#include <stdlib.h>

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

typedef int QElemType;

typedef struct QNode
{
	QElemType data;
	struct QNode * next;
}QNode;

typedef QNode* QueuePtr;

typedef struct
{
	QueuePtr front;		//头指针
	QueuePtr rear;		//尾指针
}LinkQueue;			//队列的链式存储表示

/*链栈函数列表*/
int InitQueue(LinkQueue *Q);	//初始化队列

void ClearQueue(LinkQueue *Q);	//置空Q

void DestroyQueue(LinkQueue *Q);	//销毁Q

int QueueEmpty(LinkQueue Q);	//判断Q是否为空

int QueueLength(LinkQueue Q);	//返回Q元素的个数

int GetHead(LinkQueue Q,QElemType e);	//获取队头元素

int EnQueue(LinkQueue *Q,QElemType e);	//元素e入队

int DeQueue(LinkQueue *Q,QElemType *e);	//元素e出队

void QueueTraverse(LinkQueue Q);	//访问元素

#endif

main.c

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

//访问函数
int visit(QElemType c)
{
	printf("%d ",c);
	return OK;
}

//如下为验证过程
void main()
{
	int i, n, x;
	QElemType d;
	LinkQueue q;
	printf("初始化序列\n");
	i=InitQueue(&q);
	printf("队列的长度为%d\n",QueueLength(q));
	printf("成功地构造了一个空队列!\n");
	printf("请输入需要入队的元素个数\n");
	scanf("%d", &n);
	printf("请输入需要入队的元素\n");
	for(i = 0; i < n; i++)
    {
        scanf("%d", &x);
        EnQueue(&q,x);
    }
	printf("插入%d个元素后,队列的长度为%d\n",n,QueueLength(q));
	printf("队列的元素依次为:\n");
	QueueTraverse(q);
	i=GetHead(q,d);
	if(i != 0)
	{    		
		printf("目前队列中队头元素是:%d\n",i);
	}
	printf("队头元素出队\n");
	DeQueue(&q,&d);
	printf("队列的元素依次为:\n");
	QueueTraverse(q);
	printf("正在清空队列\n");
	ClearQueue(&q);
	printf("正在销毁队列\n");
        printf("销毁队列成功\n");
	DestroyQueue(&q);
}

makefile

main:main.o LinkQueue.o
	gcc -g main.o LinkQueue.o -o main
main.o:main.c LinkQueue.h
	gcc -g -c main.c -o main.o
LinkQueue.o:LinkQueue.c LinkQueue.h
	gcc -g -c LinkQueue.c -o LinkQueue.o

clean:
	rm -fr *.o
	rm -fr main

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值