C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Using Stack in C#


Posted Date: 21 Sep 2005    Resource Type: Articles    Category: .NET Framework
Author: VijayakumaranMember Level: Silver    
Rating: 1 out of 5Points: 5



Introduction



The Stack class implements a Last In First Out data structure. The objects are stored in such a manner that the object added last is placed at the top of the stack.

Stack Interfaces



The Stack class implements the three major collection interfaces. They are ICollection, IEnumerable and ICloneable.


Important Stack methods



Push - Used to add a new object at the top of the stack
Pop - Removes and returns the object at the top of the stack.
Peek – Returns the object at the top of the stack, but does not remove the item.
Clear – Deletes all the objects from the stack.
Contains – Returns true if a specified object is found in the stack else returns false.

The Stack stores the objects in an array as like the Queue.

An example is given below



{
static void Main(string[] args)
{
Stack vegStack = new Stack();

vegStack.Push("Raddish");
vegStack.Push("Cabbage");
vegStack.Push("Carrot");
vegStack.Push("Brinjal");
vegStack.Push("Onion");

Console.WriteLine("Contents of Vegetable Stack...");
foreach(string veg in vegStack)
{
Console.WriteLine(veg);
}

while(vegStack.Count > 0)
{
string veg = (string) vegStack.Pop();
Console.WriteLine("Popping {0}", veg );
}
}
}



When the code is executed, the output is as follows

Contents of Vegetable Stack...
Raddish
Cabbage
Carrot
Brinjal
Onion
Popping Raddish
Popping Cabbage
Popping Carrot
Popping Brinjal
Popping Onion


Summary



The .NET framework also includes a wide variety of collection types, including types that implements Queue and HashTable.







Responses

Author: venkateshwar rao gundlapally    22 Sep 2005Member Level: Bronze   Points : 0
Hi,
You done good job, but result shown is in reverse order. Pop always returns an item which was pushed last. Here Onion is the last item to push, so first outcome is Onion, etc.,


Author: meenakshi    29 Mar 2007Member Level: Silver   Points : 0
The output is not correct.Try to debug!!!


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Using Queue in C#
Previous Resource: Abstract Class Vs Interface
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use