继续发很久以前的那个系列。。。今天是顺序栈。。

接口如下:

public interface IStack<T>
{
    void Clear();
    void Push(T item);
    T Pop();
    T GetTop();
}

SqStack类:

public class SqStack<T> : IStack<T>
{
    private int _Maxsize;
    private T[] _Data;
    private int _Top;

    public T this[int index]
    {
        get
        {
            return _Data[index];
        }
        set
        {
            _Data[index] = value;
        }
    }

    //容量属性
    public int Maxsize
    {
        get
        {
            return _Maxsize;
        }
        set
        {
            _Maxsize = value;
        }
    }
    //栈顶属性
    public int Top
    {
        get
        {
            return _Top;
        }
    }

    //求栈的长度
    public int Length
    {
        get
        {
            return _Top + 1;
        }
    }

    //判断顺序栈是否为空
    public bool IsEmpty
    {
        get
        {
            if (_Top == -1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    //判断顺序栈是否为满
    public bool IsFull
    {
        get
        {
            if (_Top == _Maxsize - 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    //构造器
    public SqStack(int size)
    {
        _Data = new T[size];
        _Maxsize = size;
        _Top = -1;
    }

    //清空顺序栈
    public void Clear()
    {
        _Top = -1;
    }

    //入栈
    public void Push(T item)
    {
        if (IsFull)
        {
            Console.WriteLine("Stack is full");
            return;
        }
        _Data[++_Top] = item;
    }

    //出栈
    public T Pop()
    {
        T tmp = default(T);
        if (IsEmpty) 
        {
            Console.WriteLine("Stack is empty");
            return tmp;
        }
        tmp = _Data[_Top];
        --_Top;
        return tmp;
    }

    //获取栈顶数据元素
    public T GetTop()
    {
        if (IsEmpty)
        {
            Console.WriteLine("Stack is empty!");
            return default(T);
        }
        return _Data[_Top];
    }

}

测试代码:

private static void SqStackTest()
{
    SqStack<string> stk = new SqStack<string>(4);
    string[] s = "A,B,C,D".Split(',');
    foreach (var item in s)
    {
        stk.Push(item);
    }

    Console.WriteLine("SqStack Created:\n---------------------------------");
    PrintSqStack(stk);
    Console.WriteLine();

    Console.WriteLine("Pop Stack:\n---------------------------------");
    PopStack(stk);

    Console.ReadKey();
}

private static void PopStack(SqStack<string> stk)
{

    int fixedLength = stk.Length;
    for (int i = 0; i < fixedLength; i++)
    {
        Console.WriteLine("#{0} - {1} Poped", fixedLength - i, stk.GetTop());
        System.Threading.Thread.Sleep(300);
        stk.Pop();
    }
    Console.WriteLine("The Stack is Empty now.");
}

/// <summary>
/// 打印顺序栈
/// </summary>
/// <param name="stk">顺序栈</param>
private static void PrintSqStack(SqStack<string> stk)
{
    int length = stk.Length;
    for (int i = 0; i < length; i++)
    {
        Console.WriteLine("| {0} |", stk[length - i - 1]);
    }
    Console.WriteLine("|___|");
}

有图有真相: