C基础:循环队列

转载请注明出处:http://blog.csdn.net/droyon/article/details/9840779

栈操作的top指针在Push时增大而在Pop时减小,栈空间是可以重复利用的,而队列的head、tail指针都在一直增大,虽然前面的元素已经出队了,但它所占的存储空间却不能重复利用,这样对存储空间的利用效率很低。

环形队列(Circular Queue):把queue数组想像成一个圈,head和tail指针仍然是一直增大的,当指到数组末尾时就自动回到数组开头。就像两个人围着操场赛跑,沿着它们跑的方向看,从head到tail之间是队列的有效元素,从tail到head之间是可以被利用的存储位置。head追上tail就表示队列空了,tail追上head就表示队列的存储空间满了。

使用循环队列实现广度搜索。背景介绍见:http://blog.csdn.net/droyon/article/details/9840617

测试例子:

#include<stdio.h>

#define MAX_ROW 5
#define MAX_COL 5
#define MAX_LENGTH 8//循环队列的空间大小

int curIndex = 0;

struct point {
	int x, y;
}queue[MAX_LENGTH];

int maze[MAX_ROW][MAX_COL] = {
	{0, 1, 0, 0, 0},
	{0, 1, 0, 1, 0},
	{0, 0, 0, 0, 0},
	{0, 1, 1, 1, 0},
	{0, 0, 0, 1, 0},
};

void print_maze()
{
	int i = 0, j = 0;
	printf("第%d次操作结果**************************\n",
	       ++curIndex);
	for (i = 0; i < MAX_ROW; i++) {
		for (j = 0; j < MAX_COL; j++) {
			if (maze[i][j] == 0) {
				printf("%c\t", '#');
			} else if (maze[i][j] == 2) {
				printf("%s\t", "<0>");
			} else {
				printf("%c\t", '|');
			}
		}
		printf("\n");
	}
	printf("**********************************\n");
}

int head=0,tail=0;

void enqueue(struct point p){
    printf("enqueue ****  tail :%d,head :%d\n",tail,head);
    queue[tail] = p;
    if(tail+1<MAX_LENGTH)tail++;
    else if(head!=0){
        tail = 0;
    }
    printf("enqueue #### tail :%d,head : %d\n",tail,head);
}

struct point dequeue(void){
    if(head +1 <=MAX_LENGTH)head++;
    else if(tail != 0){
        head = 0;
        head++;
    }
    return queue[head-1];
}

int isEmpty(void){
    return head == tail;
}

void visit(int row,int col){
    maze[row][col] = 2;
    struct point visit_point = {row,col};
    enqueue(visit_point);
    print_maze();
}

int main(void){
	struct point p = { 0, 0 };
	maze[0][0] = 2;
	enqueue(p);
	while (!isEmpty()) {
		p = dequeue();
		if (p.x == MAX_ROW - 1 && p.y == MAX_COL - 1) {
    		continue;
		}
		if (p.x + 1 < MAX_ROW && maze[p.x + 1][p.y] == 0) {
			visit(p.x + 1, p.y);
		}
		if (p.x - 1 > -1 && maze[p.x - 1][p.y] == 0) {
			visit(p.x - 1, p.y);
		}
		if (p.y + 1 < MAX_COL && maze[p.x][p.y + 1] == 0) {
			visit(p.x, p.y + 1);
		}
		if (p.y - 1 > -1 && maze[p.x][p.y - 1] == 0) {
			visit(p.x, p.y - 1);
		}
	}
    int i = 0;
    for(i=0;i<MAX_LENGTH;i++){
        struct point pp = queue[i];
        printf("pointx:%d,pointy:%d\n",pp.x,pp.y);
    }
	return 0;
}
打印:
enqueue ****  tail :0,head :0
enqueue #### tail :1,head : 0
enqueue ****  tail :1,head :1
enqueue #### tail :2,head : 1
第1次操作结果**************************
<0>	|	#	#	#	
<0>	|	#	|	#	
#	#	#	#	#	
#	|	|	|	#	
#	#	#	|	#	
**********************************
enqueue ****  tail :2,head :2
enqueue #### tail :3,head : 2
第2次操作结果**************************
<0>	|	#	#	#	
<0>	|	#	|	#	
<0>	#	#	#	#	
#	|	|	|	#	
#	#	#	|	#	
**********************************
enqueue ****  tail :3,head :3
enqueue #### tail :4,head : 3
第3次操作结果**************************
<0>	|	#	#	#	
<0>	|	#	|	#	
<0>	#	#	#	#	
<0>	|	|	|	#	
#	#	#	|	#	
**********************************
enqueue ****  tail :4,head :3
enqueue #### tail :5,head : 3
第4次操作结果**************************
<0>	|	#	#	#	
<0>	|	#	|	#	
<0>	<0>	#	#	#	
<0>	|	|	|	#	
#	#	#	|	#	
**********************************
enqueue ****  tail :5,head :4
enqueue #### tail :6,head : 4
第5次操作结果**************************
<0>	|	#	#	#	
<0>	|	#	|	#	
<0>	<0>	#	#	#	
<0>	|	|	|	#	
<0>	#	#	|	#	
**********************************
enqueue ****  tail :6,head :5
enqueue #### tail :7,head : 5
第6次操作结果**************************
<0>	|	#	#	#	
<0>	|	#	|	#	
<0>	<0>	<0>	#	#	
<0>	|	|	|	#	
<0>	#	#	|	#	
**********************************
enqueue ****  tail :7,head :6
enqueue #### tail :0,head : 6
第7次操作结果**************************
<0>	|	#	#	#	
<0>	|	#	|	#	
<0>	<0>	<0>	#	#	
<0>	|	|	|	#	
<0>	<0>	#	|	#	
**********************************
enqueue ****  tail :0,head :7
enqueue #### tail :1,head : 7
第8次操作结果**************************
<0>	|	#	#	#	
<0>	|	<0>	|	#	
<0>	<0>	<0>	#	#	
<0>	|	|	|	#	
<0>	<0>	#	|	#	
**********************************
enqueue ****  tail :1,head :7
enqueue #### tail :2,head : 7
第9次操作结果**************************
<0>	|	#	#	#	
<0>	|	<0>	|	#	
<0>	<0>	<0>	<0>	#	
<0>	|	|	|	#	
<0>	<0>	#	|	#	
**********************************
enqueue ****  tail :2,head :8
enqueue #### tail :3,head : 8
第10次操作结果**************************
<0>	|	#	#	#	
<0>	|	<0>	|	#	
<0>	<0>	<0>	<0>	#	
<0>	|	|	|	#	
<0>	<0>	<0>	|	#	
**********************************
enqueue ****  tail :3,head :1
enqueue #### tail :4,head : 1
第11次操作结果**************************
<0>	|	<0>	#	#	
<0>	|	<0>	|	#	
<0>	<0>	<0>	<0>	#	
<0>	|	|	|	#	
<0>	<0>	<0>	|	#	
**********************************
enqueue ****  tail :4,head :2
enqueue #### tail :5,head : 2
第12次操作结果**************************
<0>	|	<0>	#	#	
<0>	|	<0>	|	#	
<0>	<0>	<0>	<0>	<0>	
<0>	|	|	|	#	
<0>	<0>	<0>	|	#	
**********************************
enqueue ****  tail :5,head :4
enqueue #### tail :6,head : 4
第13次操作结果**************************
<0>	|	<0>	<0>	#	
<0>	|	<0>	|	#	
<0>	<0>	<0>	<0>	<0>	
<0>	|	|	|	#	
<0>	<0>	<0>	|	#	
**********************************
enqueue ****  tail :6,head :5
enqueue #### tail :7,head : 5
第14次操作结果**************************
<0>	|	<0>	<0>	#	
<0>	|	<0>	|	#	
<0>	<0>	<0>	<0>	<0>	
<0>	|	|	|	<0>	
<0>	<0>	<0>	|	#	
**********************************
enqueue ****  tail :7,head :5
enqueue #### tail :0,head : 5
第15次操作结果**************************
<0>	|	<0>	<0>	#	
<0>	|	<0>	|	<0>	
<0>	<0>	<0>	<0>	<0>	
<0>	|	|	|	<0>	
<0>	<0>	<0>	|	#	
**********************************
enqueue ****  tail :0,head :6
enqueue #### tail :1,head : 6
第16次操作结果**************************
<0>	|	<0>	<0>	<0>	
<0>	|	<0>	|	<0>	
<0>	<0>	<0>	<0>	<0>	
<0>	|	|	|	<0>	
<0>	<0>	<0>	|	#	
**********************************
enqueue ****  tail :1,head :7
enqueue #### tail :2,head : 7
第17次操作结果**************************
<0>	|	<0>	<0>	<0>	
<0>	|	<0>	|	<0>	
<0>	<0>	<0>	<0>	<0>	
<0>	|	|	|	<0>	
<0>	<0>	<0>	|	<0>	
**********************************
pointx:0,pointy:4
pointx:4,pointy:4
pointx:4,pointy:2
pointx:0,pointy:2
pointx:2,pointy:4
pointx:0,pointy:3
pointx:3,pointy:4
pointx:1,pointy:4

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hailushijie

您的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值