Stack C# with Example



Stack C# with Example

// Initialize a stack object of integers 
var stack = new Stack(); 
// add some data 
stack.Push(3); 
stack.Push(5); 
stack.Push(8); 
// elements are stored with "first in, last out" order. 
// stack from top to bottom is: 8, 5, 3 
// We can use peek to see the top element of the stack. 
Console.WriteLine(stack.Peek()); // prints 8 
// Pop removes the top element of the stack and returns it. 
Console.WriteLine(stack.Pop()); // prints 8 
Console.WriteLine(stack.Pop()); // prints 5 
Console.WriteLine(stack.Pop()); // prints 3 

0 Comment's

Comment Form

Submit Comment