Sunday 4 December 2016

C# Collections: ArrayList, HashTable, Stack

using System;
usingSystem.Collections;
usingSystem.Linq;
usingSystem.Text;

namespace App11
{
classProgram
    {
staticvoid Main(string[] args)
        {
StackDemo();
ArrayListDemo();
HashTableDemo();
Console.ReadLine();

        }
staticvoidArrayListDemo()
        {
Console.WriteLine("\n\nARRAYLIST DEMO !");
ArrayList al = newArrayList();
al.Add(1);
al.Add(2);
al.Add(3);
al.Add(4);
Console.WriteLine(al.Capacity);
Console.WriteLine(al.Count);
foreach (int x in al)
Console.WriteLine(x);

al.Clear();
al.Add("Monu");
al.Add("Alley");
al.Add("Sym");
Console.WriteLine(al.Capacity);
Console.WriteLine(al.Count);
foreach (string x in al)
Console.WriteLine(x);
        }
staticvoidHashTableDemo()
        {
Console.WriteLine("\n\nHASHTABLE DEMO !");
Hashtableht = newHashtable();
ht.Add(12,"Monu");
ht.Add(111, "Alley");
ht.Add(110, "Sym");

foreach (int k inht.Keys)
Console.WriteLine(k + " " + ht[k]);
//OR
ICollection key = ht.Keys;
foreach(int k in key)
Console.WriteLine(k + " " + ht[k]);
        }
staticvoidStackDemo()
        {
Console.WriteLine("STACK DEMO !");
Console.WriteLine("Please Enter the number of elements you want to Push in Stack :");
int n = Convert.ToInt32(Console.ReadLine());
Stackst = newStack();
for (int i = 0; i < n; i++)
            {
Console.WriteLine("PUSH : ");
st.Push(Console.ReadLine());
            }
Console.WriteLine();
Console.WriteLine("Displaying Elements of Stack : ");
while (st.Count != 0)
            {
Console.WriteLine(st.Pop());
            }
        }
    }
}


STACK DEMO!

    Please Enter the number of elements you want to push in Stack:

    3

    PUSH:

    5

    PUSH:

    4

    PUSH:

    2

    Displaying Element of Stack:

    2

    4

    5


    ARRAYLIST DEMO!

    4

    4

    1

    2

    3

    4

    4

    3

    amit

    mohan

    ajit


    HASHTABLE DEMO!

    111mohan

    12amit

    110ajit

    111mohan

    12amit

    110ajit

No comments:

Post a Comment