
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Stack and Queue in C#
Stack
Stack class represents a last-in, first out collection of object. It is used when you need a last-in, first-out access of items.
The following is the property of Stack class −
Count− Gets the number of elements in the stack.
The following are the methods of Stack class −
Sr.No. | Method & Description |
---|---|
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. |
The following is an example showing how to work with Stack class and its Push() and Pop() method −
Example
using System; using System.Collections; namespace CollectionsApplication { class Program { static void Main(string[] args) { Stack st = new Stack(); st.Push('A'); st.Push('B'); st.Push('C'); st.Push('D'); Console.WriteLine("Current stack: "); foreach (char c in st) { Console.Write(c + " "); } Console.WriteLine(); st.Push('P'); st.Push('Q'); 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 + " "); } } } }
Output
Current stack: D C B A The next poppable value in stack: Q Current stack: Q P D C B A Removing values.... Current stack: C B A
Queue
Queue collection class is a concept in C# that is included in the System.Collection namespace. The elements are stored in a QUEUE in FIFO. The first element added will be the first to go out like a queue of people outside a movie hall to buy tickets.
It has two methods −
- Enqueue() method to add values
- Dequeue() method to retrieve values
Enqueue
Add items in the queue.
Queue q = new Queue(); q.Enqueue(“Two”); q.Enqueue(“One”);
Dequeue
Return items from the queue.
Queue q = new Queue(); q.Enqueue(“Two”); q.Enqueue(“One”); // remove elements while (q.Count > 0) Console.WriteLine(q.Dequeue());
- Related Articles
- Stack and Queue in Python using queue Module
- Difference Between Stack and Queue
- Differences between stack and queue data structure
- Difference between Stack and Queue Data Structures
- Create a Stack and Queue using ArrayDeque in Java
- Reverse a Stack using Queue
- Check if a queue can be sorted into another queue using a stack in Python
- Python Program to Implement Stack using One Queue
- How can we Implement a Stack using Queue in Java?
- How can we Implement a Queue using Stack in Java?\n
- Check if moves in a stack or queue are possible or nots in Python
- queue::front() and queue::back() in C++ STL
- queue::empty() and queue::size() in C++ STL
- queue::push() and queue::pop() in C++ STL
- Difference Between Priority Queue and Queue Implementation in Java?

Advertisements