Professional Documents
Culture Documents
Unit – IX
Collection Classes
➢ Stack class –
Stack class is data structure which is based on Last in First Out (LIFO) principle. The
element is added to and removed from the same end of the stack. The mechanism to add an element
is known as push and remove an element is known as pop.
A stack takes a null reference as a valid value. It can also take duplicate value. Stack can grow
dynamically when we add elements in a stack.
The following are the some properties and methods used in a stack class –
Methods –
1) Push () – Push () method is used to insert an object on the top of the stack. This method takes
single argument as object.
Syntax – public virtual void Push (object obj)
2) Pop () – This method removes and returns the object from the top of the stack.
Syntax – public virtual void Pop ();
3) Peek () – This method returns the object at the top of stack without removing it.
Syntax – public virtual object Peek ();
4) Clear () – Clear () method is used to removes the entire element from the stack.
Syntax – public virtual void Clear ();
5) Contains () – Contains () method is used to check the specified object is present in stack or not. It
return true if object is present in stack otherwise it return false.
Syntax – public virtual bool Contains (object obj);
Property –
1) Count – Count Property returns the number of elements contained in stack.
Example –
using System;
using System.Collections;
namespace StackEx
{
class Program
{
static void Main(string[] args)
{
Stack mystack = new Stack();
mystack.Push("CPP");
mystack.Push("PHP");
mystack.Push("DBMS");
mystack.Push("Java");
Console.WriteLine("Elements add in stack");
Console.WriteLine("\nStack Elements are..");
foreach (object obj in mystack)
Console.WriteLine(obj.ToString());
Output -
Elements add in stack
Stack Elements are..
Java
DBMS
PHP
CPP
➢ Queue Class –
Queue is a data structure which is based on First in First Out (FIFO) principle. The
element is added from one end (known as rear end) and removed from other end (known as front
end). Queue processes messages or data sequentially. A queue takes a null reference as a valid value.
It can also take a duplicate value. Queue can also grow dynamically.
The following are the some properties and methods used in a stack class –
Methods –
1) Enqueue () – The Enqueue () method is used to add an object to the end of the Queue. It takes a
single argument as object type.
Syntax – public virtual void Enqueue (object obj)
2) Dequeue () – The Dequeue () method removes and return the object at beginning of the Queue.
Syntax - public virtual object Dequeue ()
3) Contains () - Contains () method is used to check the specified object is present in queue or not. It
return true if object is present in queue otherwise it return false.
Syntax – public virtual bool Contains (object obj);
4) Clear () - Clear () method is used to removes the entire element from the queue.
Syntax – public virtual void Clear ();
Property –
1) Count - Count Property returns the number of elements contained in stack
Example –
using System;
using System.Collections;
namespace QueueEx
{
class Program
{
static void Main(string[] args)
{
Queue myqueue = new Queue();
myqueue.Enqueue("CPP");
myqueue.Enqueue("PHP");
myqueue.Enqueue("DBMS");
myqueue.Enqueue("Java");
Console.WriteLine("Elements add in Queue");
Console.WriteLine("\nQueue Elements are..");
foreach (object obj in myqueue)
Console.WriteLine(obj.ToString());
Console.WriteLine("\nRemoved element is: " + myqueue.Dequeue());
Console.WriteLine("\nAfter Removing Queue is --");
Output –
Elements add in Queue
Queue Elements are..
CPP
PHP
DBMS
Java
2) The non-generic collections can store object of different types simultaneously. Thus there is no
3) Moreover the garbage collector overhead increases as objects are being allocated on heap.