Search results for 'Pop'. 1 post(s) found.

  1. 2007/08/27 Stack Tutorial In C#
2007/08/27 08:32

Stack Tutorial In C#


Stack is a LIFO (Last In First Out) queue, so if you add two items and then ask for an item you would get the second item then the first item. To add items to the Stack you use Push method. This method takes an object of any type but to keep things simple lets just give it a string object:

Stack s = new Stack();

s.Push("So");
s.Push("This");
s.Push("Is");
s.Push("How");
s.Push("Queues");
s.Push("Work");


 

Viewing a Single Object Without Removing it From the Stack
You can use the Peak method to get the bottom-most object in the Stack without removing it from the Stack:

Console.WriteLine(s.Peek());


 

Viewing All the Objects in the Stack Without Removing Them
You can read any of the data in the Stack by using an enumerator:

System.Collections.IEnumerator en = s.GetEnumerator();

while (en.MoveNext())
{
   Console.Write(en.Current   " ");
}


 

Removing Items from the Stack
To Pop an item from the Stack you can use the Pop statement. This returns the bottom-most object of the Stack.

while( s.Count > 0)
{
   Console.Write(s.Pop()   " ");
}

Trackback 5 Comment 0

Trackback : Cannot send a trackbact to this post.

  1. Subject By mail sale viagra.

    Tracked from Viagra studies women. 2009/05/02 17:29 delete

    Viagra. Cheapest viagra. Buying overnight viagra. Re viagra cello.

  2. Subject What are problems with prozac.

    Tracked from Prozac mood disorder. 2009/05/22 14:02 delete

    Prozac menopause. Prozac dosage. What are problems with prozac. Prozac.

  3. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 05:42 delete

    moneyideas

  4. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 14:22 delete

    moneyideas

  5. Subject different money making ideas

    Tracked from moneyideas 2010/01/31 16:44 delete

    moneyideas