循环链式队列


//定义结点
typedef struct QNode{
 
	ElemType data;
	struct QNode *next;
 
}QNode,*QNodeptr;
 
//该结构体含有两个结点指针变量
typedef struct{
 
	QNodeptr front;//队头指针
	QNodeptr rear;//队尾指针
 
}LinkQueue;

//初始化循环链式队列
Status Init_LQueue(LinkQueue *L)
{
	L->rear=L->front=(QNodeptr)malloc(sizeof(QNode));//两个指针都指向头结点
	if(!L->front)
		return ERROR;
 
	L->front->next = L->front;
 
	return OK;
}
 
 //销毁
Status Destroy_LQueue(LinkQueue *L)
{
	QNodeptr p,q;
	p = L->front->next;
 
	while(p!=L->front)
	{
		q = p;
		p = p->next;
		free(q);
	}
	free(p);
	return OK;
}
 
Status Clear_LQueue(LinkQueue *L)
{
	QNodeptr p,q;
	p = L->front->next;
 
	while(p!=L->front)
	{
		q = p;
		p = p->next;
		free(q);
	}
	L->rear = L->front;
	L->front->next = L->front;
 
	return OK;
}

 //判空
Status Empty_LQueue(LinkQueue L)
{
	if(L.front->next==L.front)
		return true;
	else
		return false;
}
 
 //输出长度
int    Length_LQueue(LinkQueue L)
{
	int count=0;
	QNodeptr p=L.front;
 
	while(p!=L.rear)
	{
		++count;
		p=p->next;
	}
 
	return count;
}
 
Status GetHead_LQueue(LinkQueue L,ElemType *e)
{
	if(Empty_LQueue(L))
		return ERROR;
	else
		*e = L.front->next->data;
 
	return OK;
}

 //入队
Status En_LQueue(LinkQueue *L,ElemType e)
{
	QNodeptr s;
 
	s=(QNodeptr)malloc(sizeof(QNode));
	if(!s)
		return ERROR;
 
	s->data = e;
	s->next = L->front;
	L->rear->next = s;
	L->rear = s;//rear指向最后一个元素
 
	return OK;
}

//出队 
Status De_LQueue(LinkQueue *L,ElemType *e)
{
	if(Empty_LQueue(*L))
		return ERROR;
	else
	{
		*e = L->front->next->data;
		L->front->next=L->front->next->next;
	}
	return 0;
}
 
void   visit(ElemType c)
{
	printf("%d ",c);
}

//输出所有节点数据 
Status Out_LQueue(LinkQueue L)
{
	QNodeptr p=L.front->next;
 
	printf("循环链队列内容: ");
	while(p!=L.front)
	{
		visit(p->data);
		p=p->next;
	}
 
	printf("\n\n");
 
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值