zoukankan      html  css  js  c++  java
  • C++顺序表实现

    #include<iostream>
    using namespace std;
    
    template <class T>
    class LinearList
    {
    private:
        T* element;//存储顺序表的数组
        int MaxSize;//顺序表的最大长度
        int length;//顺序表的实际长度
    public:
        //构造函数,若申明顺序表实例时未给出表的最大长度,则最大长度为10
        LinearList(int);
        //析构函数
        ~LinearList(void)
        {
            delete[] element;
        }
        //判空
        bool ListEmpty(void) const
        {
            return length==0;
        }
        //判满
        bool ListFull(void) const
        {
            return length==MaxSize;
        }
        //返回表的长度
        int ListLength(void) const
        {
            return length;
        }
        //存取,将下标为k的节点字段赋值给item
        bool Find(int,T&) const;
        //查找,在表中查找字段值为item的结点并返回下标
        int Search(const T&) const;
        //删除,删除小标为k的节点后并将其字段值赋值给item
        bool Delete(int,T&);
        //插入,在下标为k的节点后插入字段为item的结点
        bool Insert(int,const T&);
        //打印,将顺序表内容按顺序打印到屏幕
        void Print(void);
    };
    //顺序表的构造函数
    template <class T>
    LinearList<T>::LinearList(int MaxListSize=10)
    {
        MaxSize=MaxListSize;
        element=new T[MaxSize];
        length=0;
    };
    //存取:将下表为k的结点的字段值赋值给item并返回true,若不存在返回false
    template <class T>
    bool LinearList<T>::Find(int k,T& item) const
    {
        if(k<0||k>length-1||length==0)
        {
            cout<<"unreasonable position or empty list!"<<endl;
            return false;
        }
        else
        {
            item=element[k];
            return true;
        }
    }
    //查找:在表中查找字段为item的结点并返回其下标;若表中没有item,则返回-1
    template <class T>
    int LinearList<T>::Search(const T& item) const
    {
        for(int i=0; i<length; ++i)
        {
            if(element[i]==item)
                return i;
        }
        return -1;
    }
    //删除:删除表中下标为k的结点,并将其值赋给item
    template <class T>
    bool LinearList<T>::Delete(int k,T& item)
    {
        if(Find(k,item))//若找到下标为k的结点,则将其后面所有结点均向前移动一个位置
        {
            for(int i=k+1; i<length; ++i)
                element[i-1]=element[i];
            length--;//表长度相应减一
            return true;
        }
        else
            return false;
    }
    //插入,在下标为k的结点后插入item
    template <class T>
    bool LinearList<T>::Insert(int k,const T& item)
    {
        if(ListFull())
        {
            cout<<"The List is Full!"<<endl;
            return false;
        }
        else if(k<0||k>length)
        {
            cout<<"The node does not exist!"<<endl;
            return false;
        }
        else
        {
            for(int i=length; i>k; --i)
            {
                element[i]=element[i-1];
            }
            element[k]=item;
            length++;
            return true;
        }
    }
    //打印
    template <class T>
    void LinearList<T>::Print()
    {
        for(int i=0; i<length; ++i)
        {
            cout<<element[i]<<"->";
        }
        cout<<endl;
    }
    //实例演示
    
    int main()
    {
        LinearList<char> list(16);
        char ch='a';
        for(int i=0; i<10; ++i)
        {
            list.Insert(i,ch);
            ch++;
        }
        list.Print();
        list.Delete(0,ch);
        list.Print();
        list.Insert(list.ListLength(),ch);
        list.Print();
        return 0;
    }
    

      

  • 相关阅读:
    mysql错误Error(1133): Can’t find any matching row in the use
    xtrbackup备份mysql
    配置Mysql审计
    Mysql:sql语句
    mysql基准测试与sysbench工具
    tomcat排错以及优化
    nginx反向代理
    Nginx
    服务器归分第一篇
    Android安装BusyBox(三星N7108)
  • 原文地址:https://www.cnblogs.com/liujw2114/p/10400512.html
Copyright © 2011-2022 走看看