数据结构05-顺序队列(用C++、C#、lua实现)





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

1、C#实现

队列接口
IQueue.cs

interface IQueue<T>
    {
        int GetLength(); //求队列的长度
        bool IsEmpty(); //判断队列是否为空
        void Clear(); //清空
        void Enqueue(T data); //入队列
        T Dequeue(); //出队列
        T Peek(); //取队列顶元素
        void ShowAllElem(); //显示队列中所有元素
    }

顺序队列
SeqQueue.cs

	class SeqQueue<T> : IQueue<T>
    {
        private T[] mData;
        /// <summary>
        /// rear指向队尾上面一个
        /// </summary>
        private int mRear;
        /// <summary>
        /// front指向队头
        /// </summary>
        private int mFront;
        private int mMaxSize;

        public SeqQueue(int length)
        {
            mData = new T[length];
            mRear = 0;
            mFront = 0;
            mMaxSize = length;
        }

        public int GetLength()
        {
            return mRear - mFront;
        }

        public bool IsEmpty()
        {
			return mRear==mFront;
        }

        public void Enqueue(T data)
        {
            if (mMaxSize == GetLength())
            {
                Console.WriteLine("队列已满,无法再入队列。");
                return;
            }
            mData[mRear++] = data;
        }

        public T Dequeue()
        {
            if (GetLength() == 0)
            {
                Console.WriteLine("队列为空,无法再出队列。");
                return default(T);
            }
            mFront++;
            return mData[mFront-1];
        }

        public T Peek()
        {
            if (GetLength() == 0)
            {
                Console.WriteLine("队列为空,无法再出队列。");
                return default(T);
            }
            return mData[mFront];
        }

        public void Clear()
        {
            mRear = 0;
            mFront = 0;
        }

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

Program.cs

class Program
    {
        static void Main(string[] args)
        {
            #region 顺序队列
            IQueue<string> seqQueue = new SeqQueue<string>(10);
            seqQueue.Enqueue("111");
            seqQueue.Enqueue("222");
            seqQueue.Enqueue("333");
            seqQueue.Enqueue("444");

            seqQueue.Clear();
            seqQueue.Enqueue("111");
            seqQueue.Enqueue("222");
            seqQueue.Enqueue("333");
            seqQueue.Enqueue("444");

            Console.WriteLine(seqQueue.GetLength());
            Console.WriteLine(seqQueue.IsEmpty());
            Console.WriteLine(seqQueue.Dequeue());
            Console.WriteLine(seqQueue.Peek());
            seqQueue.ShowAllElem(); 
            #endregion


            Console.ReadKey();
        }
    }

2、C++实现

队列接口
IQueue.cpp

#include <iostream>
using namespace std;

typedef int ElemType;

class IQueue
{
public:

    ///求队列的长度
    virtual int GetLength() = 0;

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

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

    ///入队列
    virtual void Enqueue(ElemType data) = 0;

    ///出队列
    virtual ElemType Dequeue() = 0;

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

    ///显示队列中所有元素
    virtual void ShowAllElem() = 0;

};

顺序队列
SeqQueue.cpp

#include <iostream>
#include "IQueue.cpp"
using namespace std;

typedef int ElemType;


class SeqQueue:public IQueue
{
private:
    int* mData;
    int mRear;
    int mFront;
    int MaxSize;

public:
    SeqQueue();
    SeqQueue(int length);
    ~SeqQueue();

    int GetLength();

    bool IsEmpty();

    void Clear();

    void Enqueue(ElemType data);

    ElemType Dequeue();

    ElemType Peek();

    void ShowAllElem();

};

SeqQueue::SeqQueue()
{
    //默认数组长度为10
    mData = new ElemType[10];
    mRear = 0;
    mFront = 0;
    MaxSize = 10;
}

SeqQueue::SeqQueue(int length)
{
    mData = new ElemType[length];
    mRear = 0;
    mFront = 0;
    MaxSize = length;
}

SeqQueue::~SeqQueue()
{
    delete []mData;
}

int SeqQueue::GetLength()
{
    return mRear - mFront;
}

bool SeqQueue::IsEmpty()
{
    return mRear == mFront;
}

void SeqQueue::Clear()
{
    mRear = 0;
    mFront = 0;
}

void SeqQueue::Enqueue(ElemType data)
{
    if(GetLength() == MaxSize){
        cout<<"队列已满,无法入队列。"<<endl;
        return;
    }
    mData[mRear++] = data;
}

ElemType SeqQueue::Dequeue()
{
    if(GetLength() == 0){
        cout<<"队列为空,无法出队列。"<<endl;
        return 0;
    }
    mFront++;
    return mData[mFront-1];
}

ElemType SeqQueue::Peek()
{
    if(GetLength() == 0){
        cout<<"队列为空,无法出队列。"<<endl;
        return 0;
    }
    return mData[mFront];
}

void SeqQueue::ShowAllElem()
{
    for(int i=mFront;i<mRear;i++){
        cout<<mData[i]<<endl;
    }
}

main.cpp

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

using namespace std;

typedef int ElemType;

int main()
{
    SeqQueue seqQueue;

    seqQueue.Enqueue(111);
    seqQueue.Enqueue(222);
    seqQueue.Enqueue(333);
    seqQueue.Enqueue(444);

    seqQueue.Clear();
    seqQueue.Enqueue(111);
    seqQueue.Enqueue(222);
    seqQueue.Enqueue(333);
    seqQueue.Enqueue(444);

    cout<<seqQueue.GetLength()<<endl;
    cout<<seqQueue.IsEmpty()<<endl;
    cout<<seqQueue.Dequeue()<<endl;
    cout<<seqQueue.Peek()<<endl;

    seqQueue.ShowAllElem();

    return 0;
}

3、lua实现

--队列
seqQueue = {}
--头指针(指向最后一个元素的上面)
rear = 0
--尾指针(指向第一个元素)
front = 0



--初始化
function seqQueue:Init()
	rear = 0
	front = 0
end

--销毁
function seqQueue:Destory()
	seqQueue = nil
	rear = nil
	front = nil

end

--清空
function seqQueue:Clear()
	rear = 0
	front = 0
end

--是否为空
function seqQueue:IsEmpty()
	return rear==front
end

--长度
function seqQueue:GetLength()
	return rear - front
end

--入队列
function seqQueue:EnQueue(value)
	seqQueue[rear] = value
	rear = rear + 1
end

--出队列
function seqQueue:DeQueue()
	if(self:GetLength() == 0) then
		print("队列为空,无法出队列。");
		return
	end
	front = front + 1
	return seqQueue[front-1]
end

--获取队列顶元素
function seqQueue:Peek()
	if(self:GetLength() == 0) then
		print("队列为空,无法出队列。");
		return
	end
	return seqQueue[front]
end

--显示所有元素
function seqQueue:ShowAllElem()
	for i=front,rear-1,1 do
		print(seqQueue[i])
	end
end


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

	seqQueue:Clear()
    seqQueue:EnQueue(111)
    seqQueue:EnQueue(222)
    seqQueue:EnQueue(333)
    seqQueue:EnQueue(444)

    --print(seqQueue:GetLength())
    --print(seqQueue:IsEmpty())
    --print(seqQueue:DeQueue())
    --print(seqQueue:Peek())

	seqQueue:ShowAllElem()
end

main()


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