0% found this document useful (0 votes)
19 views66 pages

7.collection Classes

Uploaded by

lion man
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views66 pages

7.collection Classes

Uploaded by

lion man
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 66

University of Computer Studies

Chapter (6)
Collection Classes

1
Contents

Array Class
Properties and Methods in Array Class Hashtable
Properties and Method in Hashtable
Collection Classes
Differences between SortedList and
ArrayList Hashtable
Properties and Methods in ArrayList
Stack
List Properties and Method in Stack
Properties and Methods in Dictionary
Queue
Dictionary Properties and Method in Queue
Properties and Methods in Dictionary

SortedList
Properties and Method in SortedList
2
Array Class

 store multiple values in a single variable, instead of declaring separate variables for each value
 stores a fixed-size sequential collection of elements of the same type
 two kinds of arrays in C#
Normal Array:

int[] num = new int[10];


num[0]=200;
Console.WriteLine(num[0]);

double[,] salary = new double[5,2]


salary[0,1]=300000;
Console.WriteLine(salary[0,1]);

3
Array Class
 another kind of array provided by Array class
 the base class for all the arrays in C#
 defined in the System namespace
Example

Array num = Array.CreateInstance(typeof(int), 10);


num.setValue(200,0);
Console.WriteLine(num.getValue(0));

Array salary = Array.CreateInstance(typeof(double),5,2)


salary.setValue(300000,0,1);
Console.WriteLine(salary.getValue(0,1));

4
Properties and Methods in Array Class

Table 1

Property Description

IsFixedSize Gets a value indicating whether the Array has a fixed size

IsReadOnly Gets a value indicating whether the Array is read-only

Length Gets a 32-bit integer that represent the total number of elements in all the dimensions of the
Array

LongLength Gets a 64-bit integer that represent the total number of elements in all the dimensions of the
Array

Rank Get the rank (the number of dimensions) of the Array

5
Properties and Methods in Array Class
Table 2

Method Description
Clear( ) Sets a range of elements in the Array to zero, to false, or to null, depending on the
element type
Copy(Array, Array,Int32) Copies a range of elements from an Array starting at the first element and pastes them
into another Array starting at the first element. The length is specified as a 32-bit
integer.
CopyTo(Array, Int32) Copies all the elements of the current one-dimensional Array to the specified one-
dimensional Array starting at the specified destination Array index. The index is
specified as a 32-bit integer.
GetLength(dimension) Gets a 32-bit integer that represents the number of elements in the specified dimension of
the Array
GetLongLength(dimension) Gets a 64-bit integer that represents the number of elements in the specified dimension of
the Array
GetLowerBound(dimension) Gets the lower bound of the specified dimensions in the Array
6
Properties and Methods in Array Class
Table 3
Method Description

GetUpperBound(dimension) Gets the upper bound of the specified dimension in the Array. The index is specified as a 32-bit
integer.
GetValue(Int32) Gets the value at the specified position in the one-dimensional Array. The index is specified as
32-bit integer
IndexOf(Array, Object) Searches for the specified object and returns the index of the first occurrence within the entire
one-dimensional Array
Reverse(Array) Reverse the sequence of the elements in the entire one-dimensional Array

SetValue(Object, Int32) Sets a value to the element at the specified position in the one-dimensional Array. The index is
specified as 32-bit integer
Sort(Array) Sorts the elements in an entire one-dimensional Array using the IComparable implementation of
each element of the Array
ToString( ) Returns a string that represents the current object. (Inherited from Object)
7
Collection Classes

 allocate memory dynamically to elements

 store, manage and manipulate (add, remove, find, and insert) similar data more efficiently

 implement the following common functionality:


 Adding and inserting items to a collection
 Removing items from a collection
 Finding and sorting items
 Replacing items
 Copy and clone collections and items
 Capacity and Count properties to find the capacity of the collection and number of items in the collection

8
Collection Classes

 two types of collections in C#: non-generic collections and generic collections

non-generic Collections generic Collections

 ArrayList  List
 HashTable  Dictionary
 SortedList  SortedList
 Stack  Stack
 Queue  Queue

9
ArrayList

 It is a non-generic type of collection in C#.

 It can contain elements of any data types.

 It is similar to an array, except that it grows automatically as you add items in it.

 Unlike an array, you don't need to specify the size of ArrayList.

10
Properties and Methods in ArrayList
Table 4

Property Description

Capacity Gets or sets the number of elements that the ArrayList can contain.

Count Gets the number of elements actually contained in the ArrayList.

IsFixedSize Gets a value indicating whether the ArrayList has a fixed size.

IsReadOnly Gets a value indicating whether the ArrayList is read-only.

Item Gets or sets the element at the specified index.

11
Properties and Methods in ArrayList
Table 5
Property Description

Add( ) / AddRange( ) Add( ) method adds single elements at the end of ArrayList. 
AddRange( ) method adds all the elements from the specified collection into ArrayList.

Insert( ) / InsertRange( ) Insert() method insert a single elements at the specified index in ArrayList. 
InsertRange() method insert all the elements of the specified collection starting from
specified index in ArrayList.

Remove()/ RemoveRange( ) Remove() method removes the specified element from the ArrayList. 
RemoveRange() method removes a range of elements from the ArrayList.

RemoveAt( ) Removes the element at the specified index from the ArrayList.

Sort( ) Sorts entire elements of the ArrayList.

12
Properties and Methods in ArrayList
Table 6
Property Description

Reverse( ) Reverses the order of the elements in the entire ArrayList.

Contains( ) Checks whether specified element exists in the ArrayList or not. Returns true if exists
otherwise false.
Clear( ) Removes all the elements in ArrayList.

CopyTo( ) Copies all the elements or range of elements to compitible Array.

GetRange( ) Returns specified number of elements from specified index from ArrayList.

IndexOf( ) Search specified element and returns zero based index if found. Returns -1 if element
not found.
ToArray( ) Returns compitible array from an ArrayList.

13
Properties and Methods in ArrayList

Create ArrayList and Add elements


Example
ArrayList A = new ArrayList( );
A.Add(1);
A.Add("Two");
A.Add(3);
A.Add(4.5);

ArrayList B = new ArrayList( ) { 100, 200 };


B.AddRange(A);

ArrayList C= new ArrayList( ) { 100, "Two", 12.5, 200 };

14
Properties and Methods in ArrayList
Accessing Elements
Example
ArrayList A = new ArrayList( );
A.Add(1);
A.Add("Two");
A.Add(3);
A.Add(4.5f);

int firstElement = (int) A[0];

string secondElement = (string) A[1];

int thirdElement = (int) A [2];

float fourthElement = (float) A [3];

var firstElement = A [0];


15
Properties and Methods in ArrayList

Iterating an ArrayList by using a foreach or a for loop


Example:

ArrayList A = new ArrayList(){ 1, "Two", 3, 4.5F };

foreach (var value in A)

Console.WriteLine(value);

( or )

for(int i = 0 ; i< A.Count; i++)

Console.WriteLine(A[i]);

16
Properties and Methods in ArrayList

Inserting Elements into ArrayList

Signature: void Insert(int index, Object value)

Example:
ArrayList A = new ArrayList ();
A.Add(1);
A.Add("Two");
A.Add(3);
A.Add(4.5);

A.Insert(1, "Second Item");


A.Insert(2, 100);

foreach (var value in A)


Console.WriteLine(value) 17
Properties and Methods in ArrayList

Signature: Void InsertRange(int index, ICollection c)

Example:
ArrayList A = new ArrayList();
A.Add(100);
A.Add(200);

ArrayList B = new ArrayList();


B.Add(10);
B.Add(20);
B.Add(30);

B.InsertRange(2, A);

foreach(var item in B)
Console.WriteLine(item); 18
Properties and Methods in ArrayList
Remove Elements from ArrayList

Signature: void Remove(Object )

Example:
ArrayList num = new ArrayList();
num.Add(100);
num.Add(200);
num.Add(300);

num.Remove(100);
foreach (var item in num)
Console.WriteLine(item);

19
Properties and Methods in ArrayList

Signature: void RemoveAt(int index)

Example: RemoveAt()
ArrayList num = new ArrayList();
num.Add(100);
num.Add(200);
num.Add(300);

num.RemoveAt(1);

foreach (var item in num)


Console.WriteLine(item);

20
Properties and Methods in ArrayList

Signature: void RemoveRange(int index, int count)

Example:
ArrayList num = new ArrayList();
num.Add(100);
num.Add(200);
num.Add(300);
num.RemoveRange(0,2);

foreach(var item in num)


Console.WriteLine(item);

21
Properties and Methods in ArrayList

ArrayList Sorting and Reverse


Example:

ArrayList num = new ArrayList(); num.Reverse();


num.Add(300); Console.WriteLine("Reverse Order:");
num.Add(200); foreach(var item in num)
num.Add(100); Console.WriteLine(item);
num.Add(500);
num.Add(400); num.Sort();
foreach(var item in num) Console.WriteLine("Ascending Order:");
Console.WriteLine(item); foreach(var item in num)
Console.WriteLine(item);

22
Properties and Methods in ArrayList

Check for an Existing Elements in ArrayList


Example:
IList A = new ArrayList();
A .Add(100);
A .Add("Hello World");
A .Add(300);
Console.WriteLine(A .Contains(100)); // true

23
List

 It is used to store and fetch elements.

 It can have duplicate elements.

 It is found in System.Collections.Generic namespace.

24
Properties and Methods in List
Table 7
Properties/ Methods Description

Capacity Gets or sets the total number of elements the internal data structure can
hold without resizing.

Count Gets the number of elements contained in the List<T>.


Item[ Int32] Gets or sets the element at the specified index.
Add( ) Adds an object to the end of the List<T>.
AddRange( ) Adds the elements of the specified collection to the end of the List<T>.

BinarySearch( ) Uses a binary search algorithm to locate a specific element in the sorted
List<T> or a portion of it.

Clear( ) Removes all elements from the List<T>.


Contains( T ) Determines whether an element is in the List<T>.
25
Properties and Methods in List
Create List and Add elements
Example
List <string> names = new List<string>( );
names.Add(“Li Li”);
names.Add(“Cindy”);
names.Add(“Mary”);
names.Add(“Susan");
(or)
List <string> names = new List<string>() {“Li Li”, “Cindy”, “Mary”, “Su San”};

Iterating an List by using a foreach loop


foreach (var name in names)
{ Console.WriteLine(name); }
26
Dictionary

 same as English dictionary - collection of words and their definitions

 collection of Keys and Values, where key is like word and value is like definition

 a generic collection class in the System.Collection.Generics namespace.

 TKey : the type of key and TValue: the type of value

 stores Key-Value pairs where the key must be unique

27
Properties and Methods in Dictionary

Table 8

Property Description

Count Gets the total number of elements exists in the Dictionary<Tkey, Tvalue>

Keys Returns collection of keys of Dictionary<TKey, Tvalue>

Values Returns collection of values of Dictionary<TKey, Tvalue>

28
Properties and Methods in Dictionary
Table 9
Method Description

Add( ) Adds an item to the Dictionary collection

Add( ) Add key-value pairs in Dictionary <TKey, TValue> collection

Remove( ) Removes the first occurrence of specified item from the Dictionary<TKey, TValue>

Remove( ) Remove the element with the specified key

ContainsKey( ) Checks whether the specified key exists in Dictionary<TKey, TValue>

ContainsValue( ) Checks whether the specified value exists in Dictionary<TKey, TValue>

Clear( ) Removes all the elements from Dictionary<TKey, TValue>

TryGetValue( ) Returns true and assigns the value with the specified key, if key does not exist

29
Properties and Methods in Dictionary
Create Dictionary and Add elements

Dictionary<int, string> dict = new Dictionary<int, string> ( );


dict.Add(1,“Apple");
dict.Add(2,“Orange");
dict.Add(3,“Pink");

Add key-value pair into dictionary

Dictionary<int, string> dict = new Dictionary<int, string> ( );


dict.Add(new KeyValuePair<int, string>(1, "One"));
dict.Add(new KeyValuePair<int, string>(2, "Two"));
dict.Add(3, "Three");

30
Properties and Methods in Dictionary
Create Dictionary with initialization

Dictionary<int, string> dict = new Dictionary<int, string> ( )


{
{1,"One"},
{2, "Two"},
{3,"Three"}
};

Accessing elements in Dictionary

Console.WriteLine ( dict[0] ); // KeyNotFound Error


Console.WriteLine ( dict[2] ); // access value with key
Console.WriteLine ( "Key: " + dict.Keys.ElementAt(2) );
Console.WriteLine ( "Value: " + dict.Values.ElementAt(2) );

31
Properties and Methods in Dictionary
Accessing elements in Dictionary by using for loop

for (int i = 0; i < dict.Count; i++)


{
Console.Write ( "Key: " + dict.Keys.ElementAt(i) + ", “ );
Console.Write ( "Value: " + dict.Values.ElementAt(i) );
Console.WriteLine ( );
}

Accessing Keys in Dictionary

foreach ( int k in dict.Keys )


{ Console.WriteLine( "Key: " + k ); }

32
Properties and Methods in Dictionary
Accessing Values in Dictionary

foreach (string st in dict.Values)


{ Console.WriteLine("Value: " + st); }

Accessing Keys and Values in Dictionary

foreach (int k in dict.Keys)


{ Console.WriteLine("Key: " + k + " , " + dict[k]); }

Iterate all elements in Dictionary by using foreach loop

foreach ( KeyValuePair<int, string> kp in dict)


{ Console.WriteLine("Key: " + kp.Key + " , " + kp.Value); }

33
Properties and Methods in Dictionary
Removing elements in Dictionary

dict.Remove(2);
dict.Remove(7);

Remove all elements in Dictionary

dict.Clear();

Check whether specified value exist or not in Dictionary

if ( dict.ContainsValue(“pink”) )
Console.WriteLine("This value already exist");

34
Properties and Methods in Dictionary

Check whether specified key exist or not in Dictionary

if (dict.ContainsKey(3))
Console.WriteLine("This key already exist");

Search value by using index

bool b = dict.TryGetValue(2, out result);


Console.WriteLine("Fount or Not: " + b);
Console.WriteLine("Value : " + result);

35
SortedList

 stores key-value pairs in the ascending order of key by default

 elements can be accessed both by key and index

 Key cannot be null but value can be null

 datatype of all keys must be same

36
Properties and Methods in SortedList
Table 10

Property Description

Capacity Gets or sets the number of elements that the SortedList instance can store

Count Gets the number of elements actually contained in the SortedList

IsFixedSize Gets a value indicating whether the SortedList has a fixed size

IsReadOnly Gets a value indicating whether the SortedList is read-only

Keys Get list of keys of SortedList

Values Get list of values in SortedList

37
Properties and Methods in SortedList
Table 11
Method Description

Add(object key, object value) Add key-value pairs into SortedList

Remove(object key) Removes element with the specified key

RemoveAt(int index) Removes element at the specified index

Contains(object key) Checks whether specified key exists in SortedList

Clear( ) Removes all elements from SortedList

GetByIndex(int index) Returns the value by index stored in internal array

GetKey(int index) Returns the key stored at specified index in internal array

IndexOfKey(object key) Returns an index of specified key stored in internal array

IndexOfValue(object value) Returns an index of specified value stored in internal array

38
Properties and Methods in SortedList
Create SortedList and Add elements
SortedList s = new SortedList( );
s.Add(3, “Red");
s.Add(4, “Pink");
s.Add(1, “Cyan");
s.Add(5, null);
s.Add(2, true);

Create SortedList with initialization


SortedList s1 = new SortedList()
{
{3, “Red"},
{4, “Pink"},
{1, “Cyan"},
{5, null},
{2, true}
};
39
Properties and Methods in SortedList
Count and Capacity
Console.WriteLine("Capacity: " + s.Capacity);
Console.WriteLine("Count: " + s.Count);

IsReadOnly, IsFixedSize
Console.WriteLine("IsReadOnly: " + s.IsReadOnly);
Console.WriteLine("IsFixedSize: " + s.IsFixedSize);

Accessing element in SortedList by key and index


Console.WriteLine("By using key: "+s["two"]);
Console.WriteLine("By using index: "+s.GetByIndex(0));

40
Properties and Methods in SortedList

Access all Keys in SortedList


ICollection k = s.Keys;
foreach (int x in k)
{ Console.Write(x + " , "); }

Access all Values in SortedList


ICollection v = s.Values;
foreach ( string x in v)
{ Console.Write(x + " , "); }

41
Properties and Methods in SortedList

Iterate elements in SortedList by using foreach loop


foreach( DictionaryEntry kvp in s)
Console.WriteLine(kvp.Key+ " ,"+ kvp.Value );

Iterate elements in SortedList by using for loop


for (int i = 0; i < s.Count; i++)
{
Console.Write(s.GetKey(i) + ", ");
Console.Write(s.GetByIndex(i));
Console.WriteLine();
}
42
Properties and Methods in SortedList

Access Key List in SortedList


IList k= s.GetKeyList();
foreach (int num in k)
{ Console.Write(k+ " "); }

Access Value List in SortedList


IList v = s.GetValueList();
foreach (string st in v)
{ Console.Write(st + " "); }

Insert item at specified index


s.SetByIndex(1, “Orange”); // replace value at specific index
43
Properties and Methods in SortedList

Search element in SortedList by Key and Value


int position= s.IndexOfKey("Mango"); // serach index of key
Console.WriteLine("Positiono so Mango:" + position);

int y = s.IndexOfValue(300000); // search index of value


Console.WriteLine("Positiono so 300000:" + position);

Remove element in SortedList by using Key and Index


s.Remove(3);
s.RemoveAt(4);
s.Clear();
44
Properties and Methods in SortedList

Check whether key and value exist or not in SortedList


if (s.ContainsValue(“Mango”))
Console.WriteLine("Value already exist");
if (s.ContainsKey(1))
Console.WriteLine("Value already exist");

Copy SortedList
SortedList s1=(SortedList) s.Clone(); // create a shallow copy of sortedlist object
// explicit conversion from object type to SortedList

foreach (DictionaryEntry kv in s1)


{ Console.WriteLine(kv.Key + " , " + kv.Value); } 45
Properties and Methods in SortedList

Array a = Array.CreateInstance(typeof(object), 10);


s.CopyTo(a, 0);
Console.WriteLine("Copy To");

for (int i = 0; i < a.Length; i++)


{
DictionaryEntry d = (DictionaryEntry) a.GetValue(i);
Console.WriteLine(d.Key + ", " + d.Value);
}

46
Hashtable
 non-generic collection type

 similar to generic Dictionary collection

 stores key-value pairs of any datatype where the Key must be unique

 key cannot be null whereas the value can be null

47
Properties and Methods in Hashtable
Table 12

Property Description

Count Gets the total count of key/value pairs in Hashtable

IsReadOnly Gets Boolean value indicating whether Hashtable is read-only

Keys Gets ICollection of keys in Hashtable

Values Gets ICollection of values in Hashtable

48
Properties and Methods in Hashtable
Table 13
Method Description
Add Adds an item with a key and value into hashtable
Remove Removes the item with the specified key from hashtable

Clear Removes all items from hashtable


Contains Checks whether hashtable contains a specific key
ContainsKey Checks whether hashtable contains a specific key
ContainsValue Checks whether hashtable contains a specific value
GetHashCode Returns the hash code for the specified key

49
Properties and Methods in Hashtable

Create Hashtable and Add elements


Hashtable ht = new Hashtable( );
ht.Add(1, “Orange");
ht.Add(2, “Airplane");
ht.Add(5, null);
ht.Add("Fv", "Five");
ht.Add(8.5F, true);

Create Hashtable with Initialization


Hashtable ht = new Hashtable( ) { { 1, “Orange" },
{ 2, “Mango" },
{ 5, null },
{ "Fv", "Five" },
{ 8.5F, true }
}; 50
Properties and Methods in Hashtable

Create Hashtable with Dictionary Instance


Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(1, "one");
dict.Add(2, "two");
dict.Add(3, "three");

Hashtable h = new Hashtable(dict);

Console.WriteLine("Count: " + ht.Count);

Console.WriteLine("Is Read Only : " + h.IsReadOnly);

Console.WriteLine("Is Fixed Size: " + h.IsFixedSize);

51
Properties and Methods in Hashtable

Access value by using key


string value1 =ht[2].ToString();
string value2 = (string)ht["Fv"];
float value3 = (float)ht[8.5F];

Console.WriteLine(value1);
Console.WriteLine(value2);
Console.WriteLine(value3);

52
Properties and Methods in Hashtable

Iterate all elements in Hashtable by using foreah loop


foreach (DictionaryEntry item in ht)
{ Console.WriteLine(item.Key + ", " + item.Value); }

Access all Keys


ICollection k = ht.Keys;
foreach (object item in k)
{ Console.Write(item + ", "); }

53
Properties and Methods in Hashtable

Access all Values in Hashtable


ICollection v = ht.Values;
foreach (var item in v)
{ Console.Write(item+ ", "); }

Remove specified element by using Key


ht.Remove(“Fv”);

Remove all elements


ht.Clear();

54
Properties and Methods in Hashtable

Check whether key and value exist in Hashtable


if(ht.ContainsValue(“Orange”))
Console.WriteLine("Value ready exist");

if(ht.ContainsKey(1))
Console.WriteLine(“Key ready exist”);

55
Properties and Methods in Hashtable

Copy Hashtable
Hashtable h =(Hashtable) ht.Clone(); // convert object type to hashtable typem
// shallow copy, return object type

foreach (DictionaryEntry item in h)


{ Console.WriteLine(item.Key + ", " + item.Value); }

Array A = Array.CreateInstance(typeof(object),4);
ht.CopyTo(A,0);

for (int i = 0; i < A.Length; i++)


{
DictionaryEntry d = (DictionaryEntry) A.GetValue(i);

Console.WriteLine(d.Key + ", " + d.Value);


} 56
Differences between SortedList and Hashtable
Table 14

SoredList Hashtable

a collection of key/value pairs that are sorted by the keys a collection of key/value pairs that are organized based on
the hash code of the key

elements are accessible by key and by index elements are accessible by key

both non-generic collection and generic collection non-generic collection

57
Stack

 C# includes a special type of collection which stores elements in LIFO style (Last In First Out).

 C# includes a generic and non-generic Stack.

 Stack allows null value and also duplicate values.

58
Properties and Methods in Stack
Table 15

Properties/Methods Description

Count Returns the total count of elements in the Stack.


Push( ) Inserts an item at the top of the stack.
Peek( ) Returns the top item from the stack.
Pop( ) Removes and returns items from the top of the stack.

Contains( ) Checks whether an item exists in the stack or not.


Clear( ) Removes all items from the stack.

59
Properties and Methods in Stack

Create Stack and Add elements


Stack myStack = new Stack( );
myStack.Push("Hello!!");
myStack.Push(null);
myStack.Push(1);
myStack.Push(2);
myStack.Push(3);
myStack.Push(4);
myStack.Push(5);

Iterating Stack by using foreach loop


foreach (var item in myStack)
{ Console.Write(item);}

60
Properties and Methods in Stack

Console.WriteLine(myStack.Peek());
Console.WriteLine(myStack.Peek());
Console.WriteLine(myStack.Peek());

Console.Write("Number of elements in Stack:”+ myStack.Count);

while (myStack.Count > 0)


{
Console.WriteLine(myStack.Pop());
}

Console.Write("Number of elements in Stack:“ + myStack.Count);

61
Queue

 Queue stores the elements in FIFO style (First In First Out), exactly opposite of the Stack collection.

 It contains the elements in the order they were added.

 Queue collection allows multiple null and duplicate values.

62
Properties and Methods in Queue
Table 16

Properties/Methods Description

Count Returns the total count of elements in the Queue.

Enqueue( ) Adds an item into the queue.

Dequeue( ) Removes and returns an item from the beginning of the queue.

Peek( ) Returns an first item from the queue

Contains( ) Checks whether an item is in the queue or not

Clear( ) Removes all the items from the queue.

TrimToSize( ) Sets the capacity of the queue to the actual number of items in the
queue.
63
Properties and Methods in Queue

Create Queue and Add elements


Queue q= new Queue();
q.Enqueue(3);
q.Enqueue(2);
q.Enqueue(1);
q.Enqueue("Four");

Iterating Queueby using foreach loop


foreach (var item in q)
{
Console.Write(item);
}

64
Properties and Methods in Queue

Console.WriteLine("Number of elements in the Queue:“+ q.Count);

Console.WriteLine(q.Peek());
Console.WriteLine(q.Peek());
Console.WriteLine(q.Peek());

Console.WriteLine(“Number of elements in the Queue:”+q.Count);

while (q.Count > 0)


{ Console.WriteLine(q.Dequeue()); }

Console.WriteLine("Number of elements in the Queue:“+ q.Count);

65
Thank You

66

You might also like