Stack Class

Posted on Updated on

fgjtjrtjStack Class

It is a last-in, first out (LIFO) collection of object. It is used when the recent items entered are required first. When items are added in the list, it is called pushing and when removed, it is + called popping.

Methods and Properties of the Stack Class

Property                            Description

Count                                Gets the number of elements contained in the Stack.

 

Sr.No.                             Methods

     1                                             public virtual void Clear();

                                                    Removes all elements from the Stack.

     2                                             public virtual bool Contains(object obj);

                                                    Determines whether an element is in the Stack.

     3                                             public virtual object Peek();

                                                    Returns the object at the top of the Stack without removing it.

     4                                             public virtual object Pop();

                                                    Removes and returns the object at the top of the Stack.

     5                                             public virtual void Push(object obj);

                                                    Inserts an object at the top of the Stack.

     6                                             public virtual object[] ToArray();

                                                    Copies the Stack to a new array.

Example

using System;

using System.Collections;

namespace CollectionsApplication

{

class Program

{

static void Main(string[] args)

{

Stack st = new Stack();

st.Push(‘A’);

st.Push(‘M’);

st.Push(‘G’);

st.Push(‘W’);

Console.WriteLine(“Current stack: “);

foreach (char c in st)

{

Console.Write(c + ” “);

}

Console.WriteLine();

st.Push(‘V’);

st.Push(‘H’);

Console.WriteLine(“The next poppable value in stack: {0}”,

st.Peek());

Console.WriteLine(“Current stack: “);

foreach (char c in st)

{

Console.Write(c + ” “);

}

Console.WriteLine();

Console.WriteLine(“Removing values “);

st.Pop();

st.Pop();

st.Pop();

Console.WriteLine(“Current stack: “);

foreach (char c in st)

{

Console.Write(c + ” “);

}

}

}

}

*****************************************************************************************************

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.
Helpline: 9814666333, 8699444666
Email: info@technocampus.co.in

Please fill the form and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s