Friday, 28 July 2017

Collection ,Stacks, queues, lists, and hash Tables in C#.

Collection classes are specific classes for data storage and retrieval.Collections are similar to Arrays, it provide a more flexible way of working with a group of object. These classes provide maintain for stacks, queues, lists, and hash tables. Collections are similar to Arrays, it provide a more flexible way of working with a group of objects.



There are the normally two types of Collections in .Net Frame work.

 1.Non-generic     2.  Generic
1  .In non-genrics each element can represent a value of a different type. In this non-genric elements can be added / removed at runtime & most important thing is array Size are  not fixed.

2 .Generic Collections work on the specific type that is specified in the program whereas non-generic Stacks, queues, lists, and hash tables are also known as Generic Collections.Collections work on the object type.

1.1:. Specific type(Like int ,string etc)
1.2: Array Size is not fixed
1.3: Elements can be added / removed at runtime.

A.Stacks .B Queues C. lists.D Hash tables.

A: The stack is a unique case collection which represents a last in first out (LIFO) concept. We can say that we have a special type of collection which stores elements in LIFO form (Last In First Out) in C#.  C# includes a generic and non-generic Stack.
Stack allows null value and also duplicate values.
It provide various method for their functionality methrod name are:
It provides a Push() method to add a value and Pop() or Peek() methods to retrieve values.
Declaration of the stack – A stack is produced with the help of the Stack Data type.
The keyword "new" is used to create an object of a Stack. 

Stack st = new Stack().

Adding  values into Stack:

The Push() method adds values into the Stack.

Stack.push(element) & Peek()

The Peek() method returns the last (top-most) value from the stack. Calling Peek() method on empty stack will throw InvalidOperationException. 

Pop():

You can also retrieve the value using the Pop() method. The Pop() method removes and returns the value that was added last to the Stack.
 The Pop() method call on an empty stack will raise an InvalidOperationException
The Contains() method checks whether the specified item exists in a Stack collection or not. It returns boolean valve.

Clear:

The Clear() method removes all the values from the stack.

Count – This property is used to get the number of things in the Stack..

Stack.Count

Contains - This method is used to see if an element is present in the Stack.

Contains(element).


Example:

using System;
using System.Collections;

public class Program
{
    public static void Main()
    {
        Stack myStack = new Stack();  //The keyword "new" is used to create an object of a Stack. 
        myStack.Push(1);       //      The Push() method adds values into the Stack.
        myStack.Push(2);
        myStack.Push(3);
        myStack.Push(4);
        myStack.Push(5);

        Console.WriteLine("total elements in Stack are: {0}", myStack.Count);

        while (myStack.Count > 0)       //You can also retrieve the value using the Pop() method
            Console.WriteLine(myStack.Pop());
   

 Console.Write("total elements in Stack are after result: {0}", myStack.Count);

   //Count – This property is used to get the number of things in the Stack..
        Console.Read();
    }

}


Output:



Share this


0 Comments

Advertisement