车厢调度问题(队列和栈的实现)

根据队列和栈的性质

队列找尾部最大的车厢号

栈找尾部最小的车厢号

栈、队列、数组可用STL或者自己实现

#pragma once
#include "vector.h"
#include "queue.h"
#include "stack.h"
#include 

const char* INPUTVECTORFILENAME = "carriagesRearrangement.txt";
const char* RESULTFILE = "carriagesResult.txt";

template
const int calculateK(Vector & v) {
	int k = 0;
	for (int i = 1; i < v.size(); ++i) {
		if (v[i] < v[i - 1]) {
			k++;
		}
	}
	return k;
}

void generateTestData(const char* fileName, const int &n) {
	std::ofstream fout(fileName);
	if (fout.is_open()) {
		int *a = new int[n];
		for (int i = 0; i < n; ++i) {
			a[i] = i + 1;
		}
		for (int i = 0; i < n; ++i) {
			std::swap(a[i], a[rand() % n]);
		}
		for (int i = 0; i < n; ++i) {
			fout << a[i] << ' ';
		}
		fout.close();
	}
}

template
void inputVectorFromFile(Vector & v, const char* fileName) {
	std::ifstream fin(fileName);
	if (fin.is_open()) {
		T temp;
		while (fin >> temp) {
			v.push_back(temp);
		}
		v.reverse();
		fin.close();
	}
}

Vector carriagesRearrangement_queue(Vector &carriages, const int k, const char* fileName) {
	std::ofstream fout(fileName);
	if (fout.is_open()) {
		if (k == 0) {
			return carriages;
		}
		Queue *buffer = new Queue[k]; // 缓冲车厢
		Vector arrangedCarriages; // 排序后的车厢
		arrangedCarriages.push_back(0); // 加入一个头节点
		// i和j用于循环
		int i = NULL, j = NULL;
		// n记录车厢总长
		int n = carriages.size();
		// temp记录第i节车厢
		int temp = NULL;
		// max和tag记录最大的车厢及其编号
		int max = NULL, tag = NULL;
		for (i = 0; i < n; ++i) {
			temp = carriages[i];
			// 如果当前车厢的编号等于排序后的车厢尾部编号的下一个,就直接加入到排序后车厢
			if (arrangedCarriages.back() + 1 == temp) {
				arrangedCarriages.push_back(temp);
				fout << "carriage " << temp << " move to the track from carriages\n";
				continue;
			}
			max = 0;
			tag = 0;
			/*
			 *如果不满足上面的条件
			 *在尾部车厢号最大的车厢后面插入
			 *否则找一个新的缓冲轨插入
			 */
			for (j = 0; j < k; ++j) {
				// 找到空的缓冲铁轨时判断
				if (buffer[j].empty() || k - 1 == j) {
					if (0 == max) {
						buffer[j].enQueue(temp);
						fout << "carriage " << temp << " move to the buffer " << j + 1 << " from carriages\n";
						break;
					}
					else {
						buffer[tag].enQueue(temp);
						fout << "carriage " << temp << " move to the buffer " << tag + 1 << " from carriages\n";
						break;
					}
				}
				// 用来找比当前车厢号大的最大车厢号
				if (buffer[j].back() < temp) {
					if (buffer[j].back() > max) {
						max = buffer[j].back();
						tag = j;
					}
				}
			}
		}
		bool correctTag = false;
		// 退队过程
		while (arrangedCarriages.size() <= n) {
			for (i = 0; i < k; ++i) {
				correctTag = false;
				if (!buffer[i].empty() && buffer[i].front() == arrangedCarriages.back() + 1) {
					arrangedCarriages.push_back(buffer[i].front());
					fout << "carriage " << buffer[i].front() << " move to the track from buffer " << i + 1 << '\n';
					buffer[i].deQueue();
					correctTag = true;
					break;
				}
			}
			if (!correctTag) {
				fout << "wrong!";
				return carriages;
			}
		}
		arrangedCarriages.erase(0); // 删除头节点
		fout.close();
		return arrangedCarriages.reverse();
	}
	return carriages;
}

Vector carriagesRearrangement_stack(Vector &carriages, const int k, const char* fileName) {
	std::ofstream fout(fileName);
	if (fout.is_open()) {
		if (k == 0) {
			return carriages;
		}
		Stack *buffer = new Stack[k]; // 缓冲车厢
		Vector arrangedCarriages; // 排序后的车厢
		arrangedCarriages.push_back(0); // 加入一个头节点
		// i和j用于循环
		int i = NULL, j = NULL;
		// n记录车厢总长
		int n = carriages.size();
		// temp记录第i节车厢
		int temp = NULL;
		// min和tag记录最小的车厢及其编号
		int min = NULL, tag = NULL;
		for (i = 0; i < n; ++i) {
			temp = carriages[i];
			// 如果当前车厢的编号等于排序后的车厢尾部编号的下一个,就直接加入到排序后车厢
			if (arrangedCarriages.back() + 1 == temp) {
				arrangedCarriages.push_back(temp);
				fout << "carriage " << temp << " move to the track from carriages\n";
				continue;
			}
			min = n + 1;
			tag = 0;
			/*
			 *如果不满足上面的条件
			 *在尾部车厢号最小的车厢后面压入
			 *否则找一个新的缓冲轨压入
			 */
			for (j = 0; j < k; ++j) {
				// 找到空的缓冲铁轨时判断
				if (buffer[j].empty() || k - 1 == j) {
					if (n + 1 == min) {
						buffer[j].push(temp);
						fout << "carriage " << temp << " move to the buffer " << j + 1 << " from carriages\n";
						break;
					}
					else {
						buffer[tag].push(temp);
						fout << "carriage " << temp << " move to the buffer " << tag + 1 << " from carriages\n";
						break;
					}
				}
				// 用来找比当前车厢号小的最小车厢号
				if (temp < buffer[j].top()) {
					if (buffer[j].top() < min) {
						min = buffer[j].top();
						tag = j;
					}
				}
			}
		}
		bool correctTag = false;
		// 出栈过程
		while (arrangedCarriages.size() <= n) {
			for (i = 0; i < k; ++i) {
				correctTag = false;
				if (!buffer[i].empty() && buffer[i].top() == arrangedCarriages.back() + 1) {
					arrangedCarriages.push_back(buffer[i].top());
					fout << "carriage " << buffer[i].top() << " move to the track from buffer " << i + 1 << '\n';
					buffer[i].pop();
					correctTag = true;
					break;
				}
			}
			if (!correctTag) {
				fout << "something wrong!";
				return carriages;
			}
		}
		arrangedCarriages.erase(0); // 删除头节点
		fout.close();
		return arrangedCarriages.reverse();
	}
	return carriages;
}

void carriagesTest() {
	Vector v;
	int n = 20;
	generateTestData(INPUTVECTORFILENAME, n);
	inputVectorFromFile(v, INPUTVECTORFILENAME);
	v = carriagesRearrangement_stack(v, calculateK(v), RESULTFILE);
}

 

你可能感兴趣的:(算法,c++,c++,队列,栈,stl)