zoukankan      html  css  js  c++  java
  • C# 顺序栈

        public interface IStack<T>
    {
    int GetLength();
    bool IsEmpty();
    bool IsFull();
    void Clear();
    void Push(T item);
    T Pop();
    T GetTop();
    }


    public class Stack<T> : IStack<T>
    {
    private int size;
    private T[] data;
    private int top;

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

    public int GetSize
    {
    get
    {
    return size;
    }
    set
    {
    size = value;
    }
    }

    public int Top
    {
    get
    {
    return top;
    }
    }

    public Stack(int size)
    {
    data = new T[size];
    top = -1;
    this.size = size;
    }

    public int GetLength()
    {
    return top + 1;
    }

    public bool IsEmpty()
    {
    if (top == -1)
    {
    return true;
    }
    else
    {
    return false;
    }
    }

    public bool IsFull()
    {
    if (top == size -1)
    {
    return true;
    }
    else
    {
    return false;
    }
    }

    public void Clear()
    {
    top = -1;
    }

    public void Push(T item)
    {
    if (IsFull())
    {
    throw new Exception("Stack is full, can't push any item");
    }
    else
    {
    data[++top] = item;
    }
    }

    public T Pop()
    {
    T temp = data[top];
    if (IsEmpty())
    {
    throw new Exception("Stack is empty, can't pop out anything.");
    }
    top--;
    return temp;
    }

    public T GetTop()
    {
    if (IsEmpty())
    {
    throw new Exception("Stack is empty, can't get top item.");
    }
    return data[top];
    }
    }
  • 相关阅读:
    软件体系结构课后作业03
    Struts2初步认识
    springboot1
    pytorch安装
    classifier of chains
    python错误集锦
    svm-惩罚因子
    毕设笔记1----div样式
    SQLite
    MVC实例应用模式
  • 原文地址:https://www.cnblogs.com/Ligeance/p/2391659.html
Copyright © 2011-2022 走看看