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
169
PART IPART I
public char Pop() {
if(tos==0) {
Console.WriteLine(" -- Stack is empty.");
return (char) 0;
}
tos--;
return stck[tos];
}
// 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.
public int Capacity() {
return stck.Length;
}
// Return number of objects currently on the stack.
public int GetNum() {
return tos;
}
}
Let’s examine this class closely. The
Stack
class begins by declaring these two instance
variables:
// These members are private.
char[] stck; // holds the stack
int tos; // index of the top of the stack
The
stck
array provides the underlying storage for the stack, which in this case holds
characters. Notice that no array is allocated. The allocation of the actual array is handled
by the
Do'stlaringiz bilan baham: |