队列及基本功能的实现

头文件

1.顺序队列

2.循环顺序队列

3.链表实现的队列

​
#pragma once
#ifndef _QUEUE_H_
#define _QUEUE_H_

#include<stdio.h>
#include<assert.h>
#include<malloc.h>
#include<stdbool.h>
#define Queue_ElemType char
#define QUEUE_DEFAULT_SIZE 8

//顺序队列
typedef struct SeqQueue
{
	Queue_ElemType* base; //队列空间
	size_t          capacity;
	int             front;
	int             rear;
}SeqQueue;

//函数申明
void SeqQueueInit(SeqQueue* psq);
bool SeqQueueFull(SeqQueue* psq);
bool SeqQueueEmpty(SeqQueue* psq);
Queue_ElemType SeqQueueFront(SeqQueue* psq);
Queue_ElemType SeqQueueBack(SeqQueue* psq);
void SeqQueuePush(SeqQueue* psq, Queue_ElemType v);
void SeqQueuePop(SeqQueue* psq);
void SeqQueueShow(SeqQueue* psq);
void SeqQueueDestroy(SeqQueue* psq);

void SeqQueueInit(SeqQueue* psq)
{
	assert(psq!=NULL);
	psq->base = (Queue_ElemType*)malloc(sizeof(Queue_ElemType)* QUEUE_DEFAULT_SIZE);
	assert(psq->base!=NULL);
	psq->capacity = QUEUE_DEFAULT_SIZE;
	psq->front = psq->rear = 0;
}

bool SeqQueueFull(SeqQueue* psq)
{
	assert(psq != NULL);
	return psq->rear > psq->capacity;
}

bool SeqQueueEmpty(SeqQueue* psq)
{
	assert(psq != NULL);
	return psq->front == psq->rear;
}

//取队头元素
Queue_ElemType SeqQueueFront(SeqQueue* psq)
{
	assert(psq != NULL && !SeqQueueEmpty(psq));
	return psq->base[psq->front];
}

//取队尾元素
Queue_ElemType SeqQueueBack(SeqQueue* psq)
{
	assert(psq != NULL && !SeqQueueEmpty(psq));
	return psq->base[psq->rear - 1];
}

void SeqQueuePush(SeqQueue* psq, Queue_ElemType v)
{
	assert(psq != NULL && !SeqQueueFull( psq));
	psq->base[psq->rear++] = v;
}

void SeqQueuePop(SeqQueue* psq)
{
	assert(psq != NULL && !SeqQueueEmpty(psq));
	psq->front++;
}

void SeqQueueShow(SeqQueue* psq)
{
	assert(psq != NULL);
	int i = psq->front;
	while (i < psq->rear)
	{
		printf("%c", psq->base[i++]);
		printf("\n");
	}
	printf("\n");
}
void SeqQueueDestroy(SeqQueue* psq)
{
	assert(psq != NULL);
	free(psq->base);
	psq->base = NULL;
	psq->capacity = psq->front = psq->rear = 0;
}


///

//循环队列

typedef struct CircleQueue
{
	Queue_ElemType* base;
	size_t          capacity;
	int             front;
	int             rear;
}CircleQueue;

//函数申明
void CircleQueueInit(CircleQueue* psq);
bool CircleQueueFull(CircleQueue* psq);
bool CircleQueueEmpty(CircleQueue* psq);
Queue_ElemType CircleQueueFront(CircleQueue* psq);
Queue_ElemType CircleQueueBack(CircleQueue* psq);
void CircleQueuePush(CircleQueue* psq, Queue_ElemType v);
void CircleQueuePop(CircleQueue* psq);
void CircleQueueShow(CircleQueue* psq);
void CircleQueueDestroy(CircleQueue* psq);

//函数实现

void CircleQueueInit(CircleQueue* psq)
{
	assert(psq != NULL);
	psq->base = (Queue_ElemType*)malloc(sizeof(Queue_ElemType) * (QUEUE_DEFAULT_SIZE + 1));//多出的空间啥也不存
	assert(psq->base!=NULL);                                                            //用以区分队满和队空状态
	psq->capacity = QUEUE_DEFAULT_SIZE;
	psq->front = psq->rear = 0;
}

bool CircleQueueFull(CircleQueue* psq)
{
	assert(psq != NULL);
	return (psq->rear + 1) % (psq->capacity + 1) == psq->front;
}

bool CircleQueueEmpty(CircleQueue* psq)
{
	assert(psq != NULL);
	return psq->front == psq->rear;
}

Queue_ElemType CircleQueueFront(CircleQueue* psq)
{
	assert(psq != NULL && !CircleQueueEmpty(psq));
	return psq->base[psq->front];
}

Queue_ElemType CircleQueueBack(CircleQueue* psq)
{
	assert(psq != NULL && !CircleQueueEmpty(psq));
	return psq->base[(psq->rear-1) +( psq->capacity +1) % (psq->capacity+1)];//注意这里的括号!!!!
}

void CircleQueuePush(CircleQueue* psq, Queue_ElemType v)
{
	assert(psq != NULL);
	if (CircleQueueFull(psq))
	{
		printf("循环队列已满\n");
		return;
	}
	psq->base[psq->rear] = v;
	psq->rear=(psq->rear + 1 ) % (psq->capacity + 1);
}

void CircleQueuePop(CircleQueue* psq)
{
	assert(psq != NULL);
	if (CircleQueueEmpty(psq))
	{
		printf("循环队列是空队,不能出队\n");
		return;
	}
	psq->front = (psq->front + 1) % (psq->capacity + 1);
}

void CircleQueueShow(CircleQueue* psq)
{
	assert(psq != NULL);
	int i = psq->front;
	while (i!=psq->rear)
	{
		printf("%c ", psq->base[i]);
		i++;
		i = i % (psq->capacity + 1);
	}
	printf("\n ");
}

void CircleQueueDestroy(CircleQueue* psq)
{
	assert(psq != NULL);
	free(psq->base);
	psq->base = NULL;
	psq->capacity = psq->front = psq->rear =  0;
}


///

//链队列


typedef struct LinkQueueNode
{
	Queue_ElemType data;
	struct LinkQueueNode* next;
}LinkQueueNode;

typedef struct LinkQueue
{
	LinkQueueNode* front;
	LinkQueueNode* rear;
}LinkQueue;

//函数申明
void LinkQueueInit(LinkQueue* psq);
bool LinkQueueEmpty(LinkQueue* psq);
Queue_ElemType LinkQueueFront(LinkQueue* psq);
Queue_ElemType LinkQueueBack(LinkQueue* psq);
void LinkQueuePush(LinkQueue* psq, Queue_ElemType v);
void LinkQueuePop(LinkQueue* psq);
void LinkQueueShow(LinkQueue* psq);
void LinkQueueDestroy(LinkQueue* psq);

//函数实现
void LinkQueueInit(LinkQueue* psq)
{
	assert(psq != NULL);
	psq->front = psq->rear = NULL;
}

bool LinkQueueEmpty(LinkQueue* psq)
{
	assert(psq != NULL);
	return psq->front == NULL;
}

Queue_ElemType LinkQueueFront(LinkQueue* psq)
{
	assert(psq != NULL && !LinkQueueEmpty(psq));
	return psq->front->data;
}

Queue_ElemType LinkQueueBack(LinkQueue* psq)
{
	assert(psq != NULL && !LinkQueueEmpty(psq));
	return psq->rear->data;
}

void LinkQueuePush(LinkQueue* psq, Queue_ElemType v)
{
	assert(psq != NULL);
	LinkQueueNode* s = (LinkQueueNode*)malloc(sizeof(LinkQueueNode));
	assert(s != NULL);
	s->data = v;
	s->next = NULL;

	if (psq->front == NULL)
		psq->front = psq->rear = s;//若是空队列则将s节点插入为第一个节点
	{
		psq->rear->next = s;//将尾指针指向s节点,相当于是插入在队尾
		psq->rear = s;     //更新尾指针
	}
}
void LinkQueuePop(LinkQueue* psq)
{
	assert(psq != NULL && !LinkQueueEmpty(psq));
	LinkQueueNode* s = psq->front;
	psq->front = psq->front->next;

	free(s);
}

void LinkQueueShow(LinkQueue* psq)
{
	assert(psq != NULL);
	LinkQueueNode* p = psq->front;

	while (p != NULL)
	{
		printf("%c ", p->data);
		p = p->next;
	}
	printf("\n");
}

void LinkQueueDestroy(LinkQueue* psq)
{
	assert(psq != NULL);
	
	while (psq->front != NULL)
	{
		LinkQueueNode* q = psq->front;
		(psq->front) = q->next;
		free(q);
	}
}


#endif /* _QUEUE_H_ */

​

主函数是对我们基本功能的测试

#include"queue.h"

void main()
{
	LinkQueue Q;
	LinkQueueInit(&Q);
	LinkQueuePush(&Q, 'A');
	LinkQueuePush(&Q, 'B');
	LinkQueuePush(&Q, 'C');
	LinkQueuePush(&Q, 'D');

	LinkQueueShow(&Q);
	printf("front = %c\n", LinkQueueFront(&Q));
	printf("back = %c\n", LinkQueueBack(&Q));

	LinkQueuePop(&Q);
	LinkQueueShow(&Q);
	printf("front = %c\n", LinkQueueFront(&Q));
	printf("back = %c\n", LinkQueueBack(&Q));

	LinkQueueDestroy(&Q);
}

/*
void main()
{
	CircleQueue Q;
	CircleQueueInit(&Q);
	CircleQueuePush(&Q, 'A');
	CircleQueuePush(&Q, 'B');
	CircleQueuePush(&Q, 'C');
	CircleQueuePush(&Q, 'D');
	CircleQueuePush(&Q, 'E');
	CircleQueuePush(&Q, 'F');
	CircleQueuePush(&Q, 'G');
	CircleQueuePush(&Q, 'H');
	CircleQueueShow(&Q);

	CircleQueuePop(&Q);
	CircleQueuePush(&Q, 'X');
	CircleQueueShow(&Q);

	CircleQueuePop(&Q);
	CircleQueuePush(&Q, 'Y');
	CircleQueueShow(&Q);

	printf("front = %c\n", CircleQueueFront(&Q));
	printf("back = %c\n", CircleQueueBack(&Q));
}

/*
void main()
{
	SeqQueue Q;
	SeqQueueInit(&Q);
	SeqQueuePush(&Q, 'A');
	SeqQueuePush(&Q, 'B');
	SeqQueuePush(&Q, 'C');
	SeqQueuePush(&Q, 'D');
	SeqQueuePush(&Q, 'E');
	SeqQueuePush(&Q, 'F');
	SeqQueuePush(&Q, 'G');
	SeqQueuePush(&Q, 'H');

	//SeqQueuePush(&Q, 'X');这里我们断言(assert),了队列满了就不能再入队了

	SeqQueuePop(&Q);
	SeqQueuePush(&Q, 'Y');
	SeqQueueShow(&Q);
}

/*
void main()
{
	SeqQueue Q;
	SeqQueueInit(&Q);
	SeqQueuePush(&Q, 'A');
	SeqQueuePush(&Q, 'B');
	SeqQueuePush(&Q, 'C');
	SeqQueuePush(&Q, 'D');

	while (!SeqQueueEmpty(&Q))
	{
		char val = SeqQueueFront(&Q);
		printf("%c 出队.\n", val);
		SeqQueuePop(&Q);
	}

	SeqQueueDestroy(&Q);

}

/*
void main()
{
	SeqQueue Q;
	SeqQueueInit(&Q);
	SeqQueuePush(&Q, 'A');
	SeqQueuePush(&Q, 'B');
	SeqQueuePush(&Q, 'C');
	SeqQueuePush(&Q, 'D');
	SeqQueueShow(&Q);

	SeqQueuePop(&Q);
	SeqQueueShow(&Q);

	SeqQueuePop(&Q);
	SeqQueueShow(&Q);

	SeqQueueDestroy(&Q);
}
*/

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值