数据结构-c++版-硬钢队列

数据结构-c++版-硬钢队列

队列可以用顺序表,也可以用链表.
最佳的方式:双向链表(因为队列是在队头和队尾频繁的进行操作,如果用双向链表=>O(1)复杂度)
(对于顺序表
=>如果用数组,顺序表的话
=>如果是队尾O(1)复杂度,
但是
队头
的话,对头出去后,后面的所有元素就要全部前一位,就是**O(n)**复杂度)

代码如下:
①队列的顺序存储:(用数组)
:本队列实现,在对头出队后,并没有将讲后面的所有元素全部前移一位,只是在队头出去后,把队头指针后移一位.(这样的话,会出现一种现象=>大学课本称之为"假溢出")

=>解决的方法:使用循环(数组)队列,或者循环(链表)队列,链表队列,双向链表队列.

#include <stdio.h>
#include <assert.h>
#include <Windows.h>
#include <iostream>
#include <iomanip> 
using namespace std;
#define MaxSize 5	//队列的最大容量 
typedef int DataType;	//队列中元素类型
typedef struct Queue
{
	DataType queue[MaxSize]; 
	int front;	//队头指针
	int rear;	//队尾指针
}SeqQueue;

//队列初始化,将队列初始化为空队列
void InitQueue(SeqQueue *SQ){
	if (!SQ) return;

	SQ->front = SQ->rear = 0;	//把对头和队尾指针同时置 0
}

//判断队列为空
int IsEmpty(SeqQueue* SQ){
	if (!SQ) return 0;

	if (SQ->front == SQ->rear)
	{
		return 1;
	}
	return 0;
}


//判断队列是否为满
int IsFull(SeqQueue* SQ){
	if (!SQ) return 0;

	if (SQ->rear == MaxSize)
	{
		return 1;
	}
	return 0;
}

//入队,将元素 data 插入到队列 SQ 中
int EnterQueue(SeqQueue* SQ, DataType data) {
	if (!SQ)
		return 0;

	if (IsFull(SQ)) {
		cout << "无法插入元素 " << data << ", 队列已满!" << endl;
		return 0;
	}

	SQ->queue[SQ->rear] = data; //在队尾插入元素data
	SQ->rear++; //队尾指针后移一位
	return 1;
}


//出队,将队列中队头的元素 data 出队,后面的元素向前移动
int DeleteQueue(SeqQueue* SQ, DataType *data){

if (!SQ || IsEmpty(SQ)) {
	cout << "队列为空!" << endl;
	return 0;
}

if (!data) 
return 0;

*data = SQ->queue[SQ->front];

for (int i = SQ->front + 1; i < SQ->rear; i++) {
	//移动后面的元素 
	SQ->queue[i-1]=SQ->queue[i];
}

SQ->rear--;//队尾指针前移一位 
return 1;
}

//出队,将队列中队头的元素 data 出队,出队后队头指针 front 后移一位
int DeleteQueue2(SeqQueue* SQ,DataType* data){
if (!SQ || IsEmpty(SQ))
{
	cout << "队列为空!" << endl;
	return 0;
}

if (SQ->front >= MaxSize) {
	cout << "队列已到尽头!" << endl; 
	return 0;
}

*data = SQ->queue[SQ->front];	//出队元素值
SQ->front = (SQ->front) + 1;	//队首指针后移一位
return 1;
}

//打印队列中的各元素
void PrintQueue(SeqQueue* SQ){
	if (!SQ) return;
	int i = SQ->front; 
	while (i < SQ->rear){
		cout << setw(4) << SQ->queue[i]; i++;
	}
	cout << endl;
}

//获取队首元素,不出队
int GetHead(SeqQueue* SQ, DataType* data)
{
	if (!SQ || IsEmpty(SQ))
	{
		cout << "队列为空!" << endl;
	}
	return *data = SQ->queue[SQ->front];
}

//清空队列
void ClearQueue(SeqQueue* SQ)
{
	if (!SQ) return;
	SQ->front = SQ->rear = 0;
}

//获取队列中元素的个数
int getLength(SeqQueue* SQ) {
	if (!SQ) return 0;

	return SQ->rear - SQ->front;
}

int main()
{
	SeqQueue* SQ = new SeqQueue;
	DataType data = -1;

	//初始化队列
	InitQueue(SQ);

	//入队
	for (int i = 0; i < 7; i++) {
		EnterQueue(SQ, i);
	}
	//打印队列中的元素
	printf("队列中的元素(总共%d 个):", getLength(SQ));
	PrintQueue(SQ);
	cout << endl;

	//出队
	for(int i=0; i<10; i++){ 
	if(DeleteQueue2(SQ, &data)){
	cout << "出队的元素是:" << data << endl;
}
else {
cout << "出队失败!" << endl;
}
}

//打印队列中的元素 
printf("出队一个元素后,队列中剩下的元素:"); 
PrintQueue(SQ);
cout << endl;


system("pause");
return 0;
}

在这里插入图片描述

②队列的链式存储:(用链表)

#include <stdio.h>
#include <assert.h>
#include <Windows.h>
#include <iostream>
#include <iomanip>
using namespace std;
#define MaxSize 5//队列的最大容量
typedef int DataType;//队列中元素类型

typedef struct _QNode {	//结点结构
	DataType data;
	struct _QNode* next;
}QNode;
typedef QNode* QueuePtr; 
typedef struct Queue
{
	int	length;//队列的长度
	QueuePtr front;//队头指针
	QueuePtr rear;//队尾指针
}LinkQueue;

//队列初始化,将队列初始化为空队列
void InitQueue(LinkQueue* LQ)
{
	if (!LQ) return;

	LQ->length = 0;
	LQ->front = LQ->rear = NULL;	//把对头和队尾指针同时置 0
}

//判断队列为空
int IsEmpty(LinkQueue* LQ)
{
	if (!LQ) return 0;

	if (LQ->front == NULL)
	{
		return 1;
	}
	return 0;
}


//判断队列是否为满
int IsFull(LinkQueue* LQ)
{
	if (!LQ) return 0;

	if (LQ->length == MaxSize)
	{
		return 1;
	}
	return 0;
}

//入队,将元素 data 插入到队列 LQ 中
int EnterQueue(LinkQueue* LQ, DataType data) {
	if (!LQ) return 0;

	if (IsFull(LQ)) {
		cout << "无法插入元素 " << data << ", 队列已满!" << endl; 
		return 0;
	}

	QNode* qNode = new QNode; 
	qNode->data = data;
	qNode->next = NULL;

	if (IsEmpty(LQ)) {//空队列
		LQ->front = LQ->rear = qNode;
	}
	else {
		LQ->rear->next = qNode;//在队尾插入节点qNode 
		LQ->rear = qNode;	//队尾指向新插入的节点
	}
	LQ->length++;

	return 1;
}


//出队,将队列中队头的元素出队,其后的第一个元素成为新的队首
int DeleteQueue(LinkQueue *LQ, DataType *data){
QNode* tmp = NULL;
if (!LQ || IsEmpty(LQ)) {
	cout << "队列为空!" << endl; return 0;
}

if (!data) return 0; tmp = LQ->front;

LQ->front = tmp->next;
if (!LQ->front) LQ->rear = NULL;//如果对头出列后不存在其他元素,则 rear 节点也要置空

*data = tmp->data; LQ->length--;

delete tmp;

return 1;
}



//打印队列中的各元素
void PrintQueue(LinkQueue* LQ)
{
	QueuePtr tmp; if (!LQ) return;
	if (LQ->front == NULL) {
		cout << "队列为空!"; return;
	}

	tmp = LQ->front; while (tmp)
	{
		cout << setw(4) << tmp->data; tmp = tmp->next;
	}
	cout << endl;
}

//获取队首元素,不出队
int GetHead(LinkQueue* LQ, DataType* data)
{
	if (!LQ || IsEmpty(LQ))
	{
		cout << "队列为空!" << endl; return 0;
	}

	if (!data) return 0;

	*data = LQ->front->data; return 1;
}

//清空队列
void ClearQueue(LinkQueue* LQ)
{
	if (!LQ) return;

	while (LQ->front) {
		QueuePtr tmp = LQ->front->next; delete LQ->front;
		LQ->front = tmp;
	}

	LQ->front = LQ->rear = NULL; LQ->length = 0;
}

//获取队列中元素的个数
int getLength(LinkQueue* LQ) {
	if (!LQ) return 0;

	return LQ->length;
}

int main()
{
	LinkQueue* LQ = new LinkQueue; 
	DataType data = -1;

	//初始化队列
	InitQueue(LQ);

	//入队
	for (int i = 0; i < 7; i++) {
		EnterQueue(LQ, i);
	}
	//打印队列中的元素
	printf("队列中的元素(总共%d 个):", getLength(LQ));
	PrintQueue(LQ); cout << endl;

	//出队
	//for(int i=0; i<10; i++){
	if(DeleteQueue(LQ, &data)){
	cout << "出队的元素是:" << data << endl;
}
else {
cout << "出队失败!" << endl;
}
//}

//打印队列中的元素
printf("出队一个元素后,队列中剩下的元素[%d]:", getLength(LQ)); PrintQueue(LQ);
cout << endl;

ClearQueue(LQ); 
cout << "清空队列!\n"; 
PrintQueue(LQ);


//清理资源
delete LQ;

system("pause");
return 0;
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

踏过山河,踏过海

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值