Stack n Queue In C# 2.0

The System.Collections.Generic.Stack Class


The .NET Framework Base Class Library includes a Stack class in the Sytem.Collections.Generic namespace.
Like the Queue class, the Stack class maintains its elements internally using a circular array. The Stack class exposes its data through two methods: Push(item), which adds the passed-in item to the stack, and Pop(), which removes and returns the item at the top of the stack.

When a C# program is executed, the CLR maintains a call stack which, among other things, keeps track of the function invocations. Each time a function is called, its information is added to the call stack. Upon the function’s completion, the associated information is popped from the stack. The information at the top of the call stack represents the current function being executed.

The System.Collections.Generic.Stack Class


The .NET Framework Base Class Library provides the System.Collections.Generic.Queue class, which uses Generics to provide a type-safe Queue implementation.

Whereas our earlier code provided AddJob() and GetNextJob() methods, the Queue class provides identical functionality with its Enqueue(item) and Dequeue() methods, the Queue class maintain an internal circular array and two variables that serve as markers for the beginning and ending of the circular array: head and tail.

The Enqueue() method starts by determining if there is sufficient capacity for adding the new item to the queue. If so, it merely adds the element to the circular array at the tail index, and then “increments” tail using the modulus operator to ensure that tail does not exceed the internal array’s length.

The Dequeue() method returns the current element from the head index. It also sets the head index element to null and “increments” head. For those times where you may want to look at the head element, but not actually dequeue it, the Queue class also provides a Peek() method.

What is important to realize is that the Queue, unlike the List, does not allow random access.you cannot look at the third item in the queue without dequeing the first two items.the Queue class does have a Contains() method, so you can determine whether or not a specific item exists in the Queue. There’s also a ToArray() method that returns an array containing the Queue’s elements. CertainlyThere are more references and books by which you could visit . please check the given link: Microsoft Visual C# 2005 Step by Step Approach

Note: You may hear Queues referred to as FIFO data structures. FIFO stands for First In, First Out, and is synonymous to the processing order of first come, first served.

Leave a Reply