INTRODUCTION
Sequential lists are collection of objects that can be entered or extracted in a sequential way. They don’t provide access to objects in the middle on the list. You can only access a specific object each time you extract it. The two classes named “Queue” and ”Stack” which belong to the “System.Collections” namespace provide you with this kind of functionality, each of them in a slightly different way. The Queue class offers you access to the first object on the list, whereas the Stack class offers you access to the last object on the list. By first and last we mean the first and last object that entered the list. So, the Queue class is a First-in, First-out collection(FIFO Collection) whereas the Stack class is a Last-in, First-out(LIFO Collection). In this tutorial you will create and use both classes to see how they behave when entering and extracting data. You will usually work with such collections with temporary and disposable data. For a more permanent use, arrays provide much better functionality.
GRAPHICAL USER INTERFACE
The graphical user interface for our application is simple enough for beginners. Create a simple form with four textboxes and four buttons as they are depicted in picture 1.

Picture 1.
The two top textboxes are named textbox1 and textbox2 from left to right. Likewise, the two bottom textboxes are named textbox3 and textbox4 from left to right. Add the buttons accordingly and the two labels to distinguish the two operations.
CODE DEVELOPMENT
The first thing you should do is to create two public objects that represent the two classes. These two objects will be later used to enter and extract text objects from them. We could also add any other object but for this tutorial some simple text will suffice.
public Queue q = new Queue();
public Stack s = new Stack();
By pressing the button1 any text on the textbox1 will be entered in the Queue object. By pressing the button3 any text on the textBox2 will be added to the Stack object. Buttons 2 and 4 are used for the extraction of one text object at a time. It is imperative to check if there are any objects on the lists before trying to extract one of them or we risk triggering an exception. You could write the following code to add objects in the lists:
private void button1_Click(object sender, EventArgs e)
{
q.Enqueue(textBox1.Text);
textBox1.Text = "";
}
private void button3_Click(object sender, EventArgs e)
{
s.Push(textBox3.Text);
textBox3.Text = "";
}
We make use of the “Enqueue” and the “Push” methods for the two objects. Both methods add an item to the lists but they name the process differently. To extract objects one at a time we use the “Dequeue” and “Pop” methods respectively.
private void button2_Click(object sender, EventArgs e)
{
if (q.Count > 0)
textBox2.Text = q.Dequeue().ToString();
}
private void button4_Click(object sender, EventArgs e)
{
if (s.Count > 0)
textBox4.Text = s.Pop().ToString();
}
Remember that the “If” statement when proceeded by only one line of code doesn’t need to have braces ({}). So, with the method named “Count” we count the number of objects in the lists and if this number is greater than zero then we extract an object from the list. When extracting an object it is being removed from the list and the next one takes it place. It is not like an array were we can access array members again and again.
APPLICATION
Build the code and run it in debug mode. Add three or more objects to both lists by pressing the “Enqueue” and “Push” buttons. Then, retrieve the items one at a time from the sequential lists. See the difference?
Besides the three methods for each class that we saw here, there is also e method called “Peek”. The “Peek” method in both lists has the advantage that it can retrieve the next object in line without actually removing it. With this method you can test whether an object is an integer or a float for example and act accordingly. Try to replace the “Dequeue” and “Pop” with the “Peek” method and see what happens.
ITERATING THROUGH OBJECTS
Sequential lists are collections. This means that you can iterate through their objects with the use of foreach keyword. This technique does not remove the objects from the lists. Copy the following code to see the difference between the foreach keyword and the pop method:
static void Main(string[] args)
{
Stack numbers = new Stack();
numbers.Push("1");
numbers.Push("2");
numbers.Push("3");
numbers.Push("4");
Console.WriteLine("Iterating through objects");
foreach (string item in numbers)
{
Console.WriteLine(item);
}
Console.ReadLine();
Console.WriteLine("Pop the first item:");
Console.WriteLine(numbers.Pop().ToString());
Console.WriteLine("Second iteration of the objects");
foreach (string item in numbers)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
In the first iteration we iterate through and print on screen the strings contained in the collection. It does not matter that the collection is a Stack list. Then, we retrieve an item with the pop method and print it on the screen. The second iteration prints less objects than the first one. This is because the pop method removed the object from the collection, whereas the foreach loop did not.
Trackback(0)
 |