using System;
//==========================================================
// Stack< T >
//==========================================================
class Stack< T > {
protected T[] data;
protected int top;
// creates stack of the specified size
public Stack(int size) {
data = new T[size];
top = -1;
}
// pushes the value x on the stack
public void Push(T x) {
if (top == data.Length - 1) throw new Exception("-- stack overflow");
data[++top] = x;
}
// pops the topmost value from the stack
public T Pop() {
if (top < 0) throw new Exception("-- stack underflow");
return data[top--];
}
// returns the stack size
public int Size {
get { return top + 1; }
}
}
//----------------------------------------------------------
// test program; to be called with a list of argument words
//----------------------------------------------------------
class Test {
public static void Main(string[] args) {
//--- use two stacks to store the command line arguments
//--- and their lengths
Stack<string> stringStack = new Stack<string>(10);
Stack<int> intStack = new Stack<int>(10);
foreach (string s in args) {
stringStack.Push(s);
intStack.Push(s.Length);
}
while (stringStack.Size > 0)
Console.WriteLine(stringStack.Pop() + ": " + intStack.Pop());
}
}
|