码迷,mamicode.com
首页 > 编程语言 > 详细

数据结构03-顺序栈(用C++、C#、lua实现)

时间:2019-11-04 17:11:43      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:vat   walle   bool   ack   font   toc   print   space   destory   




本文为数据结构-顺序栈的代码实现。
作者水平比较差,有错误的地方请见谅。

1、C#实现

栈接口
IStack.cs

    /// <summary>
    /// 栈接口
    /// </summary>
    public interface IStack<T>
    {
        int GetLength(); //求栈的长度
        bool IsEmpty(); //判断栈是否为空
        void Clear(); //清空
        void Push(T item); //入栈
        T Pop(); //出栈
        T Peek(); //取栈顶元素
        void ShowAllElem(); //显示栈中所有元素
    }

顺序栈
SeqStack.cs

class SeqStack<T> : IStack<T>
    {
        private T[] mData;
        /// <summary>
        /// 指向栈顶的索引下标
        /// </summary>
        private int mTop;
        /// <summary>
        /// 栈最大容量
        /// </summary>
        private int mMaxSize;


        public SeqStack(int length)
        {
            mData = new T[length];
            mTop = -1;
            mMaxSize = length;
        }
        //默认栈容量为10
        public SeqStack() : this(10)
        {
        }

        public void Push(T item)
        {
            if (mMaxSize == GetLength())
            {
                Console.WriteLine("栈中元素已满,无法入栈。");
                return;
            }
            mTop++;
            mData[mTop] = item;
        }

        public T Pop()
        {
            if (mTop == -1)
            {
                Console.WriteLine("栈中无元素,无法出栈。");
                return default(T);
            }
            mTop--;
            return mData[mTop+1];
        }

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

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

        public bool IsEmpty()
        {
            if (mTop != -1)
            {
                return false;
            }
            return true;
        }

        public T Peek()
        {
            return mData[mTop];
        }

        public void ShowAllElem()
        {
            for (int i = 0; i <= mTop; i++)
            {
                Console.WriteLine(mData[i]);
            }
        }
    }

Program.cs

    class Program
    {
        static void Main(string[] args)
        {
            SeqStack<string> strStack = new SeqStack<string>(4);

            strStack.Push("111");
            strStack.Push("222");
            strStack.Push("333");
            strStack.Push("444");

            strStack.Clear();
            strStack.Push("111");
            strStack.Push("222");
            strStack.Push("333");
            strStack.Push("444");

            //Console.WriteLine(strStack.IsEmpty());
            //Console.WriteLine(strStack.GetLength());
            //Console.WriteLine(strStack.Pop());
            //Console.WriteLine(strStack.Peek());
            strStack.ShowAllElem();

            Console.ReadKey();
        }
    }

2、C++实现

栈接口
IStack.cpp

#include <iostream>
using namespace std;

typedef int ElemType;

class IStack
{
public :
    ///求栈的长度
    virtual int GetLength() = 0;

    ///判断栈是否为空
    virtual bool IsEmpty() = 0;

    ///清空操作
    virtual void Clear() = 0;

    ///入栈操作
    virtual void Push(ElemType item) = 0;

    ///出栈操作
    virtual ElemType Pop() = 0;

    ///取栈顶元素
    virtual ElemType Peek() = 0;

    ///显示所有元素
    virtual void ShowAllElem() = 0;
};

顺序栈
SeqStack.cpp

#include <iostream>
#include "IStack.cpp"

using namespace std;
typedef int ElemType;

typedef struct{
    ///栈顶指针(指向最上面元素的上一个)、栈底指针(指向最下面的元素)、最大容量
    ElemType* top;
    ElemType* base;
    int maxSize;
}MyStack;

class SeqStack : public IStack
{
private :
    MyStack mStack;

public :
    ///默认栈容量设置为10
    SeqStack();
    SeqStack(int stackSize);

    ~SeqStack();

    int GetLength();

    bool IsEmpty();

    void Clear();

    void Push(ElemType item);

    ElemType Pop();

    ElemType Peek();

    void ShowAllElem();
};

SeqStack::SeqStack()
{
    mStack.base = new ElemType[10];
    if(mStack.base==NULL){
        cout<<"分配空间失败!"<<endl;
        return;
    }
    mStack.top = mStack.base;
    mStack.maxSize = 10;
}

SeqStack::SeqStack(int stackSize)
{
    mStack.base = new ElemType[stackSize];
    if(mStack.base==NULL){
        cout<<"分配空间失败!"<<endl;
        return;
    }
    mStack.top = mStack.base;
    mStack.maxSize = stackSize;
}

SeqStack::~SeqStack()
{
    delete []mStack.base;
    mStack.base = NULL;
    mStack.top = NULL;
}

int SeqStack::GetLength()
{
    return mStack.top - mStack.base;
}

bool SeqStack::IsEmpty()
{
    if(mStack.top == mStack.base){
        return true;
    }
    return false;
}

void SeqStack::Clear()
{
    mStack.top = mStack.base;
}

void SeqStack::Push(ElemType item)
{
    if(GetLength() == mStack.maxSize){
        cout<<"入栈失败,空间已满。"<<endl;
        return;
    }
    *mStack.top = item;
    mStack.top++;
}

ElemType SeqStack::Pop()
{
    if(mStack.top == mStack.base){
        cout<<"出栈失败,空间中无元素。"<<endl;
        return 0;
    }

    mStack.top--;
    return *mStack.top;
}

ElemType SeqStack::Peek()
{
    if(mStack.top == mStack.base){
        cout<<"空间中无元素。"<<endl;
        return 0;
    }
    return *(mStack.top-1);
}

void SeqStack::ShowAllElem()
{
    ElemType* temp = mStack.base;
    while(temp!=mStack.top){
        cout<<*temp<<endl;
        temp++;
    }
}

main.cpp

#include <iostream>
#include "SeqStack.cpp"

using namespace std;

int main()
{
    SeqStack myStack(10);

    myStack.Push(111);
    myStack.Push(222);
    myStack.Push(333);
    myStack.Push(444);

    myStack.Clear();
    myStack.Push(111);
    myStack.Push(222);
    myStack.Push(333);
    myStack.Push(444);

    //cout<<myStack.GetLength()<<endl;
    //cout<<myStack.IsEmpty()<<endl;
    //cout<<myStack.Pop()<<endl;
    //cout<<myStack.Peek()<<endl;

    myStack.ShowAllElem();

    return 0;
}

3、lua实现

--顺序栈和栈顶指针
seqStack = {}
top = 0

--初始化
function seqStack:Init()

end

--销毁
function seqStack:Destory()
    self = nil
    top = nil
end

--清空
function seqStack:Clear()
    top = 0
end

--是否为空
function seqStack:IsEmpty()
    if(top==0) then
        return true
    else
        return false
    end
end

--长度
function seqStack:GetLength()
    return top
end

--入栈
function seqStack:Push(value)
    top = top + 1
    self[top] = value
end

--出栈
function seqStack:Pop()
    if(self:IsEmpty()) then
        print("出栈失败,空间中无元素。");
    else
        top = top - 1
        return self[top+1]
    end
end

--获取栈顶元素
function seqStack:Peek()
    if(self:IsEmpty()) then
        print("空间中无元素。");
    else
        return self[top]
    end
end

--显示所有元素
function seqStack:ShowAllElem()
    for i=1,top,1 do
        print(self[i])
    end
end


--主运行函数
function main()
    seqStack:Init()
    seqStack:Push(111)
    seqStack:Push(222)
    seqStack:Push(333)
    seqStack:Push(444)

    seqStack:Clear();
    seqStack:Push(111);
    seqStack:Push(222);
    seqStack:Push(333);
    seqStack:Push(444);

    --print(seqStack:GetLength())
    --print(seqStack:IsEmpty())
    --print(seqStack:Pop())
    --print(seqStack:Peek())

    seqStack:ShowAllElem()
end

main()

数据结构03-顺序栈(用C++、C#、lua实现)

标签:vat   walle   bool   ack   font   toc   print   space   destory   

原文地址:https://www.cnblogs.com/Fflyqaq/p/11792930.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!