A Stack is used to represent a last-in, first out collection of objects. It is used when you need last-in, first-out access to items. It is of both generic and non-generic type of collection. The generic stack is defined in System.Collections.Generic namespace whereas non-generic stack is defined under System.Collections namespace, here we will discuss non-generic type stack. A stack is used to create a dynamic collection which grows, according to the need of your program. In a stack, you can store elements of the same type or different types.
The below diagram illustrates the Stack class hierarchy:
- The Stack class implements the IEnumerable, ICollection, and ICloneable interfaces.
- When you add an item in the list, it is called pushing the element.
- When you remove it, it is called popping the element.
- The capacity of a Stack is the number of elements the Stack can hold. As elements are added to a Stack, the capacity is automatically increased as required through reallocation.
- In Stack, you are allowed to store duplicate elements.
- A Stack accepts null as a valid value for reference types.
How to create a Stack?
Stack class has three constructors which are used to create a stack which is as follows:
- Stack(): This constructor is used to create an instance of the Stack class which is empty and having the default initial capacity.
- Stack(ICollection): This constructor is used to create an instance of the Stack class which contains elements copied from the specified collection, and has the same initial capacity as the number of elements copied.
- Stack(Int32): This constructor is used to create an instance of the Stack class which is empty and having specified initial capacity or the default initial capacity, whichever is greater.
Let’s see how to create an stack using Stack() constructor:
Step 1: Include System.Collections namespace in your program with the help of using keyword.
Step 2: Create a stack using Stack class as shown below:
Stack stack_name = new Stack();
Step 3: If you want to add elements in your stack, then use Push() method to add elements in your stack. As shown in the below example.
490.98 1234 G geeksforgeeks Geeks
How to remove elements from the Stack?
In Stack, you are allowed to remove elements from the stack. The Stack class provides two different methods to remove elements and the methods are:
- Clear: This method is used to remove all the objects from the stack.
- Pop: This method removes the beginning element of the stack.
Total elements present in my_stack: 4 Total elements present in my_stack: 3 Total elements present in my_stack: 0
How to get topmost element of the Stack?
In Stack, you can easily find the topmost element of the stack by using the following methods provided by the Stack class:
- Pop: This method returns the object at the beginning of the stack with modification means this method remove the topmost element of the stack.
- Peek: This method returns the object at the beginning of the stack without removing it.
Total elements present in my_stack: 4 Topmost element of my_stack is: GeeksforGeeks Total elements present in my_stack: 3 Topmost element of my_stack is: geeks23 Total elements present in my_stack: 3
How to check the availability of elements in the stack?
In a stack, you can check whether the given element is present or not using Contain() method. Or in other words, if you want to search an element in the given stack use Contains() method. This method returns true if the element present in the stack. Otherwise, return false.
Element is found...!!
Generic Queue Vs Non-Generic Stack
|Generic Stack||Non-Generic Stack|
|Generic stack is defined under System.Collections.Generic namespace.||Non-Generic stack is defined under System.Collections namespace.|
|Generic stack can only store same type of elements.||Non-Generic stack can store same type or different types of elements.|
|There is a need to define the type of the elements in the stack.||There is no need to define the type of the elements in the stack.|
|It is type- safe.||It is not type-safe.|
- Stack.ToString() Method in C# with examples
- C# | Stack<T>.TrimExcess Method with Examples
- Stack.Pop() Method in C#
- C# | Stack Class
- Stack.Contains() Method in C#
- Implementing Stack in C#
- C# | How to create a Stack
- Getting an enumerator that iterates through the Stack in C#
- Removing the object from the top of the Stack in C#
- Stack.Equals() Method in C#
- Stack.Synchronized() Method in C#
- Stack.Push() Method in C#
- Stack.Peek Method in C#
- Stack.ToArray() Method in C#
- How to get Synchronize access to the Stack in C#
- Stack.Count Property in C#
- Stack.GetEnumerator Method in C#
- Stack.CopyTo() Method in C#
- Stack.IsSynchronized Property in C#
- Stack.Clone() Method in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.