C#中的FIFO字符串分配

Pat*_*Lim 0 c# memory arrays allocation fifo

我目前正在创建一个我正在创建的程序的问题.我已经找到了答案,但它与我想要发生的事情有所不同,因为这里给出的是字符串.

我们被要求创建一个FIFO分配,这是程序作为控制台应用程序的预期流程:

输入号码.页面框架:2

输入号码.要插入的页面数:4

要插入的页面:A

插入帧1.中断生成.

要插入的页面:B

插入帧2.中断生成.

要插入的页面:A

插入失败.A是常驻页面.

要插入的页面:C

插入帧1.中断生成.

根据FIFO分配算法,如果插入新的不同页面,它将删除插入帧中的最早页面.如果页面已经在框架中,则页面插入将被拒绝.

我已经做了一个,虽然我目前陷入困境,试图弄清楚如何在数组中找到最早插入的元素.

我希望你能帮助我.我已经花了很多时间,但我只是不知道该怎么做.这是我的代码:

class Program
{
    static void Main(string[] args)
    {
        int f, p, interrupt;

        Console.WriteLine("Enter the number of frames: ");
        f = Int32.Parse(Console.ReadLine());
        string[] frame = new string[f];
        Console.WriteLine("Enter the number of pages: ");
        p = Int32.Parse(Console.ReadLine());

        for (int i = 0; i < p; i++) {


            Console.WriteLine("Page to be inserted: ");

            string x = Console.ReadLine();

            if (frame.Contains(x))
            {

                Console.WriteLine(x + " is a resident page.");


            }
            else {

                frame[i] = x;
                Console.WriteLine("Inserted in frame " + (i + 1) + ". Interrupt generated"));
                interrupt +=1;

            }
        }





        Console.ReadKey();
    }
}
Run Code Online (Sandbox Code Playgroud)

eva*_*nal 6

不要使用数组."先入先出"模型是一个队列http://msdn.microsoft.com/en-us/library/system.collections.queue.aspx

如果您使用队列,它将保留订单.您只能删除第一个项目.这个概念就是它像交通队列一样工作,前面的物体必须先行,然后才能移动.在排队之前,只需执行一个for each或LINQ查询以确保该项不是重复项.该Dequeue方法将始终删除添加到队列中的第一个项目.

 // for each example. For LINQ just use Where then check the length of the IEnumerable
 // if it 0 then item is unique.
 bool duplicate = false;
 foreach (string s in MyQueue)
 {
     if (s == inputString)
         duplicate = true;
 }

 if (!duplicate)
     MyQueue.Enqueue(inputString);



 // to get first item added simply do
 string firstIn = MyQueue.Dequeue();
Run Code Online (Sandbox Code Playgroud)