Stack and Queue
Written by: maffelu , 2009-09-28 20:40:50
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();
}
}
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
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();
}
}
There are no comments on this article.
If you have any question or just want to leave a message, just fill out the form below!
Your e-mail will not be visible in your post, it is for validation reasons only
Maffelu
Creator and admin of MorkaLork.com.
Started programming in HTML back when frames and tables was the way to design a page, moved on to Pascal/Delphi, PHP, javascript/jQuery, VB.NET/C#, Java and C++.
Currently studies .NET (in general) focusing on ASP.NET.