• [数据结构] 顺序表


    数据结构的练习与巩固
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    1
    //顺序表静态结构 2 #define MaxSize 100 3 typedef int T; 4 typedef struct 5 { 6 T data[MaxSize]; 7 int n; 8 }SeqList;
    1 //顺序表动态结构
    2 typedef int T;
    3 typedef struct
    4 {
    5     T *data;
    6     int n, MaxSize;
    7 }SeqList;
      1 //顺序表(SeqList)类的定义
      2 template <class T>
      3 class SeqList : public LinearList<T>
      4 {
      5 protected:
      6     T* data;        //数据数组
      7     int MaxSize;    //最大容量
      8     int last;       //当前最后元素下标
      9     void reSize(int newSize) //重构容量大小
     10 
     11 public:
     12     SeqList(int size = MaxSize); //构造函数
     13     SeqList(SeqList<T>& L);      //复制构造函数
     14     ~SeqList()                   //析构函数
     15          {delete[] data;}
     16     int Size()const              //求顺序表容量
     17          { return MaxSize; } 
     18     int Length()const            //求顺序表当前长度
     19          { return last + 1; }
     20     int Search(T& x)const;       //查找
     21     int Locate(int i)const;      //定位
     22     bool getData(int i, T &x)      //提取i位置数据
     23          {
     24         if (i > 0 && i <= last + 1) x = data[i - 1];
     25         else return false;
     26          }
     27     void setData(int i, T& x)
     28          {
     29         if (i > 0 && i <= last + 1) data[i - 1] = x;
     30         else return false;
     31          }
     32     int Insert(int i, T& x);     //插入
     33     int Remove(int i, T& x);     //移除
     34     bool IsEmpty()               //是否是空
     35          {return (last == -1) ? true : false;}
     36     bool IsFull()                //是否是满
     37          {return (last == MaxSize - 1) ? true : false;}
     38     void Input();                //输入
     39     void Output();               //输出
     40 
     41     SeqList<T> operator=(SeqList<T>& L);
     42 };
     43 
     44 template <class T>
     45 SeqList<T>::SeqList(int size)
     46 {
     47     if(size>0)
     48     { 
     49         MaxSize = size;        
     50         last = -1;             
     51         data = new T[MaxSize];  
     52         if (data == NULL)       //如果创建的是空指针则提示失败
     53         {
     54             cerr << "存储分配失败!" << endl;
     55             exit(1);
     56         }
     57     }
     58 }
     59 
     60 template <class T>
     61 SeqList<T>::SeqList(SeqList<T>&L)
     62 {
     63     MaxSize = L.Size();
     64     last = L.Length() - 1;
     65     T value;
     66     data = new T[MaxSize];
     67     if (data == NULL)           //如果创建的是空指针则提示失败
     68     {
     69         cerr << "存储分配失败" << endl;
     70         exit(1);
     71     }
     72     for (int i = 1; i <= last + 1; i++)   //持续提取数据并依次赋给新数组
     73     {
     74         L.getData(i, value);
     75         data[i - 1] = value;
     76     }
     77 }
     78 
     79 template <class T>
     80 void SeqList<T>::reSize(int newSize)
     81 {
     82     if (newSize <= 0)           //长度必须大于0
     83     {
     84         cerr << "无效的数组大小" << endl;
     85         return 0;
     86     }
     87     if (newSize != MaxSize)     //如果输入的新大小跟原来一样,则不需操作
     88     {
     89         T* newArray = new T[newSize];
     90         if (newArray == NULL)
     91         {
     92             cerr << "存储分配失败!" << endl;
     93             exit(1);
     94         }
     95         int length = last + 1;  
     96         T* srcptr = data;       //源头指针
     97         T* destptr = newArray;  //目标指针
     98         while (length--)*destptr++ = *srcptr++;  //依次赋值
     99         delete[]data;           //删除原来数据
    100         data = newArray;        //指针指向新数组
    101         MaxSize = newSize;      //大小改为新大小
    102     }
    103 }
    104 
    105 template <class T>
    106 int SeqList<T>::Search(T&x)const
    107 {
    108     for (int i = 0; i <= last; i++)
    109     {
    110         if (data[i] == x)return i + 1; //搜查data为x的位置
    111     }
    112 }
    113 
    114 template <class T>
    115 void SeqList<T>::Insert(T &x,int i)
    116 {
    117     if (last + 1 >= MaxSize || (i<0 || i>last + 1))   //如果插入后长度大于定义的最大长度,或插入位置不在规定范围内
    118         return false;
    119     for (int j = last; j >= i; j--)                   //插入位置后的数据全部后挪一位
    120         data[j + 1] = data[j];
    121     data[i] = x;                                      //将数值赋予该位置的data
    122     last++;                                           //当前长度+1
    123     return true;
    124 }
    125 
    126 template <class T>
    127 SeqList<T>::Remove(int i, T&x)
    128 {
    129     if (last == -1 || i<1 || i>last + 1)              //如果长度为0,或移除位置不在规定范围内
    130         return false;
    131     x = data[i - 1];                                  //提取移除数据
    132     for (int j = i; j <= last; j++)                   //将该位置后的数据全部前移一位
    133         data[j - 1] = data[j];
    134     last--;                                           //当前长度-1
    135     return true;
    136 }
    137 
    138 template <class T>
    139 SeqList<T>::Input()
    140 {
    141     cout << "请输入元素个数";
    142     while (1)
    143     {
    144         cin >> last;
    145         if (last + 1 <= MaxSize)break;
    146         cout << "超出限制";
    147     }
    148     cout << "0:" << endl;
    149     for (int i = 0; i <= last; i++)
    150     {
    151         cin >> data[i];
    152         cout << i + 1 << endl;
    153     }
    154 }
    155 
    156 template <class T>
    157 SeqList<T>::Output()
    158 {
    159     cout << "当前最后元素位置为" << last + 1 << endl;
    160     for (int i = 0; i <= last; i++)
    161     {
    162         cout << "#" << i + 1 << ":" << data[i] << endl;
    163     }
    164 }
  • 相关阅读:
    cmake 基本命令
    webpack4下import()模块按需加载,打包按需切割模块,减少包体积,加快首页请求速度
    内容安全策略(CSP)详解
    内网穿透访问Vue项目的时候出现Invalid Host header解决办法
    webpack3 项目升级 webpack4
    vue-multi-module【多模块集成的vue项目,多项目共用一份配置,可以互相依赖,也可以独立打包部署】
    JS的数据类型判断函数、数组对象结构处理、日期转换函数,浏览器类型判断函数合集
    使用模块化工具打包自己开发的JS库(webpack/rollup)对比总结
    基于AlipayJSBridge封装的H5网页支付宝打赏、网站打赏、个人免签支付,支付宝转账打赏支付组件
    whistle替代Fiddler调试远程服务器代码使用教程
  • 原文地址:https://www.cnblogs.com/nagishiro/p/13132893.html
Copyright © 2020-2023  润新知