C# 0 The Complete Reference


PART II C h a p t e r   2 4



Download 4,07 Mb.
Pdf ko'rish
bet986/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   982   983   984   985   986   987   988   989   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART II

C h a p t e r   2 4 :  

C o l l e c t i o n s ,   E n u m e r a t o r s ,   a n d   I t e r a t o r s    

795


The following program demonstrates 

Stack

:

// Demonstrate the Stack class.



using System;

using System.Collections.Generic;

class GenStackDemo {

  static void Main() {

    Stack st = new Stack();

    st.Push("One");

    st.Push("Two");

    st.Push("Three");

    st.Push("Four");

    st.Push("Five");

    while(st.Count > 0) {

      string str = st.Pop();

      Console.Write(str + " ");

    }


    Console.WriteLine();

  }


}

The output is shown here:

Five Four Three Two One

The Queue Class

Queue

is the generic equivalent of the non-generic 



Queue

 class. It supports a first-in, 

first-out list. 

Queue

 implements the 



ICollection

,

IEnumerable

, and 

IEnumerable

interfaces.



Queue

 directly implements the 



Clear( )

,

Contains( )

, and 

CopyTo( )

 methods 

Method

Description



public T Peek( )

Returns the element on the top of the stack, but does not 

remove it.

public T Pop( )

Returns the element on the top of the stack, removing it in the 

process.


public void Push(T 

v

)

Pushes 



v

 onto the stack. 

public T[ ] ToArray( )

Returns an array that contains copies of the elements of the 

invoking stack.

public void TrimExcess( )

Removes the excess capacity of the invoking stack.

T

ABLE



 24-19 

The Methods Defi ned by 

Stack

www.freepdf-books.com




796

 

P a r t   I I :  



E x p l o r i n g   t h e   C #   L i b r a r y

defined by 



ICollection

. (The 


Add( ) 

and


Remove( )

 methods are not supported, nor is 

the

IsReadOnly

 property.) 



Queue

 is a dynamic collection that grows as needed to 

accommodate the elements it must store. It defines the following constructors:

public Queue( )

public Queue(int 

capacity

)

public Queue(IEnumerable 



c

)

The first form creates an empty queue with an initial default capacity. The second form 



creates an empty queue with the initial capacity specified by 

capacity.

 The third form creates 

a queue that contains the elements of the collection specified by 

c.

In addition to the methods defined by the interfaces that it implements (and those 

methods defined by 

ICollection

 that it implements on its own), 



Queue

 defines the 

methods shown in Table 24-20. 

Queue

works just like its non-generic counterpart. To 

put an object in the queue, call 

Enqueue( )

. To remove and return the object at the front of 

the queue, call 

Dequeue( )

. You can use 



Peek( )

 to return, but not remove, the next object. 

An

InvalidOperationException

 is thrown if you call 



Dequeue( )

 or 


Peek( )

 when the 

invoking queue is empty.

Here is an example that demonstrates 



Queue

:

// Demonstrate the Queue class.



using System;

using System.Collections.Generic;

class GenQueueDemo {

  static void Main() {

    Queue q = new Queue();

    q.Enqueue(98.6);

    q.Enqueue(212.0);

    q.Enqueue(32.0);

    q.Enqueue(3.1416);

    double sum = 0.0;

    Console.Write("Queue contents: ");

    while(q.Count > 0) {

      double val = q.Dequeue();

      Console.Write(val + " ");

      sum += val;

    }


    Console.WriteLine("\nTotal is " + sum);

  }


}

The output is shown here:

Queue contents: 98.6 212 32 3.1416

Total is 345.7416

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   982   983   984   985   986   987   988   989   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish