关于队列queue

用数组构造队列

//队列结构体(用数组构造)
typedef struct queue
{
	int queuesize;	//数组大小
	int head;		//队列的头下标
	int tail;		//队列的尾下标
	int *sz;		//数组头指针
}Queue;
//初始化队列
void InitQueue(Queue *q)
{
	q->queuesize = 8;//数组的大小
	q->sz = (int*)malloc(q->queuesize*sizeof(int));
	q->head = 0;//头下标,尾下标都为0
	q->tail = 0;
}
//进队(插入)
void EnQueue(Queue *q,int key)
{
	//这个值主要是判定队列是否满了,以及让尾指针后移1位
	int tail_next = (q->tail+1)%q->queuesize;
	if (tail_next == q->head)
	{
		printf("队列满了!");
	}
	else
	{
		q->sz[q->tail] = key;
		q->tail = tail_next;
	}
	//分析一下,插入第一个数据,
	//填满[0]后,tail == 1
	//填满[1]后,tail == 2
	//填满[2]后,tail == 3
	//填满[3]后,tail == 4
	//填满[4]后,tail == 5
	//填满[5]后,tail == 6
	//填满[6]后,tail == 7
	//这个时候已经满了!!所以说“浪费”了一个空间,不然的话没法区分队列是满了还是空了!
	//(q->tail+1)%q->queuesize == head 的时候就是满了,q->tail == head时则是空队列!
}
//出队(删除)
int DeQueue(Queue *q)
{
	int tmp;
	if (q->tail == q->head)//队列为空
	{
		printf("队列是空的\n");
	}
	else
	{
		tmp = q->sz[q->head];
		q->head = (q->head+1)%q->queuesize;
	}
	return tmp;
}
//判断队列是否为空
int IsQueueEmpty(Queue *q)
{
	if (q->head == q->tail)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
//判断队列是否满了
int IsQueueFull(Queue *q)
{
	if ((q->tail+1)%q->queuesize == q->head)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
参考网址:<a target=_blank href="http://www.cnblogs.com/kaituorensheng/archive/2013/02/28/2937865.html" target="_blank">http://www.cnblogs.com/kaituorensheng/archive/2013/02/28/2937865.html</a>
<a target=_blank href="http://blog.csdn.net/lpp0900320123/article/details/20694409" target="_blank">http://blog.csdn.net/lpp0900320123/article/details/20694409</a>

用链表构造队列

struct Node
{
	int data;	//值域
	struct Node *next;	//链接指针
};
struct queue
{
	struct Node *front;	//队首指针
	struct Node *rear;	//队尾指针
};
//初始化链表队列
void initQueue(struct queue *hq)
{
	hq->front = hq->rear = NULL;//把队首和队尾指针置空
}
//队列是否为空
int emptyQueue(struct queue *hq)
{
	//队首或者队尾任一个指针为空即为空
	if (hq->front == NULL)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
//向链表队列中插入一个元素x
void inQueue(struct queue *hq,int x)
{
	struct Node *newNode;
	newNode = (struct Node*)malloc(sizeof(struct Node));
	if (newNode == NULL)
	{
		printf("内存空间分配失败!");
		exit(1);
	}
	newNode->data = x;
	newNode->next = NULL;
	//若队列为空,则新节点既是队首节点又是队尾节点
	if (emptyQueue(hq))
	{
		hq->front = hq->rear = newNode;
	}
	//队列非空,队尾节点的指针域和队尾指针,指向该节点
	else
	{
<span style="white-space:pre">		</span>//这里很重要,顺序不能错
		hq->rear = hq->rear->next = newNode;
<span style="white-space:pre">		</span>//hq->rear->next = newNode;
<span style="white-space:pre">		</span>//hq->rear = newNode;
	}
}
//出队
int delQueue(struct queue *hq)
{
	struct Node *p;
	int temp;
	if (emptyQueue(hq))
	{
		printf("队列为空,无法删除!");
		exit(1);
	}
	temp = hq->front->data;
	p = hq->front;
	hq->front = p->next;//使队首指针指向下一个节点
	//若删除后,队列为空,则使队尾指针也为空
	if (hq->front == NULL)
	{
		hq->rear = NULL;
	}
	free(p);//回收原队首节点
	return temp;//返回被删除的队首元素值
}
//清除队列中的所有元素
void clearQueue(struct queue *hq)
{
	struct Node *p = hq->front;
	//依次删除队列中的每一个节点,最后,队首指针为空,再置队尾指针为空
	while(p != NULL)
	{
		hq->front = hq->front->next;
		free(p);
		p = hq->front;
	}
	//循环结束后,队首指针已经为空
	hq->rear = NULL;//置队尾指针为空
}
//读取队首元素
int peekQueue(struct queue *hq)
{
	if (hq->front == NULL)
	{
		printf("队列为空,无法获得队首元素!");
		exit(1);
	}
	return hq->front->data;

}
int _tmain(int argc, _TCHAR* argv[])
{
	struct queue q;
	int a[8] = {3,8,5,17,9,30,15,22};
	int i ;
	initQueue(&q);
	for (i = 0;i<8;i++)
	{
		inQueue(&q,a[i]);
	}
	printf("delnode is %d\n",delQueue(&q));
	printf("delnode is %d\n",delQueue(&q));
	inQueue(&q,68);
	printf("peeknode is %d\n",peekQueue(&q));
	while (!emptyQueue(&q))
	{
		printf("%d\n",delQueue(&q));
	}
	clearQueue(&q);

	getchar();
	return 0;
}
运行结果:


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值