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
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:
- //declare a stack with elements have type of int.
- static void Main(string[] args)
- {
- //add elements to stack
- for (int i = 0; i < 6; i++)
- {
- int_stack.Push(i);
- //print value
- Console.WriteLine("push value to stack " + i);
- }
- //print number of elements in stack
- Console.WriteLine("number of elements in stack " + int_stack.Count);
- //get each element from stack
- while(int_stack.Count>0){
- {
- int val = int_stack.Pop();
- //print value
- Console.WriteLine("pop value from stack " + val);
- }
- Console.ReadKey();
- }
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
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:
- //declare a queue with elements have type of int.
- static void Main(string[] args)
- {
- //add elements to queue
- for (int i = 0; i < 6; i++)
- {
- int_queue.Enqueue(i);
- //print value
- Console.WriteLine("push value to stack " + i);
- }
- //print number of elements in queue
- Console.WriteLine("number of elements in queue " + int_queue.Count);
- //get each element from stack
- while(int_queue.Count>0){
- int val = int_queue.Dequeue();
- //print value
- Console.WriteLine("pop value from queue " + val);
- }
- Console.ReadKey();
- }
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
- 338 views