Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Stack class in C#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 70
    Comment on it

    It represent last in first out type of hierarchy .

     

    It is used when you want to access elements in the LIFO form.

     

    When you add item in the list it is known as pushing the item into the list.

     

    When you delete the item in the list it is know as poping the item from the list.

     

     

     

    Properties of Stack class :

    Property Description
    Count Gets the number of elements contained in the Stack.

     

    Methods of Stack class:

    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.

     

    We can insert and delete elements according to our need but the access of elements is on the LIFO principle.

     

    class Program
       {
          static void Main(string[] args)
          {
             Stack st = new Stack();
             
             st.Push('Himanshu');
             st.Push('Dheeraj');
             st.Push('Gaurav');
             st.Push('Mayank');
             
             Console.WriteLine("Current stack: ");
             foreach (char c in st)
             {
                Console.Write(c + " ");
             }
             
             Console.WriteLine();
             
             st.Push('Vinod');
             st.Push('Manoj');
             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 + " ");
             }
          }
       }

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: