队列(C#)

using System;

//队列
class Queue
{
    private int maxSize;
    private long[] queArray;
    private int front;
    private int rear;
    private int nItems;

    public Queue(int s)     //constructor
    {
        maxSize = s;
        queArray = new long[maxSize];
        front = 0;
        rear = -1;
        nItems = 0;
    }

    public void Insert(long j)  //put item at rear of queue
    {
        if (rear == maxSize - 1)    //deal with wraparound
            rear = -1;              
        queArray[++rear] = j;       //increment rear and insert
        nItems++;                   //one more item
    }

    public long Remove()    //take item from front of queue
    { 
        long temp = queArray[front++];      //get value and incr front
        if (front == maxSize)               //deal with wraparound
            front = 0;
        nItems--;                           //one less item
        return temp;
    }

    public long PeekFront()     //peek at front of queue
    { 
        return queArray[front];
    }

    public bool IsEmpty()       //true if queue is empty
    { 
        return (nItems == 0);
    }

    public bool IsFull()        //true if queue if full
    {
        return (nItems == maxSize);
    }

    public int Size()           //number of items in queue 
    {
        return nItems;
    }
}//end class Queue

class QueueApp
{
    static void Main(string[] args)
    {
        Queue theQueue = new Queue(5);  //queue holds 5 items

        theQueue.Insert(10);            //insert 4 items
        theQueue.Insert(20);
        theQueue.Insert(30);
        theQueue.Insert(40);

        theQueue.Remove();      //remove 3 items
        theQueue.Remove();      //(10, 20, 30)
        theQueue.Remove();

        theQueue.Insert(50);    //insert 4 more items
        theQueue.Insert(60);    // (wraps around)
        theQueue.Insert(70);
        theQueue.Insert(80);

        while (!theQueue.IsEmpty()) //remove and display
        {                           //all items
            long n = theQueue.Remove(); //(40, 50, 60, 70, 80)
            Console.Write("{0}", n);
            Console.Write(" ");
        }
        Console.WriteLine();
        Console.ReadLine();
    } // end main
}   // end class QueueApp



using System;

//优先级对列
class PriorityQ
{ 
    //array in sorted order, from max at 0 to min at size-1
    private int maxSize;
    private long[] queArray;
    private int nItems;

    public PriorityQ(int s) //constructor
    {
        maxSize = s;
        queArray = new long[maxSize];
        nItems = 0;
    }

    public void Insert(long item)   //insert item
    {
        int j;

        if (nItems == 0)      // if no items,
            queArray[nItems++] = item;  // insert at 0
        else    // if items,
        {
            for (j = nItems - 1; j >= 0; j--)  //start at end,
            {
                if (item > queArray[j]) //if new item larger,
                    queArray[j + 1] = queArray[j];//shift upword
                else //if smaller,
                    break;  //done shifting
            } //end for
            queArray[j + 1] = item; //insert it
            nItems++;
        } //end else (nItems > 0)
    }   //end insert()

    public long Remove()    //remove minimumitem
    {
        return queArray[--nItems];
    }

    public long PeekMin()   //peek at minmumitem
    { 
        return queArray[nItems-1];
    }

    public bool IsEmpty() //true if queue is empty
    { 
        return (nItems == 0);
    }

    public bool IsFull()    //true if queue is full
    { 
        return (nItems == maxSize); 
    }
} // end class PriortyQ

class PriorityQApp
{
    static void Main(string[] args)
    {
        PriorityQ thePQ = new PriorityQ(5);
        thePQ.Insert(30);
        thePQ.Insert(50);
        thePQ.Insert(10);
        thePQ.Insert(40);
        thePQ.Insert(20);

        while (!thePQ.IsEmpty())
        {
            long item = thePQ.Remove();
            Console.Write("{0} ", item);
        }//end while
        Console.WriteLine();
        Console.ReadLine();
    }//end main
}//end class PrioritQApp

//用链表实现的队列(ADT)
using System;

class Link
{
    public long dData;  //data item
    public Link next;   //next link in list

    public Link(long d)     //constructor
    {
        dData = d;
    }

    public void DisplayLink()   //display this link
    {
        Console.Write("{0} ", dData);
    }
}//end class Link

class FirstLastList
{
    private Link first;     //ref to first item
    private Link last;      //ref to last item

    public FirstLastList()  //constructor
    {
        first = null;
        last = null;
    }

    public bool IsEmpty()   //true if no links
    {
        return first == null;
    }

    public void InsertLast(long dd) //insert at end of list
    {
        Link newLink = new Link(dd);    //make new link
        if (IsEmpty())              //if empty list
            first = newLink;        //first --> newLink
        else
            last.next = newLink;        //old last -->newLink
        last = newLink;             //newLink <-- last
    }

    public long DeleteFirst()   //delete first link
    {                           //(assume non-empty list)
        long temp = first.dData;
        if (first.next == null)  //if only one item
            last = null;        //null <-- last
        first = first.next;     //first --> old next
        return temp;
    }

    public void DisplayList()
    {
        Link current = first;   //start at beginning
        while (current != null)  //until end of list
        {
            current.DisplayLink();  //print data
            current = current.next; //move to next link
        }
        Console.WriteLine();
    }
}//end class FirstLastList


class LinkQueue
{
    private FirstLastList theList;

    public LinkQueue()  //constructor
    {
        theList = new FirstLastList();  //make a 2-ended list
    }

    public bool IsEmpty()   //true if queue is empty
    {
        return theList.IsEmpty();
    }

    public void Insert(long j)  //insert, rear of queue
    {
        theList.InsertLast(j);
    }

    public long Remove()    //remove, front of queue
    {
        return theList.DeleteFirst();
    }

    public void DisplayQueue()
    {
        Console.Write("Queue (front-->rear):");
        theList.DisplayList();
    }
}//end class LinkQueue

class LinkQueueApp
{
    public static void Main(string[] args)
    {
        LinkQueue theQueue = new LinkQueue();

        theQueue.Insert(20);    //insert items
        theQueue.Insert(40);

        theQueue.DisplayQueue();    //display queue

        theQueue.Insert(60);
        theQueue.Insert(80);

        theQueue.DisplayQueue();    //display queue

        theQueue.Remove();      //remove items
        theQueue.Remove();
        theQueue.DisplayQueue();    //display queue

        Console.ReadLine();
    }//end main
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值