数据结构D7

计算循环队列的长度

实现函数:

//计算队列长度
int len_queue(queue_list list)
{
	if(NULL == list || list->front == list->rear)
	{
		printf("ERROR\n");
		return -1;
	}
	int len = 0;
	int front = 0;
	int rear = 0;
	for(int i = list->front ; i <MAXSIZE-1 ; i = (i+1)%MAXSIZE)
	{
		front++;
	}
	for(int i = 0 ; i <= list->rear ; i = (i+1)%MAXSIZE)
	{
		rear++;
	}
	len = (front + rear)%MAXSIZE;
	return len;
}

主函数:

 	queue_list list = create();
	int element = 0;
	char str[10] = "";
	
	//插入
	do
	{
		printf("请输入插入的值:");
		scanf("%d",&element);
		loop_enqueue(list,element);
		printf("是否继续? yes/no:");
		scanf("%s",str);
	}while(strcmp(str,"no"));

	//遍历输出
	printf("遍历输出:\n");
	loop_output(list);
    
    //计算队列长度
	int len = len_queue(list);
	if(len == -1)
	{
		printf("ERROR\n");
	}
	else
	{
		printf("队列长度=%d\n",len);
	}
	
	//删除
	do
	{
		loop_delqueue(list);
		printf("是否继续? yes/no:");
		scanf("%s",str);
	}while(strcmp(str,"no"));

	printf("遍历输出:\n");
	loop_output(list);

    //计算队列长度
	len = len_queue(list);
	if(len == -1)
	{
		printf("ERROR\n");
	}
	else
	{
		printf("队列长度=%d\n",len);
	}

运行结果:

Xmind:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值