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





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

1、C#实现

使用数组和泛型来自己实现类似List集合效果。

IListDS.cs

	/// <summary>
    /// 线性表接口
    /// </summary>
    interface IListDS<T>
    {
        int GetLength(); //求表长度
        void Clear(); //清空
        bool IsEmpty(); //判断表是否为空
        void Add(T item); //添加
        void Insert(T item, int index); //插入
        T PriorElem(int index);  //求前驱
        T NextElem(int index);  //求后继
        T Delete(int index); //删除
        T GetElem(int index); //取表元素
        T this[int index] { get; }//根据索引器 获取表中元素
        int Locate(T value); //按值查找,返回值下标
        void ShowAllElem();  //显示所有元素
    }

SeqList.cs

	/// <summary>
    /// 顺序表
    /// </summary>
    class SeqList<T> : IListDS<T>
    {
        private T[] mData;
        private int mCount;

        /// <summary>
        /// 初始化时可以给顺序表长度赋初值
        /// </summary>
        public SeqList(int size)
        {
            mData = new T[size];
            mCount = 0;
        }

        //当未给数组长度赋初值时,默认长度为10
        public SeqList():this(10)
        {
        }

        public void Add(T item)
        {
            if (mCount == mData.Length)
            {
                Console.WriteLine("顺序表已满,不可再存储。");
                return;
            }
            mData[mCount] = item;
            mCount++;
        }

        public int GetLength()
        {
            return mCount;
        }

        public T GetElem(int index)
        {
            if (index >= 0 && index < mCount)
            {
                return mData[index];
            }
            Console.WriteLine("索引值超出范围");
            return default(T);
        }

        public T this[int index]
        {
            get
            {
                return GetElem(index);
            }
        }

        public bool IsEmpty()
        {
            return mCount == 0;
        }

        /// <summary>
        /// 清空顺序表
        /// </summary>
        public void Clear()
        {
            mCount = 0;
        }

        public T Delete(int index)
        {
            T deleteT = mData[index];
            if (index >= 0 && index < mCount)
            {
                for (int i = index; i < mCount - 1; i++)
                {
                    mData[i] = mData[i + 1];
                }
                mCount--;
                return deleteT;
            }

            Console.WriteLine("索引值超出范围");
            return default(T);
        }

        public void Insert(T item, int index)
        {
            if (index >= 0 && index <= mCount)
            {
                for (int i = mCount; i > index; i--)
                {
                    mData[i] = mData[i - 1];
                }
                mCount++;
                mData[index] = item;
                return;
            }

            Console.WriteLine("插入位置有误");
        }

        public T PriorElem(int index)
        {
            if (index ==0)
            {
                Console.WriteLine("第一个元素无前驱。"); return default(T);
            }
            if (index < 0 || index >= mCount)
            {
                Console.WriteLine("索引范围有误。"); return default(T);
            }
            return mData[index - 1];
        }

        public T NextElem(int index)
        {
            if (index == mCount-1)
            {
                Console.WriteLine("最后一个元素无后继。"); return default(T);
            }
            if (index < 0 || index >= mCount)
            {
                Console.WriteLine("索引范围有误。"); return default(T);
            }
            return mData[index + 1];
        }

        public int Locate(T value)
        {
            for (int i = 0; i < mCount; i++)
            {
                if (mData[i].Equals(value))
                {
                    return i;
                }
            }
            Console.WriteLine(value + "在顺序表中不存在");
            return -1;
        }

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

Program.cs

	class Program
    {
        static void Main(string[] args)
        {
            IListDS<string> strList = new SeqList<string>(8);

            //测试
            strList.Add("aaa");
            strList.Add("bbb");
            strList.Add("ccc");
            strList.Add("ddd");
            //Console.WriteLine(strList.GetLength());
            //Console.WriteLine(strList.GetElem(2));
            //Console.WriteLine(strList[3]);
            //Console.WriteLine(strList.IsEmpty());
            //strList.Insert("111",4);
            //Console.WriteLine(strList.Locate("cccc"));
            //Console.WriteLine(strList.PriorElem(2));
            //Console.WriteLine(strList.NextElem(2));
			strList.ShowAllElem();

            Console.ReadKey();
        }
    }

2、C++实现

线性表接口
IList.cpp

#include <iostream>
using namespace std;

typedef int ElemType;

class IList
{
public:
    ///清空
    virtual void Clear() = 0;

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

    ///长度
    virtual int GetLength() = 0;

    ///根据下标取表元素
    virtual int GetElem(int index) = 0;

    ///返回相同元素的位置
    virtual int LocateElem(ElemType value) = 0;

    ///求前驱
    virtual int PriorElem(int index) = 0;

    ///求后继
    virtual int NextElem(int index) = 0;

    ///添加元素
    virtual void Add(ElemType value) = 0;

    ///插入元素
    virtual void Insert(ElemType value,int index) = 0;

    ///删除元素
    virtual void Delete(int index) = 0;

    ///显示线性表中元素
    virtual void ShowAllList() = 0;

};

顺序表类
SeqList.cpp

#include <iostream>
#include <stdlib.h>
#include <math.h>
#include "IList.cpp"
using namespace std;

typedef int ElemType;

class SeqList:public IList
{
private:
    ElemType *elem;
    int length;
    int listSize;

public:
    ///顺序表初始化
    SeqList(int listSize);
    ///顺序表销毁
    ~SeqList();

    void Clear();

    bool IsEmpty();

    int GetLength();

    ElemType GetElem(int index);

    ElemType LocateElem(ElemType value);

    ElemType PriorElem(int index);

    ElemType NextElem(int index);

    void Add(ElemType value);

    void Insert(ElemType value,int index);

    void Delete(int index);

    void ShowAllList();

};

SeqList::SeqList(int s)
{
    elem = new int(s);
    if (!elem) {
        //溢出时报错
		exit(OVERFLOW);
	}
    length = 0;
    listSize = s;
}

SeqList::~SeqList()
{
    if(elem){
        //销毁new的空间
        delete []elem;
    }
    length = 0;
    listSize = 0;
}

void SeqList::Clear()
{
    length = 0;
}

bool SeqList::IsEmpty()
{
    return length==0;
}


int SeqList::GetLength()
{
    return length;
}

ElemType SeqList::GetElem(int index)
{
    if(index>=0 && index<length){
        return elem[index];
    }
    cout<<"索引超出范围!"<<endl;
    return -1;
}

int SeqList::LocateElem(ElemType value)
{
    for(int i=0;i<length;i++){
        if(elem[i] == value){
            return i;
        }
    }
    cout<<value<<"在顺序表中不存在!"<<endl;
    return -1;
}


ElemType SeqList::PriorElem(int index)
{
    if(index==0){
        cout<<"第一个元素无前驱!"<<endl;
        return -1;
    }

    if(index<0 || index>=length){
        cout<<"索引超出范围!"<<endl;
        return -1;
    }

    return elem[index-1];
}


ElemType SeqList::NextElem(int index)
{
    if(index==length-1){
        cout<<"最后一个元素无后继!"<<endl;
        return -1;
    }

    if(index<0 || index>length-1){
        cout<<"索引超出范围!"<<endl;
        return -1;
    }

    return elem[index+1];
}

void SeqList::Add(ElemType value)
{
    if(length == listSize/sizeof(int)){
        cout<<"顺序表已满!"<<endl;
        return;
    }
    elem[length] = value;
    length++;
}

void SeqList::Insert(ElemType value,int index)
{
    if(index<0 || index>length){
        cout<<"索引超出范围!"<<endl;
        return;
    }

    if(length == listSize/sizeof(int)){
        cout<<"顺序表已满!"<<endl;
        return;
    }

    for(int i=length;i>index;i--){
        elem[i] = elem[i-1];
    }
    elem[index] = value;
    length++;
}


void SeqList::Delete(int index)
{
    if(index<0 || index>length-1){
        cout<<"索引超出范围!"<<endl;
        return;
    }

    for(int i=index;i<length-1;i++){
        elem[i] = elem[i+1];
    }
    length--;
}

void SeqList::ShowAllList()
{
    for(int i=0 ; i<length ; i++)
        cout<<elem[i]<<endl;
}


main.cpp

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

using namespace std;
typedef int ElemType;

int main()
{
    SeqList l(100);

    ///测试
    l.Add(111);
    l.Add(222);
    l.Add(333);
	l.Add(444);

    //l.Insert(123,3);
    //l.Delete(2);
    //cout<<l.NextElem(2)<<endl;
    //cout<<l.PriorElem(2)<<endl;
    //cout<<l.GetElem(2)<<endl;
    //cout<<l.IsEmpty()<<endl;
    //cout<<l.LocateElem(222)<<endl;
    //cout<<l.GetLength()<<endl;

    //l.Clear();

    l.ShowAllList();


    return 0;
}

3、lua实现

sqList = {}

--初始化
function sqList:Init()
	--顺序表长度,空的顺序表
	self.length = 0
	self.list = {}
end

--销毁
function sqList:Destory()
	self.length = nil
	self.list = nil
end

--清空
function sqList:Clear()
	self.length = 0
end

--长度
function sqList:GetLength()
	return self.length
end

--添加元素
function sqList:Add(value)
	--lua表下标从1开始
	--就算给[0]赋值了,访问[0]也会返回nil
	self.length = self.length + 1

	self.list[self.length] = value
end

--取表元素
function sqList:GetElem(index)
	if(index>=1 and index<=self.length) then
		return self.list[index]
	end
	print("索引范围有误")
end

--按值查找,返回下标
function sqList:Locate(value)
	for i = 1,self.length,1 do
		if(self.list[i] == value) then
			return i
		end
	end

	print(value .. "在顺序表中不存在")
end

--前驱
function sqList:PriorElem(index)
	if(index == 1) then
		print("第一个元素无前驱")
		return
	end

	if(index>1 and index<=self.length) then
		return self.list[index-1]
	end

	print("索引范围有误")
end

--后继
function sqList:NextElem(index)
	if(index == self.length) then
		print("最后一个元素无后继")
		return
	end

	if(index>=1 and index<self.length) then
		return self.list[index+1]
	end

	print("索引范围有误")
end

--插入
function sqList:Insert(value,index)
	if(index<1 or index>self.length+1) then
		print("选择插入范围有误")
		return
	end

	for i=self.length+1,index+1,-1 do
		self.list[i] = self.list[i-1]
	end
	self.list[index] = value
	self.length = self.length+1
end

--删除
function sqList:Delete(index)
	if(index<1 or index>self.length) then
		print("选择删除范围有误")
		return
	end

	for i=index,self.length-1,1 do
		self.list[i] = self.list[i+1]
	end
	self.length = self.length-1
end

--显示所有元素
function sqList:ShowAllElem()
	for i = 1,self.length,1 do
		print(self.list[i])
	end
end


--主运行函数
function main()

	--测试
	sqList:Init()
	sqList:Add(111)
	sqList:Add(222)
	sqList:Add(333)
	sqList:Add(444)

	--sqList:ShowAllElem()
	--print(sqList:GetLength())
	--print(sqList:GetElem(2))
	--print(sqList:Locate(333))
	--print(sqList:PriorElem(2))
	--print(sqList:NextElem(2))

	--sqList:Insert(999,5);
	--sqList:Delete(4);
	sqList:ShowAllElem()
end

main()
原文地址:https://www.cnblogs.com/Fflyqaq/p/11775866.html