You are on page 1of 4

Shriram Institute of Information Technology, Paniv

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");

Prof. Honrao Charushila Prakash (1 )


Shriram Institute of Information Technology, Paniv

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());

Console.WriteLine("\nRemoved element is: " + mystack.Pop());


Console.WriteLine("\nAfter Removing stack is --");
foreach (object obj in mystack)
Console.WriteLine(obj.ToString());
Console.WriteLine("\nTop most element from stack is: "+mystack.Peek());
Console.WriteLine("\nStack contain element: " + mystack.Contains("Java"));
Console.WriteLine("\nStack contain {0} number of elements", mystack.Count);
Console.ReadLine();
}
}
}

Output -
Elements add in stack
Stack Elements are..
Java
DBMS
PHP
CPP

Removed element is: Java

After Removing stack is --


DBMS
PHP
CPP

Top most elements from stack is: DBMS

Stack contain element: False

Stack contain 3 number of elements

➢ Queue Class –
Queue is a data structure which is based on First in First Out (FIFO) principle. The

Prof. Honrao Charushila Prakash (2 )


Shriram Institute of Information Technology, Paniv

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 --");

Prof. Honrao Charushila Prakash (3 )


Shriram Institute of Information Technology, Paniv

foreach (object obj in myqueue)


Console.WriteLine(obj.ToString());
Console.WriteLine("\nQueue contain element: " + myqueue.Contains("Java"));
Console.WriteLine("\nQueue contain {0} number of elements",myqueue.Count);
Console.ReadLine();
}
}
}

Output –
Elements add in Queue
Queue Elements are..
CPP
PHP
DBMS
Java

Removed element is: CPP

After Removing Queue is --


PHP
DBMS
Java

Queue contain element: True

Queue contain 3 number of elements

➢ Disadvantages of non-generic collections -


1) All objects in non-generic collections are stored as object of type, System. Object. Implicit boxing

takes place if any value type is stored in any of this collection.

2) The non-generic collections can store object of different types simultaneously. Thus there is no

guarantee of type safety with non-generic collections.

3) Moreover the garbage collector overhead increases as objects are being allocated on heap.

Prof. Honrao Charushila Prakash (4 )

You might also like