国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

使用C#實現順序隊列

2023-05-16 12:36:01
字體:
來源:轉載
供稿:網友

隊列(Queue)是插入操作限定在表的尾部而其它操作限定在表的頭部進行的線性表。把進行插入操作的表尾稱為隊尾(Rear),把進行其它操作的頭部稱為隊頭(Front)。當對列中沒有數據元素時稱為空對列(Empty Queue)。

隊列通常記為:Q= (a1,a2,…,an),a1為隊頭元素,an為隊尾元素。元素按照a1,a2,…,an的次序依次入隊,出隊的次序與入隊相同,即a1第一個出隊,an最后一個出隊。所以,對列的操作是按照先進先出(First In First Out)或后進后出( Last In Last Out)的原則進行的,因此,隊列又稱為FIFO表或LILO表。

隊列的常用操作有:

1、構造一個空隊列:InitQueue()//在C#中可以使用構造函數來實現

2、清空隊列:ClearQueue()

3、判斷隊列是否為空:IsEmpty()

4、判斷隊列是否已滿:IsFull()

5、求隊列長度:QueueLength()

6、入隊操作:In()

7、出隊操作:Out()

8、得到隊頭元素:GetHead()

下面給出一個實現順序棧的源代碼:

using System;

 

class Queue

{

    object[] data;  //數據元素

    int maxsize;    //最大容量

    int front;      //指向隊頭

    int rear;       //指向隊尾

 

    //初始化隊列

    public Queue(int size)

    {

        this.maxsize = size;

        data = new object[size];

        front = rear = -1;

    }

    //最大容量屬性

    public int MaxSize

    {

        get

        {

            return this.maxsize;

        }

        set

        {

            this.maxsize = value;

        }

    }

    //隊尾屬性

    public int Rear

    {

        get

        {

            return this.rear;

        }

    }

    //隊頭屬性

    public int Front

    {

        get

        {

            return this.front;

        }

    }

    //數據屬性

    public object this[int index]

    {

        get

        {

            return data[index];

        }

    }

    //獲得隊列的長度

    public int GetQueueLength()

    {

        return rear-front;

    }

    //判斷隊列是否滿

    public bool IsFull()

    {

        if(GetQueueLength() == maxsize)

            return true;

        else

            return false;

    }

    //判斷隊列是否為空

    public bool IsEmpty()

    {

        if(rear==front)

            return true;

        else

            return false;

    }

    //清空隊列

    public void ClearQueue()

    {

        rear = front = -1;

    }

    //入隊

    public void In(object e)

    {

        if(IsFull())

        {

            Console.WriteLine("隊列已滿!");

            return;

        }

        data[++rear]=e;

    }

    //出隊

    public object Out()

    {

        if(IsEmpty())

        {

            Console.WriteLine("隊列為空!");

            return null;

        }

        if(rear-front>0)

        {

            object tmp = data[++front];

            return tmp;

        }

        else

        {

            Console.WriteLine("全出隊了!");

            ClearQueue();

            return null;

        }

    }

    //獲得隊頭元素

    public object GetHead()

    {

        if(IsEmpty())

        {

            Console.WriteLine("隊列為空!");

            return null;

        }

        return data[front+1];

    }

}

class Test

{

    static void Main()

    {

        Queue q = new Queue(1);

        int rdNum;

        Random rd = new Random();

        while(!q.IsFull())

        {

            rdNum = rd.Next(10,100);

            q.In(rdNum);

            Console.WriteLine("{0}入隊,隊列元素個數:{1}",rdNum,q.GetQueueLength());

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 股票| 乌审旗| 抚宁县| 抚州市| 长兴县| 定陶县| 新竹县| 绥德县| 文昌市| 玉龙| 高密市| 马公市| 台北市| 长葛市| 崇信县| 报价| 阿合奇县| 武汉市| 西丰县| 绥棱县| 黑山县| 新平| 白银市| 湟源县| 鱼台县| 四平市| 水城县| 达尔| 贵南县| 武城县| 葵青区| 喀喇沁旗| 天等县| 深泽县| 蒲城县| 手游| 亚东县| 罗甸县| 新昌县| 隆回县| 东至县|