MorkaLork Development

Interesting stuff I've picked up over the years...

Stack and Queue

2009-09-28 20:40:50 | 414 views | Queue Stack csharp collection push pop enqueue dequeue

Queue<T> and Stack<T>



Queue<T>


The Queue collection uses a ”first-come, first-served” system meaning that whatever what put in first into the queue comes out of the queue first. A Queue<T> object does not have an index, it only allows objects to be taken out in the order they were entered.

The Queue<T> gives you two methods to work with in order to make it easy, Enqueue and Dequeue. To no ones surprise, Enqueue enters a value into the collection and Dequeue extracts it. When extracted a value is also removed from the collection meaning that the next value is now at the front of the queue.
Queue<T> also gives you the Peek method that lets you obtain the object in the front without removing it.

Example:



using System;

class Person {
public String Name { get; set; }
public int Age { get; set; }

public Person(String name, int age) {
this.Name = name;
this.Age = age;
}
}


class Program {
static void Main(string[] args) {

//Alot of patients enters...
Person patient1 = new Person("John Dole", 54);
Person patient2 = new Person("Doris Dale", 49);
Person patient3 = new Person("Günter Dyke", 16);
Person patient4 = new Person("Lisa Dingh", 28);

//Quick! Fix a waiting list
Queue<Person> waitingList = new Queue<Person>();

//Push all patients into the waiting list
waitingList.Enqueue(patient1);
waitingList.Enqueue(patient2);
waitingList.Enqueue(patient3);
waitingList.Enqueue(patient4);

//Right, now we will deal with them in an orderly fashion
//using a first come, first served system...
while (waitingList.Count > 0) {
Person p = waitingList.Dequeue();
Console.WriteLine("The doctor will now attend to {0}, {1}", p.Name, p.Age);
}

Console.Read();
}
}


This will output:

The doctor will now attend to John Dole, 54
The doctor will now attend to Doris Dale, 49
The doctor will now attend to Günter Dyke, 16
The doctor will now attend to Lisa Dingh, 28


As we can see, the patients were received in the order they came. Very good.


Stack<T>


The stack is the inverted Queue<T> (or vice versa). Instead of First come, First Served it uses the incredibly unfair First Come, Last Served logic. Whatever is put into the stack first, comes out last. Sort of like a real stack come to think of it.
If the two collections are used next to each other it becomes quite clear what the difference is:



class Program {

static void Main() {
Queue<int> myQueueNumbers = new Queue<int>();

myQueueNumbers.Enqueue(1); //1
myQueueNumbers.Enqueue(3); //1, 3
myQueueNumbers.Enqueue(51); //1, 3, 51
myQueueNumbers.Enqueue(199); //1, 3, 51, 199


Stack<int> myStackNumbers = new Stack<int>();

myStackNumbers.Push(1); //1
myStackNumbers.Push(3); //3, 1
myStackNumbers.Push(51); //51, 3, 1
myStackNumbers.Push(199); //199, 51, 3, 1

Console.WriteLine("Queue:");
foreach (int number in myQueueNumbers) {
Console.Write("{0} ", number);
}

Console.WriteLine();
Console.WriteLine();

Console.WriteLine("Stack:");
foreach (int number in myStackNumbers) {
Console.Write("{0} ", number);
}

Console.Read();
}
}


The Queue and Stack output




Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.