c#中queue的用法

Queue队列就是先进先出。它并没有实现 IList,ICollection。所以它不能按索引访问元素,不能使用Add和Remove。下面是 Queue的一些方法和属性

  Enqueue():在队列的末端添加元素

  Dequeue():在队列的头部读取和删除一个元素,注意,这里读取元素的同时也删除了这个元素。如果队列中不再有任何元素。就抛出异常

  Peek():在队列的头读取一个元素,但是不删除它

  Count:返回队列中的元素个数

  TrimExcess():重新设置队列的容量,因为调用Dequeue方法读取删除元素后不会重新设置队列的容量。

  Contains():确定某个元素是否在队列中

  CopyTo():把元素队列复制到一个已有的数组中

  ToArray():返回一个包含元素的新数组

做一个小例子来说明下队列的用法:

首先建立一个实体类

[Serializable]
    public class Person:IEquatable<Person>
    {
        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private string phone;
        public string Phone
        {
            get { return phone; }
            set { phone = value; }
        }

        private bool? isGet;

        public bool? IsGet
        {
            get { return isGet; }
            set { isGet = value; }
        }

        public Person() { }

        public Person(string name, string phone,bool? isGet)
        {
            this.name = name;
            this.phone = phone;
            this.isGet = isGet;
        }

        public bool Equals(Person person)
        {
            if (person == null)
            {
                return false;
            }
            if (this.name == person.name && this.phone == person.phone)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

然后建立一个queue的包装类

public class Manager
    {
        private Queue<Person> queue = new Queue<Person>();
        public void Add(Person p)
        {
            queue.Enqueue(p);
        }

        public Person Get()
        {
            return queue.Dequeue();
        }

        public bool IsGet(Person p)
        {
            bool resule = false;
            resule = queue.Contains(p);
            return resule;
        }

        public bool IsHaveElement()
        {
            if (queue.Count <= 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public int GetQueueCount()
        {
            return queue.Count;
        }
    }

剩下就是搞一个winform界面:

最后,就可以向队列里加东西了,每次显示的时候 都从队列里减一条

public partial class Form1 : Form
    {
        private Manager manager;
        public Form1()
        {
            manager=new Manager();
            InitializeComponent();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtPhone.Text.Trim()) || string.IsNullOrEmpty(txtPhone.Text.Trim()))
            {
                MessageBox.Show("Invalided Message");
            }
            else
            {
                string name = txtName.Text.Trim();
                string phone = txtPhone.Text.Trim();

                if (manager.IsGet(new Person(name, phone, null)))
                {
                    MessageBox.Show("This list have already in queue");
                }
                else
                {
                    manager.Add(new Person(name, phone, null));
                    txtName.Text = string.Empty;
                    txtPhone.Text = string.Empty;
                    tsLabel.Text ="Number : "+ manager.GetQueueCount().ToString();
                 //   MessageBox.Show("OK!");
                }
            }

        }

        private void btnView_Click(object sender, EventArgs e)
        {
            Person person = new Person();
            if(manager.IsHaveElement())
            {
                person = manager.Get();
                 ListViewItem li = new ListViewItem();
                 li.SubItems[0].Text = person.Name;
                 li.SubItems.Add(person.Phone);
                 listView.Items.Add(li);
                 tsLabel.Text = "Number : " + manager.GetQueueCount().ToString();
            }
            else
            {
                MessageBox.Show("No user");
            } 
        }
    }

可见以下运行结果,其中状态栏中的Number是指队列中元素的数量

转载:http://www.cnblogs.com/xuekai-to-sharp/p/3540709.html?utm_source=tuicool&utm_medium=referral

原文地址:https://www.cnblogs.com/larva/p/7300011.html