Sequential Lists - Queue and Stack

Posted by Techie Cocktail | 11:15 PM | | 0 comments »

Sequential lists are collections of objects that are stored and retrived as lists in a sequential fashion. In .net there are two types of sequential collections called Queue and stack. Both of these are found in system.collections namespace.

Queue
The Queue list is of FIFO (First-in-First-out) type of sequential collection i.e. the object that is stored first in the queue gets retrived first when the queue is dequeued. The items in the queue are added using Enqueue method and are removed using Dequeue method. Count property gets the number of items in the queue.

The below example shows how to add items into the queue.


static void Main(string[] args)
{
int queueCount;

Queue _queue = new Queue();
_queue.Enqueue("obj1");
_queue.Enqueue("obj2");
_queue.Enqueue("obj3");
_queue.Enqueue("obj4");
_queue.Enqueue("obj4");
_queue.Enqueue(null);

queueCount= _queue.Count;
Console.WriteLine(queueCount.ToString()); //count = 6
Console.Read();
}

As you can see in the above example, queue also accepts duplicates and null values.

Now lets see some of the methods and properties on the queue to check its count, access and remove them as in the following example.

static void Main(string[] args)
{

Queue _queue = new Queue();

//Add items to the queue
_queue.Enqueue("obj1");
_queue.Enqueue("obj2");
_queue.Enqueue("obj3");
_queue.Enqueue("obj4");
_queue.Enqueue("obj4");
_queue.Enqueue(null);

//Displays the queue count.
Console.WriteLine(_queue.Count.ToString()); //6

//Removes the first element from the queue.
Console.WriteLine(_queue.Dequeue()); //removes obj1

//displays the top most queue element but does not removes it from the queue.
Console.WriteLine(_queue.Peek()); //displays obj2

//count check after dequeue and peek.
Console.WriteLine(_queue.Count.ToString()); //5

Console.Read();
}

Stack
The stack class is LIFO (Last-in-First-Out) type of sequential list collection. It means that the object that is stored last is retrived first during retrieval operation. To add the item to the stack, it uses push() method and to retrieve items it uses pop() method.

Stack also uses count property and peek() method as in Queue and has similar uses.

Lets see an example to add and retrieve objects in Stack as in the below example.

static void Main(string[] args)
{

Stack _stack = new Stack();

//Add items to the queue
_stack.Push("obj1");
_stack.Push("obj2");
_stack.Push("obj3");
_stack.Push("obj4");
_stack.Push("obj4");
_stack.Push(null);

//Displays the queue count.
Console.WriteLine(_stack.Count.ToString()); //6

//Removes the first element from the queue.
Console.WriteLine(_stack.Pop()); //removes null
Console.WriteLine(_stack.Pop()); //removes obj4

//displays the top most queue element but does not removes it from the queue.
Console.WriteLine(_stack.Peek()); //views obj4

//count check after dequeue and peek.
Console.WriteLine(_stack.Count.ToString()); //4

Console.Read();
}

As we can see from the above examples, both Queue and Stack accepts duplicates and null values. You can perform validations checks while inserting items into these collections as required. Also, the items can be accessed only in sequential fashion, they dont have indexers in order to access items in the middle of the list as we do when using arrays or other collections.

0 comments