zoukankan      html  css  js  c++  java
  • 数据结构C++实现代码-顺序表

    参考:https://blog.csdn.net/ebowtang/article/details/43094041

    //seqList.h//
    //包含顺序表中的声明//
    
    #include<iostream>
    template<typename DataType>
    class SeqList
    {
    public:
        SeqList(int size = defaultSize)
        {
            if (size > 0)
            {
                maxSize = size;
                length = 0;
                elements = new DataType[maxSize];
                for (int i = 0; i < maxSize; i++)
                {
                    elements[i] = NULL;
                }
    
            }
            else
            {
                cout << "Error Length of SeqList!" << endl;
    
            }
        }
            ~SeqList()
            {
                //delete[] elements;
            }
            bool InsertElement(DataType data);
            DataType GetElement(int location);
            bool DelElement(int location);
            bool ChangeElement(int location, DataType data);
            bool PrintList();
            int FindElement(DataType data);
            bool isEmpty(SeqList L);
            bool InitList(int nLen);
            void ClearList(SeqList *L);
            void DestroyList();
            void ConverseList();
            int getLength()
            {
                return length;
            }
    private:
        static const int defaultSize = 10;
        DataType *elements;
        int maxSize;
        int length;
    };
    //seqList.cpp//
    //包含顺序表中主要函数功能的具体实现//
    #include<iostream>
    #include"seqList.h"
    template <typename DataType>
    bool SeqList<DataType>::InsertElement(DataType data)
    {
        int curIndex = length;
        if (length >= maxSize)
        {
            return false;
        }
        else
        {
            elements[curIndex] = data;
            length++;
            return true;
        }
    }
    
    template <typename DataType>
    bool SeqList<DataType>::InitList(int nLen)
    {
        DataType nchar = 'A';
        for (int i = 0; i < nLen; i++)
        {
            InsertElement(nchar++);
        }
        return true;
    }
    template <typename DataType>
    DataType SeqList<DataType>::GetElement(int location)
    {
        if (location<0 || location>length)
        {
            std::cout << "参数无效" << std::endl;
            return 0;
        }
        else
        {
            return elements[location];
        }
    }
    template <typename DataType>
    bool SeqList<DataType>::DelElement(int location)
    {
        if (location<0 || location >length)
        {
            std::cout << "参数无效" << std::endl;
            return false;
        }
        else
        {
            int j = 0;
            for (int i = location; i < length; i++)
            {
                elements[location + j - 1] = elements[location + j];
                j++;
            }
            length--;
            return true;
        }
    }
    template <typename DataType>
    bool SeqList<DataType>::ChangeElement(int location, DataType data)
    {
        if (location<0 || location>length)
        {
            std::cout << "参数无效" << std::endl;
            return false;
        }
        else
        {
            elements[location - 1] = data;
            return true;
        }
    }
    
    template <typename DataType>
    bool SeqList<DataType>::PrintList()
    {
        for (int i = 0; i < length; i++)
            std::cout << GetElement(i) << " ";
        std::cout<< endl;
        return true;
    }
    template <typename DataType>
    int SeqList<DataType>::FindElement(DataType data)
    {
        for (int i = 0; i < length; i++)
        {
            if (elements[i] == data)
            {
                return i;
            }
        }
        std::cout << "没有更改元素" << std::endl;
        return 0;
    }
    
    template <typename DataType>
    bool SeqList<DataType>::isEmpty(SeqList<DataType> L)
    {
        if (L.length == 0)
            return true;
        else
            return false;
    }
    template <typename DataType>
    void SeqList<DataType>::ClearList(SeqList *L)
    {
        for (int i = 0; i < length; i++)
            elements[i] = 0;
        L->length = 0;
        L->maxSize = 0;
    }
    template <typename DataType>
    void SeqList<DataType>::DestroyList()
    {
        length = 0;
        maxSize = 0;
        delete[] elements;
        elements = NULL;
    }
    template <typename DataType>
    void SeqList<DataType>::ConverseList()
    {
        for (int i = 0; i < length / 2; i++)
        {
            DataType temp;
            temp = elements[i];
            elements[i] = elements[length - 1 - i];
            elements[length - i - 1] = temp;
        }
    }
    //main.cpp//
    #include "seqList.cpp"
    #include "windows.h"
    using namespace std;
    typedef char Mytype;
    int main(int argc, char* argv[])
    {
        int nLen = 0;
        cout << "请输入顺序表的长度: ";
        cin >> nLen;
        SeqList<Mytype>list(nLen);
        list.InitList(nLen);
        cout << "初始化后的内容为l(亲,萌妹纸自动帮您完成的哦!!!)" << endl;
        list.PrintList();
        int nPos = 0;
        cout << "你想删除的那个位置的元素(位置从1开始算起)";
        cin >> nPos;
        while (true)
        {
            if (nPos > list.getLength())
            {
                cout << "输入过大,重新输入(位置从1开始算起)" << endl;
                cin >> nPos;
            }
            else
            {
                break;
            }
        }
        list.DelElement(nPos);
        list.PrintList();
        cout << "现在顺序表的长度为: " << endl;
        cout << list.getLength() << endl;
        Mytype mchar = '0';
        int nPos1 = 0;
        char ans = 'n';
        do
        {
            cout << "请输入您想改变的指定位置和相应元素(示例:“k 5”)";
            cin >> mchar >> nPos1;
            list.ChangeElement(nPos1, mchar);
            cout << "继续修改?(Y/N)" << endl;
            cin >> ans;
        } while (ans == 'y' || ans == 'Y');
        cout << "更改后的顺序表为: " << endl;
        list.PrintList();
        cout << "执行逆序: " << endl;
        list.ConverseList();
        list.PrintList();
        Mytype bchar = '0';
        cout << "请输入您想查找的元素: ";
        cin >> bchar;
        int npos2 = list.FindElement(bchar);
        cout << "您查找的元素的位置为:" << endl;
        cout << npos2 << endl;
        list.ClearList(&list);
        if (list.isEmpty(list) == true)
        {
            cout << "顺序表已被清空" << endl;
    
        }
        else
        {
            cout << "顺序表还原元素" << endl;
    
        }
        cout << "5秒后执行销毁命令....................." << endl;
        Sleep(1000);
        cout << "4秒后执行销毁命令....................." << endl;
        Sleep(1000);
        cout << "3秒后执行销毁命令....................." << endl;
        Sleep(1000);
        cout << "2秒后执行销毁命令....................." << endl;
        Sleep(1000);
        cout << "1秒后执行销毁命令....................." << endl;
        Sleep(1000);
        cout << "再见,谢谢....................." << endl;
        list.DestroyList();
        system("PAUSE");
        return 0;
    }

    代码运行结果图:

    2018-04-2211:13:09

  • 相关阅读:
    EasyUI:combotree(树形下拉框)复选框选中父节点(子节点的状态也全部选中)输入框中只显示父节点的文本值
    js表单插件
    前端模块化入门
    前端模板引擎
    量化策略研究员
    量化策略研究员
    C++ 纯虚函数接口,标准 C 导出 DLL 函数的用法
    一月5日
    一月5日
    一月5日
  • 原文地址:https://www.cnblogs.com/fourmi/p/8906147.html
Copyright © 2011-2022 走看看