Wednesday, 6 May 2015

How to use Stack using System.Collections.Genrics NameSpace ?

Stack is used when you need a last-in, first-out access of items. When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item.

The Stack class represents a last-in-first-out (LIFO) Stack of Objects. Stack follows the push-pop operations. That is we can Push (insert) Items into Stack and Pop (retrieve) it back . Stack is implemented as a circular buffer. It follows the Last In First Out (LIFO) system. That is we can push the items into a stack and get it in reverse order. Stack returns the last item first. As elements are added to a Stack, the capacity is automatically increased as required through reallocation.

Methos of Stack:-












Property of Stack:-





Type Safety

One of the most significant features of Generics is Type Safety. In the case of the non-generic ArrayList class, if objects are used, any type can be added to the collections that can sometimes result in a great disaster. The following example shows adding an integer, string and object to the collection of Stack<> type;

Stack<int> stObj1 = new Stack<int>();
stObj .Add(100);
Stack<string> stObj = new Stack<string>();
stObj .Add("Item1");
Stack<Object> stObj2 = new Stack<Object>();
stObj .Add(New ClassEmp());



Source Code:-

using System;
using System.Collections.Genrics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleAppDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            stackOperations classObj = new stackOperations();
            while (true)
            {
               // Console.Clear();
                Console.WriteLine("\n");
                Console.WriteLine("1. Add an element");
                Console.WriteLine("2. See the Top element.");
                Console.WriteLine("3. Remove top element.");
                Console.WriteLine("4. Display stack elements.");
                Console.WriteLine("5. Exit");
                Console.Write("Select your choice: ");
                int choice = Convert.ToInt32(Console.ReadLine());
                switch (choice)
                {
                    case 1:
                        classObj.Push();
                        break;

                    case 2: classObj.Peek();
                        break;

                    case 3:  classObj.Pop();
                        break;
                    case 4: classObj.Display();
                        break;

                    case 5: System.Environment.Exit(1);
                        break;
                }
                Console.ReadKey();
            }
        }
    }

  
    class stackOperations
    {  
        string item;

      // using Generics in c# .NET
        Stack<string> stObj = new Stack<string>();
       
        public bool isEmpty()
        {
            if (stObj.Count == 0) return true;

            return false;
        }

        public void Push()
        { 
           Console.Write("\nEnter a Item\n");
           item = Console.ReadLine();
           stObj.Push(item);
           Console.WriteLine("\nItem pushed successfully!");
           
        }

        public void Pop()
        {
            if (isEmpty())
            {
                Console.WriteLine("Stack is empty!");
            }
            else
            {
                stObj.Pop();
                Console.WriteLine("Removed Successfuly"); 
            }
        }
        public void Peek()
        {
            if (isEmpty())
            {
              Console.WriteLine("Stack is empty!");  
            }
            else
            {
                Console.Write("Top Item is: {0}", stObj.Peek());  
            }
         }

        public void Display()
        {
            if (isEmpty())
            {
                Console.WriteLine("Stack is empty!");
            }
            else
            {
                Console.Write("\nItems in Stack are: \n");
                foreach (string item in stObj)
                {
                    Console.Write(item);
                    Console.Write("\n");
                }
            }
        }

    }
}


Output:-









































Tuesday, 5 May 2015

How to use Queue in c# .NET using Generics ?


It represents a first-in, first out collection of object. It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue, and when you remove an item, it is called deque.Objects stored in a Queue are inserted at one end and removed from the other.
We can Enqueue (add) items in Queue and we can Dequeue (remove from Queue ) or we can Peek (that is we will get the reference of first item ) item from Queue


Queue<T> is the class in the System.Collection.Generic Namespace, where T specifies the type of elements in the queue

Constructors:-
Queue<T> Constructor - Initializes a new instance of the Queue<T> class that is empty. The next lines shows how we create the empty queue.

Queue<string> queue = new Queue<string>();

Queue<T> Constructor (IEnumerable<T>) - Initializes a new instance of the Queue<T> class that contains elements copied from the specified collection.

string[] courses = { "10th","12th", "Bsc","Msc", "M. Phil","M. Pharama" };
Queue<string> queue = new Queue<string>(courses);

Queue<T> Constructor (Int32) - Initializes a new instance of the Queue<T> class that is empty and has the specified initial capacity.


Queue<string> queue = new Queue<string>(4);

Operation on Queue:-


















Property of Queue:-






Source code example:-

using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleAppDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            stackOperations classObj = new stackOperations();
            while (true)
            {
                // Console.Clear();
                Console.WriteLine("\n");
                Console.WriteLine("1. Add an element");
                Console.WriteLine("2. See the First element.");
                Console.WriteLine("3. Remove First element.");
                Console.WriteLine("4. Display Queue elements.");
                Console.WriteLine("5. Exit");
                Console.Write("Select your choice: ");
                int choice = Convert.ToInt32(Console.ReadLine());
                switch (choice)
                {
                    case 1:
                        classObj.Enqueue();
                        break;

                    case 2: classObj.Peek();
                        break;

                    case 3: classObj.Dequeue();
                        break;
                    case 4: classObj.Display();
                        break;

                    case 5: System.Environment.Exit(1);
                        break;
                }
                Console.ReadKey();
            }
        }
    }


    class stackOperations
    {
        string item;
        Queue<string> qObj = new Queue<string>();

        public bool isEmpty()
        {
            if (qObj.Count == 0) return true;

            return false;
        }

        public void Enqueue()
        {
            Console.Write("\nEnter a Item\n");
            item = Console.ReadLine();
            qObj.Enqueue(item);
            Console.WriteLine("\nItem Added successfully!");

        }

        public void Dequeue()
        {
            if (isEmpty())
            {
                Console.WriteLine("Queue is empty!");
            }
            else
            {
                qObj.Dequeue();
                Console.WriteLine("Removed Successfuly");
            }
        }
        public void Peek()
        {
            if (isEmpty())
            {
                Console.WriteLine("Queue is empty!");
            }
            else
            {
                Console.Write("First Item is: {0}", qObj.Peek());
            }
        }

        public void Display()
        {
            if (isEmpty())
            {
                Console.WriteLine("Queue is empty!");
            }
            else
            {
                Console.Write("\nItems in Queue are: \n");
                foreach (string item in qObj)
                {
                    Console.Write(item);
                    Console.Write("\n");
                }
            }
        }

    }
}


Output:-

Listing 1





































Listing 2





































Listing 3





































Listing 4