C# 0 The Complete Reference


PART I C h a p t e r   8



Download 4,07 Mb.
Pdf ko'rish
bet241/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   237   238   239   240   241   242   243   244   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART I

C h a p t e r   8 :  

A   C l o s e r   L o o k   a t   M e t h o d s   a n d   C l a s s e s  

171


PART IPART I

public int Capacity() {

  return stck.Length;

}

// Return number of objects currently on the stack.



public int GetNum() {

  return tos;

}

The


IsFull( )

 method returns 



true

 when the stack is full and 



false

 otherwise. The 



IsEmpty( )

method returns 



true

 when the stack is empty and 



false

 otherwise. To obtain the total capacity 

of the stack (that is, the total number of elements it can hold), call 

Capacity( )

. To obtain the 

number of elements currently stored on the stack, call 

GetNum( )

. These methods are useful 

because the information they provide requires access to 

tos

, which is private. They are also 

examples of how public methods can provide safe access to private members.

The following program demonstrates the stack:

// Demonstrate the Stack class.

using System;

class StackDemo {

  static void Main() {

    Stack stk1 = new Stack(10);

    Stack stk2 = new Stack(10);

    Stack stk3 = new Stack(10);

    char ch;

    int i;

    // Put some characters into stk1.

    Console.WriteLine("Push A through J onto stk1.");

    for(i=0; !stk1.IsFull(); i++)

      stk1.Push((char) ('A' + i));

    if(stk1.IsFull()) Console.WriteLine("stk1 is full.");

    // Display the contents of stk1.

    Console.Write("Contents of stk1: ");

    while( !stk1.IsEmpty() ) {

      ch = stk1.Pop();

      Console.Write(ch);

    }


    Console.WriteLine();

    if(stk1.IsEmpty()) Console.WriteLine("stk1 is empty.\n");

    // Put more characters into stk1.

    Console.WriteLine("Again push A through J onto stk1.");

    for(i=0; !stk1.IsFull(); i++)

      stk1.Push((char) ('A' + i));

    // Now, pop from stk1 and push the element in stk2.

www.freepdf-books.com




172

 

P a r t   I :  



T h e   C #   L a n g u a g e

    // This causes stk2 to hold the elements in reverse order.

    Console.WriteLine("Now, pop chars from stk1 and push " +

                      "them onto stk2.");

    while( !stk1.IsEmpty() ) {

      ch = stk1.Pop();

      stk2.Push(ch);

    }


    Console.Write("Contents of stk2: ");

    while( !stk2.IsEmpty() ) {

      ch = stk2.Pop();

      Console.Write(ch);

    }

    Console.WriteLine("\n");



    // Put 5 characters into stack.

    Console.WriteLine("Put 5 characters on stk3.");

    for(i=0; i < 5; i++)

      stk3.Push((char) ('A' + i));

    Console.WriteLine("Capacity of stk3: " + stk3.Capacity());

    Console.WriteLine("Number of objects in stk3: " +

                      stk3.GetNum());

  }


}

The output from the program is shown here:

Push A through J onto stk1.

stk1 is full.

Contents of stk1: JIHGFEDCBA

stk1 is empty.

Again push A through J onto stk1.

Now, pop chars from stk1 and push them onto stk2.

Contents of stk2: ABCDEFGHIJ

Put 5 characters on stk3.

Capacity of stk3: 10

Number of objects in stk3: 5




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   237   238   239   240   241   242   243   244   ...   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