【数据结构】03栈与队列-队列

1. 队列的定义

队列(queue)是只允许在一端进行插入操作,而在另一端进行删除操作的线性表。队列是一种先进先出(First In First Out,FIFO)的线性表,简称FIFO结构。允许差于的一端称为队尾,允许删除的一端称为队头。
线性表有顺序存储和链式存储,栈是线性表,也具有这两种存储方式。同样的,队列作为一种特殊的线性表,
同样存在这两种存储方式。但是,顺序存储结构的队列有严重的缺陷。

2. 队列的顺序存储

2.1 队列顺序存储的不足

入队操作(即在队尾插入元素),其实就是在队尾追加一个元素,不需要移动任何元素,时间复杂度是 O ( 1 ) O(1) O(1)。与栈不同的是,队列元素的出队是在对头,即下标为0的位置,那么也就意味着,队头元素出队后,其后面的元素都会向前移动一位,此时的时间复杂度为 O ( n ) O(n) O(n)

2.2 顺序队列-循环队列

把队列的头尾相接的顺序存储结构称为循环队列。
定义front指针指向队头元素,定义rear指针指向队尾元素的下一个位置。如果数组末尾元素已经占用,rear再向后会产生数组越界的错误,此时,rear就需要跳转到下标为0的位置,从头开始,也就是头尾相接的循环。
这样的话rear有可能比front小,也有可能比front大。

  1. 如何判断队列是否满呢?
    假设数组大小为MAXSIZE。那么队列满的条件是 (rear+1)%MAXSIZE==front。(rear和front是从0开始的指针,因此rear+1,对MAXSIZE取余,等于front证明刚好循环一圈为MAXSIZE,此时队列满)。例如整个队列大小为5,rear指向了4,front指向了0。那么 ( 4 + 1 ) % 5 = = 0 (4+1)\%5==0 (4+1)%5==0,此时队列满了;rear指向了1,front指向了2,那么 ( 1 + 1 ) % 5 = = 2 (1+1)\%5==2 (1+1)%5==2,队列满了。
  2. 如何计算队列长度(队列元素个数)呢?
  • 当rear>front时,rear-front就是队列长度。
  • 当rear<front时,队列长度分为两段:MAXSIZE-front和0+rear,即rear-front+MAXSIZE。

因此通用的队列长度计算公式:(rear-front+MAXSIZE)%MAXSIZE

2.3 具体实现

2.3.1 初始化

初始化一个空队列

typedef int ElemType;
#define MAXSIZE 10
struct SqQueue
{
	ElemType data[MAXSIZE];
	int front; // 队头指针
	int rear;  // 队尾指针
};

// 初始化队列
bool InitQueue(SqQueue* sq)
{
	sq->front = 0;
	sq->rear = 0;
	memset(sq->data, 0, sizeof(ElemType) * MAXSIZE);
	return true;
}

2.3.2 入队操作

将元素插入到队尾,即rear指针指向的位置,然后将rear指针循环后移一位即rear=(rear+1)%MAXSIZE。

// 入队
bool EnQueue(SqQueue* sq, ElemType e)
{
	if (QueueLength(sq) == MAXSIZE) // 队列满,无法入队
	{
		return false;
	}
	// 入队
	sq->data[sq->rear] = e;
	sq->rear = (sq->rear + 1) % MAXSIZE; // rear指针循环后移一位
	return true;
}

2.3.3 出队操作

出队操作,即删除front指针指向的元素,并将front指针指向下一个元素,即front指针循环后移一位:front=(front+1)%MAXSIZE:

// 出队
bool DeQueue(SqQueue* sq, ElemType* e)
{
	if (QueueLength(sq) == 0)// 队列空,无法出队
	{
		return false;
	}
	// 拷贝数据
	*e = sq->data[sq->front];
	sq->front = (sq->front + 1)%MAXSIZE; // front指针循环后移一位
	return true;
}

2.3.4 完整实现例子

#include <iostream>
using namespace std;
typedef int ElemType;
#define MAXSIZE 10
struct SqQueue
{
	ElemType data[MAXSIZE];
	int front; // 队头指针
	int rear;  // 队尾指针
};

// 初始化队列
bool InitQueue(SqQueue* sq)
{
	sq->front = 0;
	sq->rear = 0;
	memset(sq->data, 0, sizeof(ElemType) * MAXSIZE);
	return true;
}

// 返回队列长度
int QueueLength(SqQueue* sq)
{
	return (sq->rear - sq->front + MAXSIZE) % MAXSIZE;
}

// 获取队头元素
bool FrontQueue(SqQueue* sq, ElemType* e)
{
	if (sq==nullptr||QueueLength(sq) == 0) // 队列长度为空
	{
		return false;
	}
	*e = sq->data[sq->front];
	return true;
}

// 获取队尾元素
bool RearQueue(SqQueue* sq, ElemType* e)
{
	if (sq == nullptr || QueueLength(sq) == 0) // 队列长度为空
	{
		return false;
	}
	*e = sq->data[sq->rear-1];
	return true;
}

// 入队
bool EnQueue(SqQueue* sq, ElemType e)
{
	if (QueueLength(sq) == MAXSIZE) // 队列满,无法入队
	{
		return false;
	}
	// 入队
	sq->data[sq->rear] = e;
	sq->rear = (sq->rear + 1) % MAXSIZE; // rear指针循环后移一位
	return true;
}

// 出队
bool DeQueue(SqQueue* sq, ElemType* e)
{
	if (QueueLength(sq) == 0)// 队列空,无法出队
	{
		return false;
	}
	// 拷贝数据
	*e = sq->data[sq->front];
	sq->front = (sq->front + 1)%MAXSIZE; // front指针循环后移一位
	return true;
}

int main(void)
{
	SqQueue sq;
	InitQueue(&sq);
	ElemType e;
	e = 1; EnQueue(&sq, e); FrontQueue(&sq, &e); cout << "队头元素:" << e;  RearQueue(&sq, &e); cout << ",队尾元素:" << e; cout << ",队列长度" << QueueLength(&sq) << endl;
	e = 2; EnQueue(&sq, e); FrontQueue(&sq, &e); cout << "队头元素:" << e;  RearQueue(&sq, &e); cout << ",队尾元素:" << e; cout << ",队列长度" << QueueLength(&sq) << endl;
	e = 3; EnQueue(&sq, e); FrontQueue(&sq, &e); cout << "队头元素:" << e;  RearQueue(&sq, &e); cout << ",队尾元素:" << e; cout << ",队列长度" << QueueLength(&sq) << endl;
	DeQueue(&sq, &e); cout << "出队元素:" << e << ",队列长度" << QueueLength(&sq) << endl;
	e = 4; EnQueue(&sq, e); FrontQueue(&sq, &e); cout << "队头元素:" << e;  RearQueue(&sq, &e); cout << ",队尾元素:" << e; cout << ",队列长度" << QueueLength(&sq) << endl;
	return 0;
}

3. 队列的链式存储结构

队列的链式存储结构,其实就是线性表的单链表,只不过它只能尾进头出,简称为链队列。
空队列时,front和rear指针都指向头结点。
链队列的结构为:

typedef int ElemType;
struct Node
{
	ElemType data; // 数据域
	Node* next; // 指针域
};
typedef Node* LinkQueuePtr; // 结点指针
struct LinkQueue
{
	LinkQueuePtr front,rear; // 队头,队尾指针
};

3.1 入队操作

入队:将新结点插入到队尾,即当前rear指向的结点之后

// 入队,将元素插入队尾
bool EnQueue(LinkQueue* lq, ElemType e)
{
	if (lq == nullptr)// 队列不存在
	{
		return false;
	}
	// 入队
	// 1. 准备数据
	Node* new_elem = new Node;
	new_elem->data = e;
	new_elem->next = nullptr;
	// 2. 插入到队尾
	lq->rear->next = new_elem; // 当前队尾结点的下一个结点为new_elem
	lq->rear = new_elem; // 队尾rear指针指向new_elem,更新rear指针

	lq->count++;
	return true;
}

3.2 出队操作

出队:将队头删除,但是front指针是链表的头结点,而不是队列的头,因此队列的头结点是front->next,需要删除的是front->next结点,更新的也是front->next指向的结点

// 出队,将队头元素删除
bool DeQueue(LinkQueue* lq, ElemType* e)
{
	if (lq == nullptr || lq->front == lq->rear)// 队列不存在或队列为空
	{
		return false;
	}
	// 出队
	// 1. 记录队头
	Node* front_node = lq->front->next; // 第一个结点
	*e = front_node->data; // 返回数据
	// 2. 删除队头(第一个结点)
	lq->front->next = front_node->next; // 头结点下一个结点指向队头的下一个结点
	if (front_node == lq->rear) // 如果删除的结点也是队尾结点,则将队尾指针指向头结点
		lq->rear = lq->front; 
	delete front_node;
	
	lq->count--;
	return true;
}

3.3 清空队列

清空:删除除头结点外的所有结点,并更新front和rear指针指向链表头结点

// 清空队列
bool ClearQueue(LinkQueue* lq)
{
	if (lq == nullptr ) // 队列不存在
	{
		return false;
	}
	if (lq->front == lq->rear) // 队列为空
	{
		return true;
	}
	// 从队头(第一个结点)开始清空数据
	Node* node_ptr = lq->front->next;
	Node* next_ptr = nullptr;
	while (node_ptr)
	{
		next_ptr = node_ptr->next; //记录下一个结点
		delete node_ptr; // 删除当前结点
		node_ptr = next_ptr; // 跳转至下一个结点
	}
	// 将队尾指针指向头结点,表示链队列为空
	lq->rear = lq->front;
	return true;
}

3.4 完整实现

#include <iostream>
using namespace std;
typedef int ElemType;

struct Node // 结点
{
	ElemType data; // 数据域
	Node* next; // 指针域
}; 

typedef Node* LinkQueuePtr; // 结点指针

struct LinkQueue // 链队列
{
	LinkQueuePtr front, rear; // 队头,队尾指针
	int count; // 记录结点
};

// 初始化空链队列
bool InitQueue(LinkQueue* lq)
{
	Node* head = new Node; // 头结点
	memset(&(head->data), 0, sizeof(ElemType));
	head->next = nullptr;

	// 队头和队尾指向头结点
	lq->front = head;
	lq->rear = head;
	lq->count = 0;
	return true;
}


// 入队,将元素插入队尾
bool EnQueue(LinkQueue* lq, ElemType e)
{
	if (lq == nullptr)// 队列不存在
	{
		return false;
	}
	// 入队
	// 1. 准备数据
	Node* new_elem = new Node;
	new_elem->data = e;
	new_elem->next = nullptr;
	// 2. 插入到队尾
	lq->rear->next = new_elem; // 当前队尾结点的下一个结点为new_elem
	lq->rear = new_elem; // 队尾rear指针指向new_elem,更新rear指针

	lq->count++;
	return true;
}

// 出队,将队头元素删除
bool DeQueue(LinkQueue* lq, ElemType* e)
{
	if (lq == nullptr || lq->front == lq->rear)// 队列不存在或队列为空
	{
		return false;
	}
	// 出队
	// 1. 记录结点
	Node* front_node = lq->front->next; // 第一个结点
	*e = front_node->data; // 返回数据
	// 2. 删除队头(第一个结点)
	lq->front->next = front_node->next;
	if (front_node == lq->rear) // 如果删除的结点也是队尾结点,则将队尾指针指向头结点
		lq->rear = lq->front; 
	delete front_node;
	lq->count--;

	return true;
}

// 获取队列长度
int QueueLength(LinkQueue* lq)
{
	return lq->count;
}

// 获取队头元素
bool FrontQueue(LinkQueue* lq, ElemType* e)
{
	if (lq == nullptr || lq->front==lq->rear) // 队列为空
	{
		return false;
	}
	*e = lq->front->next->data; // 第一个结点的数据
	return true;
}

// 获取队尾元素
bool RearQueue(LinkQueue* lq, ElemType* e)
{
	if (lq == nullptr || lq->front == lq->rear) // 队列不能在或为空
	{
		return false;
	}
	*e = lq->rear->data;
	return true;
}

// 清空队列
bool ClearQueue(LinkQueue* lq)
{
	if (lq == nullptr ) // 队列不存在
	{
		return false;
	}
	if (lq->front == lq->rear) // 队列为空
	{
		return true;
	}
	// 从第一个结点开始清空数据
	Node* node_ptr = lq->front->next;
	Node* next_ptr = nullptr;
	while (node_ptr)
	{
		next_ptr = node_ptr->next; //记录下一个结点
		delete node_ptr; // 删除当前结点
		node_ptr = next_ptr; // 跳转至下一个结点
	}
	// 将队尾指针指向头结点,表示链队列为空
	lq->rear = lq->front;
	return true;
}

// 销毁链队列
bool DeleteQueue(LinkQueue* lq)
{
	Node* head_ptr = lq->front; // 从头结点开始
	Node* next_ptr=nullptr;
	while (head_ptr != nullptr) // 链表不为空
	{
		next_ptr = head_ptr->next; // 记录下一个结点
		delete head_ptr; // 删除当前结点
		head_ptr = next_ptr; // 跳转至下一个结点
	}
	// 清空head_ptr
	head_ptr = nullptr;
	return true;
}

int main(void)
{
	LinkQueue lq;
	// 初始化队列
	InitQueue(&lq);
	ElemType e;
	// 入队
	e = 1; EnQueue(&lq, e); FrontQueue(&lq, &e);
	e = 2; EnQueue(&lq, e); FrontQueue(&lq, &e); 
	e = 3; EnQueue(&lq, e); FrontQueue(&lq, &e);
	e = 4; EnQueue(&lq, e); FrontQueue(&lq, &e);
	e = 5; EnQueue(&lq, e); FrontQueue(&lq, &e);
	cout << "队头元素:" << e; RearQueue(&lq, &e); cout << ",队尾元素:" << e << ",队列长度:" << QueueLength(&lq) << endl;

	// 出队
	DeQueue(&lq, &e); cout << "出队元素:" << e; FrontQueue(&lq, &e); cout << ",队头元素:" << e; RearQueue(&lq, &e); cout << ",队尾元素:" << e << ",队列长度:" << QueueLength(&lq) << endl;
	DeQueue(&lq, &e); cout << "出队元素:" << e; FrontQueue(&lq, &e); cout << ",队头元素:" << e; RearQueue(&lq, &e); cout << ",队尾元素:" << e << ",队列长度:" << QueueLength(&lq) << endl;
	DeQueue(&lq, &e); cout << "出队元素:" << e; FrontQueue(&lq, &e); cout << ",队头元素:" << e; RearQueue(&lq, &e); cout << ",队尾元素:" << e << ",队列长度:" << QueueLength(&lq) << endl;
	DeQueue(&lq, &e); cout << "出队元素:" << e; FrontQueue(&lq, &e); cout << ",队头元素:" << e; RearQueue(&lq, &e); cout << ",队尾元素:" << e << ",队列长度:" << QueueLength(&lq) << endl;
	// 销毁队列
	DeleteQueue(&lq);


	return 0;
}
  • 18
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值