C# 0 The Complete Reference


tos  to zero. Thus, a zero value in  tos



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

tos

 to zero. Thus, a zero value in 



tos

 indicates that the stack is empty.

The public 

Push( )

 method puts an element onto the stack. It is shown here:

// Push characters onto the stack.

public void Push(char ch) {

  if(tos==stck.Length) {

    Console.WriteLine(" -- Stack is full.");

    return;

  }


  stck[tos] = ch;

  tos++;


}

The element to be pushed onto the stack is passed in 



ch

. Before the element is added to 

the stack, a check is made to ensure that there is still room in the underlying array. This is 

done by making sure that 



tos

 does not exceed the length of 



stck

. If there is still room, the 

element is stored in 

stck

 at the index specified by 



tos

, and then 



tos

 is incremented. Thus, 



tos

 always contains the index of the next free element in 



stck

.

To remove an element from the stack, call the public method 



Pop( )

. It is shown here:

// Pop a character from the stack.

public char Pop() {

  if(tos==0) {

    Console.WriteLine(" -- Stack is empty.");

    return (char) 0;

  }


  tos--;

  return stck[tos];

}

Here, the value of 



tos

 is checked. If it is zero, the stack is empty. Otherwise, 



tos

 is 


decremented, and the element at that index is returned.

Although


Push( )

 and 


Pop( )

 are the only methods needed to implement a stack, some 

others are quite useful, and the 

Stack

 class defines four more. These are 



IsFull( )

,

IsEmpty( )

,

Capacity( )

, and 


GetNum( )

, and they provide information about the state of the stack. They 

are shown here:

// Return true if the stack is full.

public bool IsFull() {

  return tos==stck.Length;

}

// Return true if the stack is empty.



public bool IsEmpty() {

  return tos==0;

}

// Return total capacity of the stack.



www.freepdf-books.com


Download 4,07 Mb.

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