转:C#数据结构和算法学习系列七----队列、队列的实现和应用
队列是一种把数据从表的末端放入并在表的前端移除的数据结构。队列会按照数据项出现的顺序来存储它们。队列是先进先出(FIFO)数据结构的实例。队列用 来对提交给操作系统或打印池的任务进行排序,而模拟应用程序则用队列对用户等待队列进行模拟。队列包含两种主要的操作。一个是给队列添加新的数据项,另一 个则是把数据项从队列中移除。添加新数据项的操作被称为是Enqueue,而从队列中移除数据项的操作则被称为是Dequeue。Enqueue 操作会在队列的末尾添加一个数据项,而Dequeue 操作则会从队列的
前端(或开始处)移除一个数据项。
(1).Queue类。下面这个Queue 类的实现包含EnQueue 方法、DeQueue 方法、ClearQueue 方法(清除队列)、Peek 方法以及Count 方法,而且还有一个用于此类的默认构造器。
- public class CQueue
- {
- private ArrayList pqueue;
- public CQueue()
- {
- pqueue = new ArrayList();
- }
- public void EnQueue(object item)
- {
- pqueue.Add(item);
- }
- public void DeQueue()
- {
- pqueue.RemoveAt(0);
- }
- public object Peek()
- {
- return pqueue[0];
- }
- public void ClearQueue()
- {
- pqueue.Clear();
- }
- public int Count()
- {
- return pqueue.Count;
- }
- }
(2).Queue的应用
1.几种初始化方式
Queue myQueue = new Queue(100);
Queue myQueue = new Queue(32, 3);
Queue<int> numbers = new Queue<int>();
2.实例应用
- using System;
- using System.Collections;
- using System.IO ;
- namespace csqueue
- {
- public struct Dancer
- {
- public string name;
- public string sex;
- public void GetName(string n)
- {
- name = n;
- }
- public override string ToString()
- {
- return name;
- }
- }
- class Class1
- {
- static void newDancers(Queue male, Queue female)
- {
- Dancer m, w;
- m = new Dancer();
- w = new Dancer();
- if (male.Count > 0 && female.Count > 0)
- {
- m.GetName(male.Dequeue().ToString());
- w.GetName(female.Dequeue().ToString());
- }
- else if ((male.Count > 0) && (female.Count == 0))
- Console.WriteLine("Waiting on a female dancer.");
- else if ((female.Count > 0) && (male.Count == 0))
- Console.WriteLine("Waiting on a male dancer.");
- }
- static void headOfLine(Queue male, Queue female)
- {
- Dancer w, m;
- m = new Dancer();
- w = new Dancer();
- if (male.Count > 0)
- m.GetName(male.Peek().ToString());
- if (female.Count > 0)
- w.GetName(female.Peek().ToString());
- if (m.name != " " && w.name != "")
- Console.WriteLine("Next in line are: " + m.name + "\t" + w.name);
- else
- if (m.name != "") //!=
- Console.WriteLine("Next in line is: " + m.name);
- else
- Console.WriteLine("Next in line is: " + w.name);
- }
- static void startDancing(Queue male, Queue female)
- {
- Dancer m, w;
- m = new Dancer();
- w = new Dancer();
- Console.WriteLine("Dance partners are: ");
- Console.WriteLine();
- for (int count = 0; count <= 3; count++)
- {
- m.GetName(male.Dequeue().ToString());
- w.GetName(female.Dequeue().ToString());
- Console.WriteLine(w.name + "\t" + m.name);
- }
- }
- static void formLines(Queue male, Queue female)
- {
- Dancer d = new Dancer();
- StreamReader inFile;
- inFile = File.OpenText(@"c:\dancers.dat");
- string line;
- while (inFile.Peek() != -1)
- {
- line = inFile.ReadLine();
- d.sex = line.Substring(0, 1);
- d.name = line.Substring(2, line.Length - 2);
- if (d.sex == "M")
- male.Enqueue(d);
- else
- female.Enqueue(d);
- }
- }
- static void Main(string[] args)
- {
- Queue males = new Queue();
- Queue females = new Queue();
- formLines(males, females);
- startDancing(males, females);
- if (males.Count > 0 || females.Count > 0)
- headOfLine(males, females);
- newDancers(males, females);
- if (males.Count > 0 || females.Count > 0)
- headOfLine(males, females);
- newDancers(males, females);
- Console.Write(" press enter");
- Console.Read();
- }
- }