zoukankan      html  css  js  c++  java
  • [数据结构与算法] (顺序)线性表简单demo程序

      1 /*******************************************************
      2 * @: Project:    (顺序)线性表数据结构演示
      3 * @: File:        main.c
      4 * @: Function: 提供(顺序)线性表操作的基本函数和方法
      5 * @: History:    2013年8月23日 09:16:56
      6 * @: Author:    Alimy
      7 *******************************************************/
      8 
      9 /*******************************************************
     10 * @: 头文件包含
     11 *******************************************************/
     12 #include "list.h"
     13 #include <stdio.h>
     14 /*******************************************************
     15 * @:(外部)函数&(外部)变量声明
     16 *******************************************************/
     17 void printmenu(void);
     18 
     19 
     20 SqList List_1;
     21 
     22 
     23 /*******************************************************
     24 * @: 主函数
     25 *******************************************************/
     26 int main(void){
     27     ElemType e = 0;
     28     char keyvalue = 0;
     29     int idx = 0;
     30 
     31     if(OK==InitList(&List_1)){
     32         //printListData(&List_1);
     33         printf("初始化链表完成, 按任意键进入主菜单 
    ");
     34     }
     35     getch(); //等待按键,进入大循环
     36     while(1){
     37         system("cls");
     38         printmenu();
     39         fflush(stdin);
     40         scanf("%c",&keyvalue);
     41         //getchar();//去掉回车键的键值
     42         fflush(stdin);
     43         switch(keyvalue){
     44             case 'a':
     45                 printf("请输入你要插入的位置索引及要插入的数据
    ");
     46                 scanf("%d%d",&idx,&e);
     47                 printf("你要插入数据的位置是 【%d】,要插入的数据是【%d】
    ",idx,e);
     48                 InsertListElem(&List_1,idx,e);
     49                 printf("按任意键返回主菜单
    "); 
     50                 getch();
     51                 break;
     52             
     53             case 'b':
     54                 printf("请输入你要删除顺序线性表中的元素的索引
    ");
     55                 scanf("%d",&idx);
     56                 printf("你要删除数据的位置是 【%d】
    ",idx);
     57                 if(OK==DeleteListElem(&List_1,idx,&e)){
     58                     printf("删除元素成功,被删除的元素值为 %d 
    ",e);
     59                 }
     60                 printf("按任意键返回主菜单
    "); 
     61                 getch();
     62                 break;
     63             case 'c':
     64                 printf("请输入你要得到顺序线性表中的元素的索引
    ");
     65                 scanf("%d",&idx);
     66                 if(OK!=GetListElem(List_1,idx,&e)){
     67                     printf("获取元素失败,请检查相关输入是否符合操作条件
    ");
     68                     }
     69                 else{
     70                     printf("获取到的元素是 %d 
    ",e);
     71                     }
     72                 printf("按任意键返回主菜单
    "); 
     73                 getch();
     74                 break;
     75             case 'd':
     76                 printf("请输入你要得到顺序线性表中的元素值
    ");
     77                 scanf("%d",&e);
     78                 LocateListElem(&List_1,e);
     79                 printf("按任意键返回主菜单
    "); 
     80                 getch();
     81                 break;
     82             case 'e':
     83                 printf("当前顺序线性表的表长为【%d】
    ",List_1.m_ListCurrentlength);
     84                 printf("按任意键返回主菜单
    "); 
     85                 getch();
     86                 break;
     87             case 'f':
     88                 InitList(&List_1);
     89                 printf("清除线性顺序表完成,按任意键返回主菜单
    "); 
     90                 getch();
     91                 break;
     92             case 'g':
     93                 printListData(&List_1);
     94                 printf("按任意键返回主菜单
    "); 
     95                 getch();
     96                 break;
     97             case 'h':
     98                 goto _end;
     99                 break;
    100             default:
    101                 printf("你的输入环节有误,清重新输入 
    ");
    102                 printf("按任意键返回主菜单
    "); 
    103                 getch();
    104                 break;
    105 
    106                 
    107 
    108 
    109         }
    110         
    111 
    112 
    113     
    114     }
    115 
    116 _end:
    117 //    getchar();
    118     return 1; 
    119 }
    120 
    121 
    122 void printmenu(void){
    123     printf("  【主菜单】: 清输入数据相应对应的操作
    ");
    124     printf("【a】: 在顺序线性表中插入元素
    ");
    125     printf("【b】: 在顺序线性表中删除元素
    ");
    126     printf("【c】: 在顺序线性表中得到元素
    ");
    127     printf("【d】: 在顺序线性表中定位元素
    ");
    128     printf("【e】: 获取当前顺序线性表的当前表长
    ");
    129     printf("【f】: 清除顺序线性表
    ");
    130     printf("【g】: 打印当前顺序线性表的所有数据
    ");
    131     printf("【h】: 退出当前程序
    ");
    132     printf("你需要执行的操作项是:  ");
    133 }
      1 /*******************************************************
      2 * @: Project:    (顺序)线性表数据结构演示
      3 * @: File:        list.c
      4 * @: Function: 提供(顺序)线性表操作的基本函数和方法
      5 * @: History:    2013年8月23日 09:16:56
      6 * @: Author:    Alimy
      7 *******************************************************/
      8 
      9 /*******************************************************
     10 * @: 头文件包含 
     11 *******************************************************/
     12 #include "list.h"
     13 #include <stdio.h> 
     14 
     15 /*******************************************************
     16 * @: (外部&内部)变量及(外部&内部)函数声明及定义 
     17 *******************************************************/
     18 StatusType GetListElem(SqList L,int i,ElemType* pe);
     19 StatusType InsertListElem(SqList *pL,int i, ElemType e);
     20 StatusType DeleteListElem(SqList *pL,int i, ElemType* pe);
     21 StatusType InitList(SqList* pL);
     22 StatusType IsListEmpty(SqList* pL);
     23 void printListData(SqList* pL);
     24 void LocateListElem(SqList* pL,ElemType e);
     25 
     26 
     27 
     28 /*******************************************************
     29 * @: (外部&内部)变量及(外部&内部)函数声明及定义 
     30 *******************************************************/
     31 /*
     32 * @: 得到顺序线性表的第i个元素,返回在ElemType *e 指向的内存中
     33 * @: 返回值 ERROR   获取元素失败
     34 *                        OK      获取元素成功
     35 */
     36 extern StatusType GetListElem(SqList L,int i,ElemType* pe){
     37     if((i>L.m_ListCurrentlength)||(i<=0)||(0==L.m_ListCurrentlength)){ // 序号大于表的当前长度或者序号小余等于0(线性表的序号从1开始)或者目前顺序表为空表
     38         return ERROR ;
     39     }
     40     else{
     41         *pe = L.m_data[i-1];
     42         return OK;
     43     }
     44 }
     45 
     46 /*
     47 * @: 在pL指向的线性表中的第i个位置插入e
     48 * @: 返回值 ERROR   插入元素失败
     49 *                        OK      插入元素成功
     50 */
     51 extern StatusType InsertListElem(SqList *pL,int i, ElemType e){
     52 
     53     int idx;
     54     
     55 //    if((i>=pL->m_ListCurrentlength)||(i<0))    //如果要插入的位置大于或者等于当前线性表长度  或者i为负数, return ERROR
     56 //        return ERROR;
     57 
     58     if((i>pL->m_ListCurrentlength+1)||(i<1)){    // 若插入位置参数大于当前表长加1(可以在表尾插入,即等于表长加1),或者i非正整数,return ERROR
     59         
     60         //打印相关参数,分析失败原因
     61         printf("【插入位置有误,清检查参数】 
    ");
     62         printf("插入位置 i = %d 
    ",i);
     63         printf("顺序表当前长度为 pL->m_ListCurrentlength = %d 
    ",pL->m_ListCurrentlength);    
     64         return ERROR;    
     65     }
     66     if(pL->m_ListCurrentlength == LIST_MAXSIZE){ //如果当前线性表长度等于最大长度, return ERROR
     67         
     68         printf("【当前表长 = %d,最大表长 = %d ,顺序表已经满了无法执行插入操作】 
    ",pL->m_ListCurrentlength,LIST_MAXSIZE);
     69         return ERROR;
     70     }
     71         
     72 //    //搬移数据
     73 //    if(i==(pL->m_ListCurrentlength+1)){  // 若是在表尾插入
     74 //        pL->m_data[i-1] = e;
     75 //    }
     76 //    else{                                // 若不是在表尾插入
     77 //    
     78 //        for(idx=pL->m_ListCurrentlength;idx>=i;idx--){
     79 //            pL->m_data[idx] = pL->m_data[idx-1];
     80 //        }
     81 //        pL->m_data[i-1] = e;
     82 //    }
     83 //    
     84 
     85     if(i!=(pL->m_ListCurrentlength+1)){     //若不是在表尾插入
     86         for(idx=pL->m_ListCurrentlength;idx>=i;idx--){
     87             pL->m_data[idx] = pL->m_data[idx-1];
     88         }
     89     }
     90     
     91     pL->m_data[i-1] = e;    //无论是否是在表尾插入,这两个步骤都必须执行
     92     pL->m_ListCurrentlength++;
     93     printf("插入操作完成 
    ");
     94     
     95     return OK;
     96 }
     97 
     98 /*
     99 * @: 删除pL指向的顺序表中的第i个元素,并将被删除的值存放在pe指向的内存空间
    100 * @: 返回值 ERROR 删除元素失败
    101 *                        OK    删除元素成功
    102 */
    103 extern StatusType DeleteListElem(SqList *pL,int i, ElemType* pe){
    104     int idx;
    105     if(0 == pL->m_ListCurrentlength){  //当前表为空表,无法执行元素删除操作 return ERROR
    106         printf("顺序表当前表长为 %d,为空表,无法执行元素删除操作 
    ");
    107         return ERROR;    
    108     }
    109     
    110     if((i<1)||(i>pL->m_ListCurrentlength)){ //i为非正整数或者 i大于当前表长,参数有误,无法执行元素删除操作 return ERROR
    111         //打印相关参数,分析失败原因
    112         printf("删除失败,请根据相关参数分析原因 
    ");
    113         printf("要删除的位置为 %d
    ",i);
    114         printf("顺序表当前表长为 %d 
    ",pL->m_ListCurrentlength);
    115         return ERROR;
    116     }
    117     
    118     *pe = pL->m_data[i-1];  // 存储被删除的元素
    119     if(i<pL->m_ListCurrentlength){  //若不是删除表尾的元素 
    120         
    121         for(idx = i; idx<pL->m_ListCurrentlength;idx++){ //搬移数据
    122             pL->m_data[idx-1] = pL->m_data[idx];
    123         }
    124     }
    125     
    126     pL->m_ListCurrentlength--; //若是删除表尾的元素,操作表长即可
    127     return OK;
    128 }
    129 
    130 
    131 /*
    132 * @: 初始化一个顺序表,初始化成功将得到一个空表
    133 * @: 返回值 :OK   初始化顺序表成功
    134 */
    135 extern StatusType InitList(SqList* pL){  
    136 
    137     int idx = 0;
    138 
    139     pL->m_ListCurrentlength = 0;
    140     for(idx=0;idx<LIST_MAXSIZE;idx++){
    141         pL->m_data[idx] = 0x00;
    142     }
    143 
    144     return OK;
    145 }
    146 
    147 /*
    148 * @: 判断pL指向的顺序表是否为空表
    149 * @: 返回值 :
    150 *        OK  此线性表是空表
    151 *        ERROR 此线性表不是空表
    152 */
    153 extern StatusType IsListEmpty(SqList* pL){  
    154 
    155     if(0 == pL->m_ListCurrentlength){
    156         printf("当前表为空表
    ");
    157         return OK;
    158     }
    159 
    160     else{
    161         printf("当前表不为空表
    ");
    162         return ERROR;
    163     }    
    164 }
    165 
    166 /*
    167 * @: 打印当前顺序表的所有元素
    168 * @: 返回值 : 无
    169 */
    170 extern void printListData(SqList* pL){  
    171     
    172     int idx;
    173     if(0 == pL->m_ListCurrentlength){
    174         printf("当前表为空表,没有什么好打印的 
    ");
    175     }
    176 
    177     else{
    178         printf("当前表的值为:
    ");
    179         printf("当前表长为:%d
    ",pL->m_ListCurrentlength);
    180         for(idx=1;idx<=pL->m_ListCurrentlength;idx++){
    181             printf("pL_data[%d] = %d
    ",idx-1,pL->m_data[idx-1]);    
    182         }
    183     }    
    184 
    185     printf("
    ");
    186     printf("
    ");
    187     
    188 }
    189 
    190 /*
    191 * @: 定位pL指向的顺序表中是否有值为e的元素?
    192 */
    193 extern void LocateListElem(SqList* pL,ElemType e){
    194     int idx = 1;
    195         
    196     int exsit_flag = 0;
    197     if(0==pL->m_ListCurrentlength){
    198         printf("当前表为空表,无法查找 
    ");
    199         return;
    200     }
    201     for(idx=1;idx<=pL->m_ListCurrentlength; idx++){
    202         if(e==pL->m_data[idx-1]){
    203             printf("成功在顺序线性表中定位到该元素,位置是【%d】 ,值L->m_data[%d]为【%d】
    ",idx,idx-1,pL->m_data[idx-1]);
    204             exsit_flag = 1;
    205         }
    206 
    207     }
    208 
    209     if(0==exsit_flag){
    210         printf("抱歉,未能在顺序线性表中定位到该元素 
    ");
    211         }
    212         
    213 
    214 }
     1 /*******************************************************
     2 * @: Project:    (顺序)线性表数据结构演示
     3 * @: File:        list.h
     4 * @: Function: 提供(顺序)线性表操作的数据结构定义及方法声明
     5 * @: History:    2013年8月23日 09:16:56
     6 * @: Author:    Alimy
     7 *******************************************************/
     8 
     9 /*******************************************************
    10 * @:头文件包含
    11 *******************************************************/
    12 
    13 #ifndef __LIST_H__
    14 #define __LIST_H__
    15 
    16 
    17 /*******************************************
    18 * @: 数据结构定义
    19 ********************************************/
    20 #define LIST_MAXSIZE  20    //顺序表的最大表长
    21 #define ElemType int
    22 #define StatusType int
    23 
    24 typedef struct {
    25     ElemType m_data[LIST_MAXSIZE];    //数据存储
    26     int m_ListCurrentlength ;         //顺序表的当前长度
    27 }SqList;
    28 
    29 //#define StatusType
    30 #define OK    1
    31 #define ERROR 0
    32 
    33 /*******************************************
    34 * @: 外部调用函数声明
    35 ********************************************/
    36 
    37 extern StatusType GetListElem(SqList L,int i,ElemType *pe); //获取表中的元素
    38 extern StatusType InsertListElem(SqList *pL,int i, ElemType e); //在表中插入新的元素
    39 extern StatusType DeleteListElem(SqList *pL,int i, ElemType* pe); //在表中删除特定位置元素
    40 extern StatusType InitList(SqList* pL);    //初始化一个空的线性表
    41 extern StatusType IsListEmpty(SqList* pL); //判断线性表是否为空表
    42 extern void printListData(SqList* pL);//打印当前顺序表的所有元素
    43 extern void LocateListElem(SqList* pL,ElemType e);  // 定位顺序线性表中的元素
    44 
    45 extern int printf(const char *,...);
    46 
    47 
    48 
    49 
    50 
    51 #endif  //  end of __LIST_H__

    顺序线性表的时间复杂度考究:

      插入 & 删除 & 查找 等操作的平均时间复杂度都是O(n)

      读取 & 写入 操作等操作的平均时间复杂度收拾O(1)

    多用于存取数据的应用。

    ..

    ~不再更新,都不让我写公式,博客园太拉胯了
  • 相关阅读:
    弄明白python reduce 函数
    Linux 下载百度网盘大文件
    java 从网上下载文件的几种方式
    Windows下Python2与Python3两个版本共存的方法详解
    python 学习笔记
    Glide实现查看图片和保存图片到手机
    Android Animation 知识点速记备忘思维导图
    You must not call setTag() on a view Glide is targeting when use Glide
    前端数据流哲学
    精读《Optional chaining》
  • 原文地址:https://www.cnblogs.com/alimy/p/3285134.html
Copyright © 2011-2022 走看看