【请教】用顺序栈解决火车车厢重排问题的编程中缺少using指令的错误
C#运行程序时错误(找不到类型或命名空间名称“LinkStack”(是否缺少using指令或程序集引用))
用顺序栈解决火车车厢重排问题的编程中,运行时有个错误,说明写道:(找不到类型或命名空间名称“LinkStack”(是否缺少using指令或程序集引用))
应该是缺少using指令吧?但是不知道应该用什么指令,希望高手们帮帮忙~~
源代码:
using System;
using System.Collections.Generic;
using System.Text;
namespace 实验三
{
class Program
{
static void Main(string[] args)
{
int[] p = new int[] { 3, 6, 9, 2, 4, 7, 1, 8, 5 };
int k = 3;
TrainArrangeByLinkStack ta = new TrainArrangeByLinkStack();
bool result;
result = ta.Railroad(p, p.Length, k);
do
{
if (result == false)
{
Console.Write("need more holding track,please enter additional number:");
k = k + Convert.ToInt32(Console.ReadLine());
result = ta.Railroad(p, p.Length, k);
}
}
while (result == false);
Console.ReadLine();
}
class TrainArrangeByLinkStack
{
//k个缓冲铁轨,车厢初始排序存储在p中
bool Railroad(int[] p, int n, int k)
{
//如果重排成功,返回true,否则返回false
//创建与缓冲铁轨对应的堆栈
LinkStack<int>[] H;
H = new LinkStack<int>[k + 1];
for (int i = 1; i <= k; i++)
H[i] = new LinkStack<int>();
int NowOut = 1;//下一次要输出的车厢
int minH = n + 1;//缓冲铁轨中编号最小的车厢
int minS = 0;//minH号车厢对应的缓冲铁轨
//车厢重排
for (int i = 1; i < n; i++)
if (p[i] == NowOut)
{
ConSole.WriteLine("Move Car{0}from input to output", p[i]);
NowOut++;
//从缓冲铁轨中输出
while (minH == nowOut)
{
Output(ref minH, ref minS, ref H, k, n);
NowOut++;
}
}
else
{//将p[i]送入某个缓冲铁轨
if (!Hold(p[i], ref minH, ref minS, ref H, k, n))
return false;
}
return true;
}
//把车厢从缓冲铁轨送至出轨处,同时修改minS和minH
void Output(ref int minH, ref int minS, ref LinkStack<int>[] H, int k, int n)
{
int c; //从堆栈minS中删除编号最小的车厢minH
c = H[minS].Pop();
Console.WriteLine("Move Car{0}from holding track {1} to output", minH, minS);
minH = n + 2;
for (int i = 1; i <= k; i++)
if (!H[i].IsEmpty() && (c = H[i].Top.Data) < minH)
{
minH = c;
minS = i;
}
}
//在一个缓冲铁轨中放入车厢c
bool Hold(int c, ref int minH, ref int minS, ref LinkStack<int>[] H, int k, int n)
{
//如果没有可用的缓冲铁轨,则返回false
//否则返回true
//为车厢c寻找最优的换成铁轨
//初始化
int BestTrack = 0;//目前最优的铁轨
int BestTop = n + 1;//最优铁轨上的头辆车厢
int x;//车厢索引
//扫描缓冲铁轨
for (int i = 1; i <= k; i++)
if (!H[i].IsEmpty())
{//铁轨i顶部的车厢编号最小
x = H[i].Top.Data;
if (c < x && x < BestTop)
{
BestTop = x;
BestTrack = i;
}
}
else //铁轨i为空
{
if (BestTrack == 0) bestTrack = i;
break;
}
if (BestTrack == 0)
return false; //没有可用的铁轨
//把车厢c送入缓冲铁轨
H[BestTrack].Push(c);
Console.WriteLine("Move Car{0}from input to holding track{1}", c, BestTrack);
//必要时修改min和HminS
if (c < minH)
{
minH = c; minS = BestTrack;
}
return true;
}
//调用火车车厢重排算法Railroad()重排车厢
}
}
}
[ 本帖最后由 ℡﹏聽雨" 于 2011-4-14 18:30 编辑 ]