namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
const int MAX_QUEUE = 12;
Queue myQ = new Queue(MAX_QUEUE);
private void btEnQ_Click(object sender, EventArgs e)
{
if (myQ.Count < MAX_QUEUE)
{
Random rndObj = new Random();
myQ.Enqueue(rndObj.Next(10));
textBox1.Text = "";
foreach (int i in myQ)
{
textBox1.Text += i.ToString() + "\r\n";
}
}
else MessageBox.Show("ใส่ข้อมูลเพิ่มไม่ได้ เพราะQueue เต็ม", "ผิดพลาดครับ");
}
private void btDeQ_Click(object sender, EventArgs e)
{
if (myQ.Count > 0)
{
int iOut = (int)myQ.Dequeue();
MessageBox.Show("ข้อมูลที่นำออกจาก Queue คือ " + iOut.ToString(), "Dequeue");
textBox1.Text = "";
foreach (int i in myQ)
{
textBox1.Text += i.ToString() + "\r\n";
}
}
else MessageBox.Show("นำข้อมูลออกมาไม่ได้ เพราะ Queue ว่าง", "ผิดพลาดครับ");
}
private void btPeek_Click(object sender, EventArgs e)
{
if (myQ.Count > 0)
{
MessageBox.Show("ข้อมูลตัวแรกของ Queue คือ " + myQ.Peek().ToString(), "เมธอด Peek");
}
else MessageBox.Show("ไม่มีข้อมูลตัวแรกสุด เพราะ Queue ว่าง", "ผิดพลาดครับ");
}
private void btCount_Click(object sender, EventArgs e)
{
MessageBox.Show("มีข้อมูลใน Queue ทั้งหมด " + myQ.Count.ToString() +
" ตัว จากความจุ Queue " + MAX_QUEUE.ToString(), "พร็อพเพอร์ตี้ Count");
}
private void btClear_Click(object sender, EventArgs e)
{
myQ.Clear();
textBox1.Text = "";
MessageBox.Show("Queue ถูกเคลียร์แล้ว", "เมธอด Clear");
}
}
}
Sign up here with your email
ConversionConversion EmoticonEmoticon