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 »

Stack: A First-In, Last-Out List.


Posted Date: 26 Feb 2004    Resource Type: Articles    Category: .NET Framework
Author: ANITA MARY JOSEPHMember Level: Gold    
Rating: 1 out of 5Points: 10



Here’s how a Stack works, imagine a stack of Books on a table. The first Book put down is the last one to be picked up.

Let’s get to the coding part,
To put an object on the top of the stack, call Push(). To remove and return the top element, call Pop(). An InvalidOperationException is thrown if you call Pop() when the invoking stack is empty. You can use Peek() to return, but not remove the top object.

Here is an example that creates a stack, pushes several Integer objects onto it, and then pops them off again.

//Demonstrate the Stack class.

using System;
using System.Collections;

class StackDemo
{
static void ShowPush(Stack stack, int number)
{
stack.Push(number);
Console.WriteLine("Push(" + number + ")");
Console.Write("stack: ");
foreach(int i in stack)
Console.Write(i + " ");
Console.WriteLine();
}
static void ShowPop(Stack stack)
{
Console.Write("Pop -> ");
int number = (int) stack.Pop();
Console.WriteLine(number);
Console.Write("stack: ");
foreach(int i in stack)
Console.Write(i + " ");
Console.WriteLine();
}
public static void Main()
{
Stack stack = new Stack();

foreach(int i in stack)
Console.Write(i + " ");
Console.WriteLine();

ShowPush(stack,2);
ShowPush(stack,4);
ShowPush(stack,77);
ShowPop(stack);
ShowPop(stack);
ShowPop(stack);

try
{
ShowPop(stack);
}
catch(InvalidOperationException)
{
Console.WriteLine("Stack empty.");
Console.Read();
}
}
}

Here’s the putput produced by the program. Notice how the exception handler for InvalidOperationException manages a stack underflow.

Push(2)
stack: 2
Push(4)
stack: 4 2
Push(77)
stack: 77 4 2
Pop -> 77
stack: 4 2
Pop -> 4
stack: 2
Pop ->2
stack:
Pop -> Stack empty.




Responses

Author: Emmanuel Mathew    13 Jun 2004Member Level: Silver   Points : 0
Could you tell me how the peek works? Does it gets the valuie of the element without poping up?

Thanks in advance
Bye


Author: ANITA MARY JOSEPH    14 Jun 2004Member Level: Gold   Points : 0
Yes Mr.Emmanuel, Thats Exactly what Peek() does, it just returns the the top most element without popping it off.
- Anita Joseph


Author: Emmanuel Mathew    14 Jun 2004Member Level: Silver   Points : 0
One more question please...

Are we using collections because stack is included in that namespace?


Author: ANITA MARY JOSEPH    14 Jun 2004Member Level: Gold   Points : 0
There are built-in collections that support stacks, queues,hash tables.......hence the System.Collections Namespace should be included!


Author: critic    16 Jun 2004Member Level: Bronze   Points : 0
There are more than one .NET class mentioned in this article, but none is mentioned in the ".NET classes" section !


Author: ANITA MARY JOSEPH    16 Jun 2004Member Level: Gold   Points : 0
Dear Sir,
About your feedback, Well i've already mentioned the .Net class name in its respective field, but to my surprise it never displays in the Article page.
Sir could you please tell me which are the 'more than one .NET class' you are talking about, i can find only one .Net class and that's "System.Collections.Stack".
please correct me if i'm wrong in this matter
Thank You Very Much,
- Anita Joseph.



Author: Tony John    16 Jun 2004Member Level: Diamond   Points : 0
I could find 3 classes -
1. Stack
2. Console
3. InvalidOperationException

Also... if you edit the article, it will take upto 1 hr to reflect in the site.... thats why you may not have seen yoru changes.

Also, due to a bug in the dotnetspider.com, the last entry in the 'classes' list is not displayed, unless you press an 'Enter' at the end of last line in the list of classes. So, if you have entered 3 classes, press enter after the 3rd entry.. so that the last oen also will be displayed.
The spider team has to fix this bug.


Author: ANITA MARY JOSEPH    17 Jun 2004Member Level: Gold   Points : 0
Dear Critic,
I have made the necessary corrections
: - )
Thank You,
Anita Joseph.



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: How to generate Random number
Previous Resource: Queue: A First-In, First-Out List
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use