Queue
1.1. Concepts
Queue represents a first-in-first-out collection of objects.Queues are used when you need FIFO access to items.When you add an item to the list, it is called queuing, and when you remove an item from the list, it is called queuing.
1.2, Properties of Queue class
The following table lists some common properties of the Queue class:
attribute | describe |
---|---|
Count | Gets the number of elements contained in the Queue. |
1.3, Queue class methods
The following table lists some commonly used methods for the Queue class:
Sequence Number | Method Name & Description |
---|---|
1 |
public virtual void Clear(); Remove all elements from the Queue. |
2 |
public virtual bool Contains( object obj ); Determines whether an element is in a Queue. |
3 |
public virtual object Dequeue(); Remove and return the object at the beginning of the Queue. |
4 |
public virtual void Enqueue( object obj ); Add an object to the end of the Queue. |
5 |
public virtual object[] ToArray(); Copy Queue to a new array. |
6 |
public virtual void TrimToSize(); Set the capacity to the actual number of elements in the Queue. |
1.4. Example demo
class Program { static void Main(string[] args) { #region queue Queue queue = new Queue(); queue.Enqueue('A'); queue.Enqueue('B'); queue.Enqueue('C'); queue.Enqueue('D'); Console.WriteLine("Current queue: "); foreach (char item in queue) { Console.Write(item + " "); } Console.WriteLine(); queue.Enqueue('E'); queue.Enqueue('F'); Console.WriteLine("Current queue: "); foreach (char item in queue) { Console.Write(item + " "); } Console.WriteLine(); Console.WriteLine("Removing some values: "); char c = (char)queue.Dequeue(); Console.WriteLine($"The removed value: {c}"); c = (char)queue.Dequeue(); Console.WriteLine($"The removed value: {c}"); Console.Read(); #endregion } }
The results are as follows:
Stack
2.1. Concepts
Stack represents a collection of LIFO objects.Use stacks when you need LIFO access to items.When you add an item to the list, called a push element, and when you remove an item from the list, it is called a pop-up element.
2.2. Properties of Stack class
The following table lists some common properties of the Stack class:
attribute | describe |
---|---|
Count | Gets the number of elements contained in the Stack. |
2.3. Methods of Stack class
The following table lists some of the commonly used methods for the Stack class:
Sequence Number | Method Name & Description |
---|---|
1 |
public virtual void Clear(); Remove all elements from the Stack. |
2 |
public virtual bool Contains( object obj ); Determines whether an element is in a Stack. |
3 |
public virtual object Peek(); Returns the object at the top of Stack without removing it. |
4 |
public virtual object Pop(); Remove and return the object at the top of Stack. |
5 |
public virtual void Push( object obj ); Add an object to the top of the Stack. |
6 |
public virtual object[] ToArray(); Copy Stack to a new array. |
2.4. Example demo
class Program { static void Main(string[] args) { #region stack Stack stack = new Stack(); stack.Push('A'); stack.Push('B'); stack.Push('C'); stack.Push('D'); Console.WriteLine("Current stack: "); foreach (char item in stack) { Console.Write(item + " "); } Console.WriteLine(); stack.Push('E'); stack.Push('F'); Console.WriteLine("Current stack: "); foreach (char item in stack) { Console.Write(item + " "); } Console.WriteLine(); Console.WriteLine($"The next popable value in stack: {stack.Peek()}"); Console.WriteLine("Removing some values: "); stack.Pop(); stack.Pop(); stack.Pop(); Console.WriteLine("Current stack: "); foreach (char item in stack) { Console.Write(item + " "); } Console.Read(); #endregion } }
The results are as follows:
Reference from: