用C++进行队列的设置——两种

最近刚刚学习了队列的使用,在这里用来两种设置队列的方法,主要是判断的条件有所区别。

在每个方法中都构造了创建,判满,加入,判空,取出,打印五种函数。

一:利用一个变量保存大小

#include<iostream>
using namespace std;
typedef struct Queue
{
	int* Data;
	int Front;
	int Rear;
	int Maxsize;
	int Capacity;
};
Queue* Create(int max)
{
	Queue* queue;
	queue = new Queue();
	queue->Maxsize = max;
	queue->Data = new int[max];
	if (queue->Data == NULL)
	{
		cout << "内存出错" << endl;
		exit(-1);
	}
	else
	{
		queue->Front = 0;
		queue->Rear = 0;
		queue->Capacity = 0;
		return queue;
	}
}
bool IsFull(Queue* queue)
{
	if (queue->Capacity == queue->Maxsize)
	{
		return true;
	}
	return false;
}
void Enqueue(Queue* queue, int data)
{
	if (IsFull(queue))
	{
		cout << "队列已满" << endl;
	}
	else
	{
		if (queue->Capacity == 0)
		{
			queue->Data[queue->Rear] = data;
		}
		else
		{
			queue->Rear = (++queue->Rear) % (queue->Maxsize);
			queue->Data[queue->Rear] = data;
		}
		queue->Capacity++;
	}
}
bool IsEmpty(Queue* queue)
{
	if (queue->Capacity == 0)
	{
		return true;
	}
	return false;
}
void Outqueue(Queue* queue,int* data)
{
	if (IsEmpty(queue))
	{
		cout << "队列已空" << endl;
	}
	else
	{
		*data = queue->Data[queue->Front];
		queue->Front = (++queue->Front) % (queue->Maxsize);
		queue->Capacity--;
	}
}
void PrintQueue(Queue* queue)
{
	if (IsEmpty(queue))
	{
		cout << "队列已空" << endl;
	}
	else
	{
		cout << "the capacity of queue is " << queue->Capacity << endl;
		while (1)
		{
			if (IsEmpty(queue))
			{
				break;
			}
			else
			{
				cout << queue->Data[queue->Front] << ' ';
				queue->Front = (++queue->Front) % (queue->Maxsize);
				queue->Capacity--;
			}
		}
	}
}
int main()
{
	int n;
	cin >> n;
	Queue* queue = Create(n);
	while (1)
	{
		if (IsFull(queue))
		{
			break;
		}
		else
		{
			int temp;
			cin >> temp;
			Enqueue(queue, temp);
		}
	}
	PrintQueue(queue);
	system("pause");
	return 0;
}

二、利用头和尾的位置进行判断

#include<iostream>
using namespace std;
typedef struct Queue
{
	int* Data;
	int Front;
	int Rear;
	int Maxsize;
};
Queue* Create(int max)
{
	Queue* queue;
	queue = new Queue();
	queue->Maxsize = max;
	queue->Data = new int[max];
	if (queue->Data == NULL)
	{
		cout << "内存出错" << endl;
		exit(-1);
	}
	else
	{
		queue->Front = 0;
		queue->Rear = 0;
		return queue;
	}
}
bool IsFull(Queue* queue)
{
	int temp = (queue->Rear + 1) % (queue->Maxsize);
	if (temp==queue->Front)
	{
		return true;
	}
	return false;
}
void Enqueue(Queue* queue, int data)
{
	if (IsFull(queue))
	{
		cout << "队列已满" << endl;
	}
	else
	{
		queue->Rear = (queue->Rear + 1) % (queue->Maxsize);
		queue->Data[queue->Rear] = data;
	}
}
bool IsEmpty(Queue* queue)
{
	if (queue->Front==queue->Rear)
	{
		return true;
	}
	return false;
}
void Outqueue(Queue* queue,int* data)
{
	if (IsEmpty(queue))
	{
		cout << "队列已空" << endl;
	}
	else
	{
		queue->Front = (++queue->Front) % (queue->Maxsize);
		*data = queue->Data[queue->Front];
	}
}
void PrintQueue(Queue* queue)
{
	if (IsEmpty(queue))
	{
		cout << "队列已空" << endl;
	}
	else
	{
		int temp = ((queue->Rear) - (queue->Front) + queue->Maxsize) % (queue->Maxsize);
		cout << "the capacity of queue is " << temp << endl;
		while (1)
		{
			if (IsEmpty(queue))
			{
				break;
			}
			else
			{
				queue->Front = (++queue->Front) % (queue->Maxsize);
				cout << queue->Data[queue->Front] << ' ';
			}
		}
	}
}
int main()
{
	int n;
	cin >> n;
	Queue* queue = Create(n);
	while (1)
	{
		if (IsFull(queue))
		{
			break;
		}
		else
		{
			int temp;
			cin >> temp;
			Enqueue(queue, temp);
		}
	}
	PrintQueue(queue);
	system("pause");
	return 0;
}

希望对大家有所帮助

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
哈夫曼算法是一种经典的无损数据压缩算法,它通过将频率较高的字符用较短的编码表示,而频率较低的字符用较长的编码表示,从而实现文件的压缩。 首先,对于要压缩的文件,我们需要统计各个字符的出现频率。然后,根据字符频率构建哈夫曼树。构建过程中,我们选择频率最低的两个节点,合并为一个新的节点,并调整树的结构,直到所有节点都合并为一个根节点。 接下来,我们通过遍历哈夫曼树,给每个字符生成对应的编码。规定向左走为0,向右走为1,当遍历到叶子节点时,就得到了字符的编码。 在压缩文件时,我们将每个字符的编码按顺序写入输出文件中。同时,为了解压缩方便,我们需要在文件开头添加字符与编码的映射表,用于解压缩时查找。 解压缩文件时,我们根据映射表将编码逐个翻译为字符,恢复出原始的文件内容。 哈夫曼算法通过合理地利用字符的频率信息,将出现频率高的字符用较短的编码表示,从而实现了对文件的压缩。经典的例子是英文文本,其中出现频率较高的字符如空格、换行符等可以用极短的编码表示,而出现频率较低的字符如字母J、Z等则可以用更长的编码表示。 用哈夫曼算法进行文件压缩解压缩可以有效地减小文件的存储空间,提高文件传输效率,并且无损地还原出原始文件内容。因此,哈夫曼算法在实际应用中被广泛使用,如在文件传输、音频、视频等领域中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值