⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 12_2_2.cpp

📁 王红梅编《数据结构》大多数的实验源码。内附详细的实验报告。
💻 CPP
字号:
#include <iostream.h>
#include "BufferTrack.h"

void main()
{
	// preparation
	int k;
	int n = 0;
	int id;
	int i;
	int queue[QUEUE_SIZE];

	cout << "请输入车厢编码,输入-1停止输入:" << endl;
	i = 0;
	while (id != -1 && i < QUEUE_SIZE - 1)
	{
		cin >> id;
		queue[i] = id;
		if (id != -1)
		{
			i++;
			n++;
		}
	}
	queue[n] = 0;

	cout << "请输入缓冲轨道数量k:";
	cin >> k;

	// 生成缓冲轨
	BufferTrack *bufferTrack[K];
	for (i = 0; i <= k; i++)
	{
		bufferTrack[i] = new BufferTrack;
	}

	// main program
	int nowOut = 1;
	int p = 0;
	int j;
	while (nowOut <= n)
	{
		if (queue[p] == nowOut)
		{
			cout << queue[p] << endl;
			nowOut++;
			if (queue[p] != 0)
				p++;
		}
		else
		{
			bool hasOneToBeOut = false;
			for (j = 0; j < k; j++)
			{
				if (bufferTrack[j]->GetHead() == nowOut)
				{
					cout << bufferTrack[j]->DeQueue() << endl;
					hasOneToBeOut = true;
					nowOut++;
				}
			}
			if (!hasOneToBeOut && queue[p] != 0)
			{
				int maxBufferTrack = k;
				for (j = 0; j < k; j++)
				{
					if (bufferTrack[j]->GetLast() >= bufferTrack[maxBufferTrack]->GetLast() &&
						bufferTrack[j]->GetLast() < queue[p])
						maxBufferTrack = j;
				}
				if (maxBufferTrack == k)
					maxBufferTrack = 0;
				bufferTrack[maxBufferTrack]->EnQueue(queue[p]);
				p++;
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -