All type of Data Structures in C# Console(Array, ArrayList, List, LinkedList, Queue, Stack, Dictionary) - C# Tutorials

Latest

For Video tutorials Visit my Channel https://www.youtube.com/channel/UCVWpZVbMqLHuQBigIfc9I8Q

Friday, 13 January 2017

All type of Data Structures in C# Console(Array, ArrayList, List, LinkedList, Queue, Stack, Dictionary)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;



namespace dataStructuresTypes
{
    class dataStructures
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter 1 for Array");
            Console.WriteLine("Enter 2 for ArrayList");
            Console.WriteLine("Enter 3 for List<>");
            Console.WriteLine("Enter 4 for LinkedList");
            Console.WriteLine("Enter 5 for Queue");
            Console.WriteLine("Enter 6 for Stack");
            Console.WriteLine("Enter 7 for Dictionary");
            Console.WriteLine("Enter 8 for Exit");
            Console.WriteLine("Enter your Option");
           
            int op = Convert.ToInt16(Console.ReadLine());
           
            switch (op)
            {
                case 1:
                    {
                        int[] myArray = new int[5];
                        myArray[0] = 5;
                        myArray[1] = 15;
                        myArray[2] = 25;
                        myArray[3] = 35;
                        myArray[4] = 45;
                        int[] myArray2 = { 0, 1, 2, 3, 4 };
                        for (int i = 0; i < 6; i++)
                        {
                            Console.WriteLine(myArray[i]);// try this one myArray2[i]
                        }
                        break;
                    }
                case 2:
                    {
                        ArrayList myArray3 = new ArrayList();
                        myArray3.Add(50);
                        myArray3.Add(60);
                        myArray3.Add(70);
                        myArray3.Add(80);
                        myArray3.Add(90);
                        myArray3.Add(100);
                        for (int i = 0; i < 6; i++)
                        {
                            Console.WriteLine(myArray3[i]);
                        }
                        break;
                    }
                case 3:
                    {
                        List<int> myList = new List<int>();
                        myList.Add(2);
                        myList.Add(4);
                        myList.Add(6);
                        myList.Add(8);
                        myList.Add(10);
                        for (int i = 0; i < 6; i++)
                        {
                            Console.WriteLine(myList[i]);
                        }
                        break;
                    }
                case 4:
                    {
                        LinkedList<int> myLinkedList = new LinkedList<int>();
                        myLinkedList.AddFirst(5);
                        myLinkedList.AddLast(10);
                        myLinkedList.AddLast(15);
                        myLinkedList.AddLast(20);
                        myLinkedList.AddLast(25);
                        foreach (var item in myLinkedList)
                        {
                            Console.WriteLine(item);
                        }
                        break;
                    }
                case 5:
                    {
                        Queue<int> myQueue = new Queue<int>();
                        myQueue.Enqueue(10);
                        myQueue.Enqueue(20);
                        myQueue.Enqueue(30);
                        myQueue.Enqueue(40);
                        myQueue.Enqueue(50);
                        foreach (var item in myQueue)
                        {
                            Console.WriteLine(item);
                        }
                        break;
                    }
                case 6:
                    {
                        Stack<int> myStack = new Stack<int>();
                        myStack.Push(50);
                        myStack.Push(40);
                        myStack.Push(30);
                        myStack.Push(20);
                        myStack.Push(10);
                        foreach (var item in myStack)
                        {
                            Console.WriteLine(item);
                        }
                        break;
                    }
                case 7:
                    {
                        Dictionary<String, Int16> myDictionary = new Dictionary<String, Int16>();
                        myDictionary.Add("Rizwan", 23);
                        myDictionary.Add("Bilal", 20);
                        myDictionary.Add("Amir", 15);
                        myDictionary.Add("Faisal", 10);
                        myDictionary.Add("Zain", 5);
                        foreach (KeyValuePair<string, Int16> student in myDictionary)
                        {
                            Console.WriteLine("Key = {0}, Value = {1}",
                            student.Key, student.Value);
                        }
                        break;
                    }
                case 8:
                    {
                        System.Environment.Exit(1);
                        break;
                    }
            }
         Console.ReadKey();
        }
    }
}//end

No comments:

Post a Comment