第一、创建接口
包含了栈的所有方法,为统一命名,使用接口,在顺序栈和链栈的具体实现中就继承该接口,以此统一命名。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace case2_Stack
{
interface IStack<T>
{
int Count {
get; }
int GetLength();
bool IsEmpty();
//对栈操作为空类型方法
void Clear();//对栈操作 清空
void Push(T item);//对栈操作 入栈
//需要返回元素,定义为元素类型方法
T Pop();//对元素操作 出栈 读取
T Peek();//对元素操作 读取
}
}
第二、实现顺序栈
class SeqStack<T> : IStack<T>
{
private T[] data;//存储数据
private int top;//指针 指向栈顶
//构造函数
public SeqStack(int size)
{
//可以初始化时,指定栈的大小
data = new T[size];
top = -