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 ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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 |
| Sponsored Links |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |