zoukankan      html  css  js  c++  java
  • 数据结构——顺序表及其操作

      1 #include<iostream>
      2 
      3 using namespace std;
      4 
      5 #define MAXSIZE 100        //最大长度
      6 #define OK 1
      7 #define ERROR -1
      8 
      9 
     10 //类型重命名
     11 typedef int Elemtype;
     12 typedef int Status;
     13 
     14 
     15 //顺序表的存储结构
     16 typedef struct 
     17 {
     18     Elemtype *elem;        //基地址
     19     int length;            //顺序表长度
     20 }SqList;                //顺序表的结构类型为SqList
     21 
     22 
     23 //顺序表的初始化---即构造一个空的顺序表
     24 Status InitList(SqList &L)
     25 {
     26     L.elem = new Elemtype[MAXSIZE];        //为顺序表分配一个大小为MAXSIZE的数组空间
     27 
     28     if (!L.elem)
     29         return ERROR;        //存储分配失败退出
     30 
     31     L.length = 0;            //初始化为空表,长度为0
     32 
     33     return OK;
     34 }
     35 
     36 
     37 //顺序表的创建
     38 Status CreateList(SqList &L)
     39 {
     40     int a, i = 0;
     41 
     42     cin >> a;        //表元素的输入
     43 
     44     while (a != -1)
     45     {
     46         if (L.length == MAXSIZE)
     47         {
     48             cout << "顺序表已满!";
     49             return ERROR;
     50         }
     51 
     52         L.elem[i++] = a;
     53         L.length++;
     54         cin >> a;    //迭代输入顺序表元素
     55     }
     56     return OK;
     57 }
     58 
     59 
     60 //取值
     61 //获取顺序表中第i个数据元素的值,赋值给e并返回
     62 Status GetElem(SqList L, int i, Elemtype &e)
     63 {
     64     //判断i值是否合理
     65     if (i<1 || i>L.length)
     66         return ERROR;
     67 
     68     e = L.elem[i - 1];    //elem[i-1]存储第i个数据元素 (注意是 i-1 !!!
     69 
     70     return OK;
     71 }
     72 
     73 
     74 //查找
     75 //查找顺序表中第1个与e相等的元素,若查找成功,则返回该元素在表中的位置序号
     76 int LocateElem(SqList L, Elemtype e)
     77 {
     78     int i;
     79 
     80     for (i = 0; i < L.length; i++)
     81     {
     82         if (L.elem[i] == e)        //查找成功,返回序号i+1 (注意位置序号是 i+1 !!!
     83             return i + 1;
     84     }
     85     return ERROR;    //查找失败
     86 }
     87 
     88 
     89 //插入
     90 //在表的第i个位置插入一个新的数据元素e
     91 Status ListInsert(SqList &L, int i, Elemtype e)
     92 {
     93     //判断i值是否合法 (注意此处是L.length + 1。因为插入一个元素后表长加1
     94     if( (i<1) || (i > L.length + 1) )
     95         return ERROR;
     96 
     97     if (L.length == MAXSIZE)    //溢出
     98         return ERROR;    
     99 
    100     for (int j = L.length - 1; j >= i - 1; j--)
    101     {
    102         L.elem[j + 1] = L.elem[j];    //从最后一个元素开始后移,直到第i个元素后移
    103     }
    104 
    105     L.elem[i - 1] = e;    //将新的数据元素放在第i个位置上
    106     L.length++;            //表长加1
    107 
    108     return OK;
    109 }
    110 
    111 
    112 //删除
    113 //将表的第i个元素删除
    114 Status ListDelete(SqList &L, int i, int &e)
    115 {
    116     //判断i值是否合法
    117     if ((i < 1) || (i > L.length))
    118         return ERROR;
    119 
    120     e = L.elem[i - 1];    //将要删除的元素保留在e中,利于删除信息的找回
    121 
    122     for (int j = i; j <= L.length - 1; j++)
    123     {
    124         L.elem[j - 1] = L.elem[j];    //要删除元素之后的元素前移
    125     }
    126 
    127     L.length--;        //表长减1
    128 
    129     return OK;
    130 }
    131 
    132 
    133 //顺序表的输出
    134 void print(SqList L)
    135 {
    136     cout << "输出顺序表" << endl;
    137     for (int i = 0; i < L.length; i++)
    138     {
    139         cout << L.elem[i] << " ";
    140     }
    141     cout << "
    ";
    142 }
    143 
    144 
    145 //顺序表的销毁
    146 void DestroyList(SqList &L)
    147 {
    148     if (L.elem)
    149         delete[]L.elem;
    150 }
    151 
    152 
    153 int main()
    154 {
    155     SqList myL;
    156     int i, e;
    157 
    158     cout << "1. 初始化
    ";
    159 
    160     cout << "2. 创建
    ";
    161 
    162     cout << "3. 取值
    ";
    163 
    164     cout << "4. 查找
    ";
    165 
    166     cout << "5. 插入
    ";
    167 
    168     cout << "6. 删除
    ";
    169 
    170     cout << "7. 输出
    ";
    171 
    172     cout << "8. 销毁
    ";
    173 
    174     cout << "0. 退出
    ";
    175 
    176     int choose = -1;
    177 
    178     while (choose != 0)
    179     {
    180         cout << "请选择:" << endl;
    181 
    182         cin >> choose;
    183 
    184         switch (choose)
    185         {
    186         case 1:    //初始化
    187             cout << "顺序表初始化..." << endl;
    188 
    189             if (InitList(myL))
    190                 cout << "顺序表初始化成功!" << endl;
    191 
    192             else
    193                 cout << "顺序表初始化失败!" << endl;
    194 
    195             break;
    196 
    197         case 2:    //创建
    198             cout << "顺序表创建..." << endl;
    199 
    200             cout << "输入整型数,输入-1结束" << endl;
    201 
    202             if (CreateList(myL))
    203                 cout << "顺序表创建成功!" << endl;
    204 
    205             else
    206                 cout << "顺序表创建失败!" << endl;
    207 
    208             break;
    209 
    210         case 3:    //取值
    211             cout << "输入整型数i,取第i个元素输出:" << endl;
    212 
    213             cin >> i;
    214 
    215             if (GetElem(myL, i, e))
    216                 cout << "第i个元素是:" << e << endl;
    217 
    218             else
    219                 cout << "取值失败!" << endl;
    220 
    221             break;
    222         case 4:    //查找
    223             cout << "请输入要查找的数e:";
    224 
    225             cin >> e;
    226 
    227             if (LocateElem(myL, e) == -1)
    228                 cout << "查找失败!" << endl;
    229 
    230             else
    231             {
    232                 cout << "查找成功!" << endl;
    233                 cout << "查找数的位置为:" << LocateElem(myL, e) << endl;
    234             }
    235 
    236             break;
    237         case 5:    //插入
    238             cout << "请输入要插入的位置和要插入的数据元素值e:";
    239 
    240             cin >> i >> e;
    241 
    242             if (ListInsert(myL, i, e))
    243                 cout << "插入成功!" << endl;
    244 
    245             else
    246                 cout << "插入失败!" << endl;
    247 
    248             break;
    249         case 6:    //删除
    250             cout << "请输入要删除的位置i:";
    251 
    252             cin >> i;
    253 
    254             if (ListDelete(myL, i, e))
    255                 cout << "删除成功!" << endl;
    256 
    257             else
    258                 cout << "删除失败!" << endl;
    259 
    260             break;
    261         case 7:    //输出
    262             print(myL);
    263 
    264             break;
    265         case 8:    //销毁
    266             cout << "顺序表销毁..." << endl;
    267 
    268             DestroyList(myL);
    269 
    270             cout << "顺序表销毁成功!" << endl;
    271             break;
    272         }
    273     }
    274     return 0;
    275 }
  • 相关阅读:
    小小的封装了一个pie的echarts
    recent.css常用的页面初始化样式
    bootstrap表格多样式及代码
    Java内存回收机制
    栈帧
    互斥锁和条件变量
    UML类图的常见关系
    堆栈详解(数据与内存中的存储方式)
    PR消减视频中的鼠标声
    SmartPlant Review 三维视图快捷键
  • 原文地址:https://www.cnblogs.com/friend-A/p/9036551.html
Copyright © 2011-2022 走看看