zoukankan      html  css  js  c++  java
  • 2、顺序表

    |   版权声明:本文为博主原创文章,未经博主允许不得转载。

       

      从这节开始,以后的每一篇博文均会只描述一种结构。这节要解决的是有关顺序表的问题,下面就是一些顺序表的基本的知识点:

            1. 顺序表其实就是线性表的一种,它除开始节点和结束节点之外的其他节点,均有且只有一个直接前趋和一个直接后继,开始

    节点只有一个后继节点,结束节点只有一个前趋节点。

            2. 用顺序存储方法存储的线性表就称为顺序表。

            3. 顺序存储方法就是将表的节点按逻辑上的次序依次的存放在一组连续的内存单元中,这里是指在逻辑上连续排列,在物理的

    存储单元上也连续的排列。

            4. 在顺序表中,因为顺序表的每一个单元均是同种类型的数据,因此它们的每一个的节点的内存单元的空间是一样大的,我们

    可以根据下面的公式,来求得某个节点的存储地址:

                                          

    >>.分步代码

      1>.  顺序表的结构体定义(代码如下):

             

                 在结构体中,有顺序表的当前容量,有顺序表的首地址listData,还要有顺序表的总大小listSize。

      2>.  顺序表的初始化(代码如下):

             

         3>.  顺序表的表长(代码如下):

              

         4>.  插入元素

             

       5>.  追加空间

             

         

         6>.  判断顺序表是否为空

             

       7>.  判断顺序表是否已满

            

        8>.  查找某个元素

             

        9>.  打印顺序表

             

        10>. 查找某个元素的前趋

             

       

       11>. 查找某个元素的后继

             

       12>. 在pos位置插入元素

             

       13>. 在pos位置删除元素

             

       14>. 销毁整个顺序表

             

       15>. 将当前表长至为0

             

    >>.完整代码

      1 /******************************************************
      2 *                   数据结构-顺序表                     *
      3 *                                                     *
      4 *                      2016/7/25                      *
      5 *                                         by Lipei    *
      6 ******************************************************/
      7 
      8 #include <iostream>
      9 #include <stdio.h>
     10 #include <stdlib.h>
     11 using namespace std;
     12 
     13 #define OK                1
     14 #define ERROR            0
     15 #define OVERFLOW        -1
     16 //顺序表初次分配空间的大小
     17 #define INIT_SIZE        100
     18 //顺序表追加空间的大小
     19 #define APPEND_SIZE        10
     20 
     21 typedef int STATUS;
     22 typedef int ELEMTYPE;
     23 
     24 //顺序表的结构体定义
     25 typedef struct
     26 {
     27     //顺序表中的元素
     28     ELEMTYPE* listData;
     29     //顺序表的当前的长度
     30     int length;
     31     //顺序表的分配的总大小
     32     int listSize;
     33 }orderList;
     34 
     35 //追加空间(在空间不足的情况之下)
     36 void appendListSpace(orderList& list)
     37 {
     38     ELEMTYPE* newList;
     39     newList = (ELEMTYPE *) realloc(list.listData, (list.listSize+APPEND_SIZE)*sizeof(ELEMTYPE));
     40     if(!newList)
     41     {
     42         exit(OVERFLOW);
     43     }
     44     else
     45     {
     46         list.listData = newList;
     47         list.listSize = list.listSize + APPEND_SIZE;
     48     }
     49 }
     50 
     51 //初始化顺序表
     52 //'*'号改成'&'号也行,将指针改成引用,下面对应的'->'则要变成'.'
     53 STATUS initOrderList(orderList* list)        
     54 {
     55     //其实在这里也可以使用C++中的动态内存的分配new关键字。此处个人习惯喜欢使用malloc
     56     list->listData = (ELEMTYPE *) malloc(INIT_SIZE * sizeof(ELEMTYPE));
     57     //判断空间分配是否成功
     58     if(!list->listData)
     59     {
     60         //分配失败
     61         cout << "空间分配失败!!!" << endl;
     62         exit(OVERFLOW);
     63     }
     64     else
     65     {
     66         //初始化当前的表长
     67         list->length = 0;
     68         //初始化表的总大小
     69         list->listSize = INIT_SIZE;
     70     }
     71     return OK;
     72 }
     73 
     74 //判断顺序表是否为空
     75 bool isEmpty(orderList& list)
     76 {
     77     //如果顺序表的当前长度为0,则说明顺序表为空
     78     if(list.length == 0)
     79     {
     80         return true;
     81     }
     82     else
     83     {
     84         return false;
     85     }
     86 }
     87 
     88 //判断顺序表是否已满
     89 bool isFull(orderList& list)
     90 {
     91     //如果顺序表的当前长度已经等于了顺序表的大小,则表明顺序表已满
     92     if(list.length >= list.listSize)
     93     {
     94         return true;
     95     }
     96     else
     97     {
     98         return false;
     99     }
    100 }
    101 
    102 //表头元素
    103 void insertList(orderList* list, ELEMTYPE elem)
    104 {
    105     //在插入元素之前,首先应该判断顺序表是否已满
    106     if(isFull(*list))
    107         appendListSpace(*list);
    108     list->listData[list->length] = elem;
    109     list->length++;
    110     return;
    111 }
    112 
    113 //取得当前顺序表的容量(及在当前的情况下,顺序表中有几个元素)
    114 STATUS getListLength(orderList* list)
    115 {
    116     return list->length;
    117 }
    118 
    119 //遍历整个顺序表(使用对象来实现)
    120 void visitList(orderList list)
    121 {
    122     int i = 0;
    123     cout << "List Data:  ";
    124     for(; i < list.length; i++)
    125     {
    126         cout << list.listData[i] << ", ";
    127     }
    128     cout << endl;
    129 }
    130 
    131 //查找某个元素是否存在
    132 STATUS findElem(orderList* list, ELEMTYPE elem)
    133 {
    134     //在查找元素之前,判断顺序表是否为空
    135     if(isEmpty(*list))
    136     {
    137         cout << "orderList is empty!" << endl;
    138     }
    139     int i;
    140     for(i=0; i<list->length; i++)
    141     {
    142         if(list->listData[i]==elem)
    143         {
    144             cout << "The orderList of elems exist in table " << list->listData[i] << " !" << endl;
    145             return OK;
    146         }
    147     }
    148     cout << "The orderList is not "  << elem << " elem!" << endl;
    149     return ERROR;
    150 }
    151 
    152 //查找某个元素的前趋
    153 STATUS getFrontElem(orderList list, ELEMTYPE elem)
    154 {
    155     //查找元素前判断顺序表是否为空
    156     if(isEmpty(list))
    157         return OVERFLOW;
    158     for(int i=0; i<list.length; i++)
    159     {
    160         if(elem==list.listData[i])
    161         {
    162             //注意是否为开始节点,开始节点是没有前趋的。
    163             if(i == 0)
    164             {
    165                 cout << "开始节点不存在前趋!" << endl;
    166                 return OVERFLOW;
    167             }
    168             else
    169                 return list.listData[i-1];
    170         }
    171     }
    172     return ERROR;
    173 }
    174 
    175 //查找某个元素的后继
    176 STATUS getRearElem(orderList list, ELEMTYPE elem)
    177 {
    178     //查找元素前判断顺序表是否为空
    179     if(isEmpty(list))
    180         return OVERFLOW;
    181     for(int i=0; i<list.length; i++)
    182     {
    183         if(elem==list.listData[i])
    184         {
    185             //在判断后继的过程中,要注意查找的元素是否为结束节点
    186             if(i >= list.listSize)
    187             {
    188                 cout << "已到达表的末尾!结束节点不存在后继!" << endl;
    189                 return OVERFLOW;
    190             }
    191             else
    192                 return list.listData[i+1];
    193         }
    194     }
    195     return ERROR;
    196 }
    197 
    198 //删除 pos 位置的那个元素(在删除这个元素之后,此元素之后的所有的元素要前移)
    199 STATUS deletePosElem(orderList& list, int pos, ELEMTYPE *elem)
    200 {
    201     //首先判断当前的顺序表是否为空,空表的话删了没必要
    202     if(isEmpty(list))
    203     {
    204         cout << "The orderList is Empty!" << endl;
    205         return ERROR;
    206     }
    207     //判断pos位置是否超出了表的当前容量
    208     if(pos >= list.length)
    209     {
    210         return ERROR;
    211     }
    212     *elem = list.listData[pos];
    213     for(int i=pos; i<=list.length-1; i++)
    214         list.listData[i] = list.listData[i+1];
    215     //因为删除表中的一个元素,表的当前长度减1
    216     list.length--;
    217     return OK;
    218 }
    219 
    220 //pos 位置插入元素(在pos元素之后插入元素后,pos位置之后的所有的元素要后移)
    221 STATUS addPosElem(orderList& list, int pos, ELEMTYPE elem)
    222 {
    223     //在插入元素之前,判断表是否已满,满了就追加空间
    224     if(isFull(list))
    225         appendListSpace(list);
    226     //判断pos位置是否合理,如果pos位置大于表的当前的容量的话,
    227     //是不能向表中插入的(因为这样就不是顺序表了)!
    228     if(pos >= list.length)
    229         return ERROR;
    230     //在插入元素前,首先要将pos位置后的元素全部后移一位。将pos位置腾空出来.
    231     for(int i=list.length-1; i>=pos; i--)
    232         list.listData[i+1] = list.listData[i];
    233     list.listData[pos] = elem;
    234     list.length++;
    235     return OK;
    236 }
    237 
    238 //销毁整个顺序表
    239 void destoryList(orderList* list)
    240 {
    241     //首先将顺序表的当前容量设为0
    242     list->length = 0;
    243     free(list->listData);
    244 }
    245 
    246 //将表的当前容量设为0
    247 void toZeroLength(orderList* list)
    248 {
    249     list->length = 0;
    250 }
    251 
    252 void DisplayFunc()
    253 {
    254     cout << "0  --> 退出程序" << endl;
    255     cout << "1  --> 初始化空表" << endl;
    256     cout << "2  --> 插入元素" << endl;
    257     cout << "3  --> 判断顺序表是否为空" << endl;
    258     cout << "4  --> 判断顺序表是否已满" << endl;
    259     cout << "5  --> 查找某个元素是否存在" << endl;
    260     cout << "6  --> 打印整个顺序表" << endl;
    261     cout << "7  --> 查找某个元素的前趋" << endl;
    262     cout << "8  --> 查找某个元素的后继" << endl;
    263     cout << "9  --> 在某个位置插入元素" << endl;
    264     cout << "10 --> 在某个位置删除元素" << endl;
    265     cout << "11 --> 当前的表长" << endl;
    266     cout << "12 --> 销毁整个顺序表" << endl;
    267     cout << "13 --> 将当前的表容量至为0" << endl << endl << endl;
    268 }
    269 
    270 int main(void)
    271 {    
    272     DisplayFunc();
    273     int n;
    274     int time = 0;
    275     int i = 0;
    276     int elem;
    277     int flag;
    278     int pos;
    279     int length;
    280     orderList list;
    281     while(1)
    282     {
    283         cout << "Please enter your select : ";
    284         cin >> n;
    285 
    286         switch(n)
    287         {
    288         case 1:
    289             initOrderList(&list);
    290             cout << endl;
    291             break;
    292         case 2:
    293             cout << "Please enter times: ";
    294             cin >> time;
    295             for(i=0; i<time; i++)
    296             {
    297                 cin >> elem;
    298                 insertList(&list, elem);
    299             }
    300             cout << endl;
    301             break;
    302         case 3:
    303             if(isEmpty(list))
    304                 cout << "orderList is empty!" << endl;
    305             else
    306                 cout << "orderList is have elems!" << endl;
    307             cout << endl;
    308             break;
    309         case 4:
    310             if(isFull(list))
    311                 cout << "orderList is full!" << endl;
    312             else
    313                 cout << "orderList is not full!" << endl;
    314             cout << endl;
    315             break;
    316         case 5:
    317             cout << "Enter you find number: ";
    318             cin >> elem;
    319             findElem(&list, elem);
    320             cout << endl;
    321             break;
    322         case 6:
    323             cout << "Print the list: " << endl;
    324             visitList(list);
    325             cout << endl;
    326             break;
    327         case 7:
    328             cout << "Enter find elem: ";
    329             cin >> elem;
    330             flag = getFrontElem(list, elem);
    331             if(flag==OVERFLOW || flag==ERROR)
    332                 cout << "Is not find, error!" << endl;
    333             else
    334                 cout << "The top " << elem << " chemotactic elements is " << flag << " !" << endl;
    335             cout << endl;
    336             break;
    337         case 8:
    338             cout << "Enter find elem: ";
    339             cin >> elem;
    340             flag = getRearElem(list, elem);
    341             if(flag==OVERFLOW || flag==ERROR)
    342                 cout << "Is not find, error!" << endl;
    343             else
    344                 cout << "The elem " << elem << " subsequent element of " << flag << " !" << endl;
    345             cout << endl;
    346             break;
    347         case 9:
    348             cout << "Please enter Location: ";
    349             cin >> pos;
    350             cout << "Please enter add elem: ";
    351             cin >> elem;
    352             flag = addPosElem(list, pos, elem);
    353             if(flag==OK)
    354             {
    355                 //delect success
    356                 cout << pos << " location is add successful! The add orderList elem is " << elem << " ." << endl;
    357                 cout << "Print the list: " << endl;
    358                 visitList(list);
    359             }
    360             else
    361             {
    362                 cout << "Error! add failure." << endl;
    363             }
    364             cout << endl;
    365             break;
    366         case 10:
    367             cout << "Please enter Location: ";
    368             cin >> pos;
    369             flag = deletePosElem(list, pos, &elem);
    370             if(flag==OK)
    371             {
    372                 //delect success
    373                 cout << pos << " location is delete successful! The delete orderList elem is " << elem << " ." << endl;
    374                 cout << "Print the list: " << endl;
    375                 visitList(list);
    376             }
    377             else
    378             {
    379                 cout << "Error! delete failure." << endl;
    380             }
    381             cout << endl;
    382             break;
    383         case 11:
    384             length = getListLength(&list);
    385             cout << "The orderList length is " << length << " ." << endl;
    386             cout << endl;
    387             break;
    388         case 12:
    389             destoryList(&list);
    390             cout << "The orderList destory is successful!" << endl;
    391             cout << endl;
    392             break;
    393         case 13:
    394             toZeroLength(&list);
    395             cout << "The orderList to list size of zero is successful!" << endl;
    396             cout << endl;
    397             break;
    398         case 0:
    399             cout << "Progarm is exit, enter any key shout black windows!" << endl;
    400             exit(0);
    401             cout << endl;
    402             break;
    403         }
    404     }
    405 
    406     return 0;
    407 }

    程序运行截图:

  • 相关阅读:
    Oracle(二)常用操作语句
    Oracle(一)概念理解
    Spring MVC实现文件上传和下载
    Spring MVC 的执行流程
    Spring MVC原理及配置详解
    idea创建maven web项目
    Spring Bean的生命周期
    integer和int的区别
    web项目搜索框智能提示
    html-tab page
  • 原文地址:https://www.cnblogs.com/geore/p/5791456.html
Copyright © 2011-2022 走看看