Stack and Queue in C#

Submitted by: 
Language: 

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:

Name Description
Count Gets the number of elements contained in the stack
Clear Removes all elements from the stack
Contains Determines if an element is in the stack
Peek Returns the element at the top of the queue without removing it
Pop Removes and returns the element at the top of the stack
Push Inserts an element at the top of the stack
TrimExcess Sets 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:

Name Description
Count Gets the number of elements contained in the queue
Clear Removes all elements from the queue
Contains Determines if an element is in the queue
Peek Returns the element at the beginning of the queue without removing it
Dequeue Removes and returns the element at the beginning of the queue
Enqueue Adds an element to the end of the queue
TrimExcess Sets 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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3>
  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.