IT Community - Software Programming, Web Development and Technical Support

Using Stack in C#

This is a discussion on Using Stack in C# within the C# Programming forums, part of the Software Development category; Stack is a LIFO (Last In First Out) queue or FILO (First In Last Out), so if you add two ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 12-26-2007, 06:06 AM
SaravananJ SaravananJ is offline
D-Web Programmer
 
Join Date: Aug 2007
Posts: 79
SaravananJ is on a distinguished road
Thumbs up Using Stack in C#

Stack is a LIFO (Last In First Out) queue or FILO (First In Last Out), so if you add two items and then ask for an item you would get the second item then the first item.

Adding Item in to Stack:

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("Am");
s.Push("Inserting");
s.Push("Value");
s.Push("Inside");
s.Push("Stack");
s.Push("List");

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:

Response.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())
{
Response.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)
{
Response.Write(s.Pop() +" ");
}
__________________
J.Saravanan
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-12-2008, 01:12 AM
Anandavinayagam Anandavinayagam is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 131
Anandavinayagam is on a distinguished road
Default Re: Using Stack in C#

i want to store Bitmap object in stack..is this possible...
__________________
The MOSS
Master of Solution Service
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How would you implement a queue from a stack? sundarraja C and C++ Programming 1 07-31-2007 03:49 AM


All times are GMT -7. The time now is 04:51 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0