Stack and Queue in C#

Objective

Stack and queue is very useful in a program. For example, you want to process a group of object like queue (First In First Out), so you can use queue in this case. This tutorial will guide you how to use stack and queue in C# language.

Let's go

Stack

Naturally you can see stack very often. An example is you put files in a pile, so when you want to take a file, you need take it from top and go down. In programming, stack often uses in statement processing. With stack all operations is processed in the end of it. To use stack, C# provide an Stack class. Bellow is list of common members that provided by Stack class:

NameDescription
Count Gets the number of elements contained in the stack
ClearRemoves all elements from the stack
ContainsDetermines if an element is in the stack
PeekReturns the element at the top of the queue without removing it
PopRemoves and returns the element at the top of the stack
PushInserts an element at the top of the stack
TrimExcessSets the capacity to the actual number of elements in the stack, if that number is less than 90% of the current capacity

This example bellow show you how to work with stack:

  1. //declare a stack with elements have type of int.
  2. static Stack<int> int_stack = new Stack<int>();
  3.  
  4. static void Main(string[] args)
  5. {
  6. //add elements to stack
  7. for (int i = 0; i < 6; i++)
  8. {
  9. int_stack.Push(i);
  10. //print value
  11. Console.WriteLine("push value to stack " + i);
  12. }
  13.  
  14. //print number of elements in stack
  15. Console.WriteLine("number of elements in stack " + int_stack.Count);
  16. //get each element from stack
  17. while(int_stack.Count>0){
  18. {
  19. int val = int_stack.Pop();
  20. //print value
  21. Console.WriteLine("pop value from stack " + val);
  22. }
  23. Console.ReadKey();
  24. }

Here is output of the program:
push value to stack 0
push value to stack 1
push value to stack 2
push value to stack 3
push value to stack 4
push value to stack 5
number of elements in stack 6
pop value from stack 5
pop value from stack 4
pop value from stack 3
pop value from stack 2
pop value from stack 1
pop value from stack 0

Queue

In case you want to process a group of objects like a queue (First In First Out), you can do it with a queue. An example of queue is buying ticket, people must arrange in a queue and wait for his/her turn. In programming language, queue is very useful, especially with high performance system. C# provide a Queue class to implement a queue. Here is list of common members:

NameDescription
Count Gets the number of elements contained in the queue
ClearRemoves all elements from the queue
ContainsDetermines if an element is in the queue
PeekReturns the element at the beginning of the queue without removing it
DequeueRemoves and returns the element at the beginning of the queue
EnqueueAdds an element to the end of the queue
TrimExcessSets the capacity to the actual number of elements in the queue, if that number is less than 90% of the current capacity

Here is example of how to work with queue:

  1. //declare a queue with elements have type of int.
  2. static Queue<int> int_queue = new Queue<int>();
  3.  
  4. static void Main(string[] args)
  5. {
  6. //add elements to queue
  7. for (int i = 0; i < 6; i++)
  8. {
  9. int_queue.Enqueue(i);
  10. //print value
  11. Console.WriteLine("push value to stack " + i);
  12. }
  13.  
  14. //print number of elements in queue
  15. Console.WriteLine("number of elements in queue " + int_queue.Count);
  16. //get each element from stack
  17. while(int_queue.Count>0){
  18. int val = int_queue.Dequeue();
  19. //print value
  20. Console.WriteLine("pop value from queue " + val);
  21. }
  22.  
  23. Console.ReadKey();
  24. }

The output of the program is:
push value to queue 0
push value to queue 1
push value to queue 2
push value to queue 3
push value to queue 4
push value to queue 5
number of elements in queue 6
pop value from queue 0
pop value from queue 1
pop value from queue 2
pop value from queue 3
pop value from queue 4
pop value from queue 5

Summary

In this tutorial we have shown you a very useful and important point in programming language in general, and C# language in private. Hope that you can investigate more carefully about that theme, and can do more with it.

Add new comment