Queues are just like stacks, except that the objects collected by them are first in, first out (FIFO).
First, a Queue
object needs to be declared and instantiated:
Queue theQueue = new Queue();
Here's the code to enqueue a string
:
private void btnEnqueue_Click(object sender, System.EventArgs e) {
theQueue.Enqueue(txtIn.Text);
txtIn.Text = "";
lstQ.Items.Clear();
foreach (string s in theQueue) {
lstQ.Items.Add(s);
}
}
Dequeuing is pretty much the same thing, although (as you'd suspect) a check needs to be added to see that there is actually something on the queue:
private void btnDequeue_Click(object sender, System.EventArgs e) {
if (theQueue.Count > 0) {
txtOut.Text = (string) theQueue.Dequeue();
lstQ.Items.Clear();
foreach (string s in theQueue) {
lstQ.Items.Add(s);
}
}
else
MessageBox.Show("Nothing on the queue to dequeue!",
"No more waiting in line!", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.