public void ReturnBalls(IBallQueue queue) { while (balls.Count > 0 || (hours && balls.Count > 1)) { queue.QueueBall(balls.Pop()); } }
static void Main(string[] args) { //定义三个队列:男嘉宾、女嘉宾、跳舞轮数(歌曲数目) IBallQueue mballQueue = null; IBallQueue fballQueue = null; IBallQueue lun = null; //实例化三个队列 mballQueue = new LinkBallQueue(); fballQueue = new LinkBallQueue(); lun = new LinkBallQueue(); Ball[] ball = new Ball[1]; Thread[] thread = new Thread[1]; ball[0] = new Ball(); ball[0].MballQ = mballQueue; ball[0].FballQ = fballQueue; ball[0].Lun = lun; //次线程指向Ball类里的Dance()方法(即执行Dance()方法) thread[0] = new Thread((new ThreadStart(ball[0].Dance))); thread[0].Start();//启用线程 while (true) { Console.WriteLine("请输入女舞者人数:"); string fballnumber = Console.ReadLine(); for (int i = 1; i <= Convert.ToInt32(fballnumber); i++)//女嘉宾进队列 { i = fballQueue.GetBallnumber(); fballQueue.EnQueue(i); } Console.WriteLine("请输入男舞者人数:"); string mballnumber = Console.ReadLine(); for (int i = 1; i <= Convert.ToInt32(mballnumber); i++)//男嘉宾进队列 { i = mballQueue.GetBallnumber(); mballQueue.EnQueue(i); } Console.WriteLine("请输入歌曲数目:"); string lunnumber = Console.ReadLine(); for (int i = 1; i <= Convert.ToInt32(lunnumber); i++)//歌曲数目进队列(队列的长度为歌曲数目) { i = lun.GetBallnumber(); lun.EnQueue(i); } break; } }
public BallStack(int capacity, IBallStack reciever, IBallQueue queue, bool hours = false) { this.capacity = capacity; if (hours) { capacity += 1; } this.reciever = reciever; this.queue = queue; this.balls = new Stack <Ball>(); this.hours = hours; if (hours) { balls.Push(new Ball(0) { Id = 0 }); } }