• 数据结构C语言版干货------->线性表之顺序表


    一:头文件定义
     1 /***************************************************************************
     2  *项目 数据结构
     3  *概要 逻辑结构:线性结构
     4        物理结构:顺序结构
     5        --------:  顺序表
     6  *单位 西安电子科技大学
     7  *作者 刘周
     8  *日期 2018-3-5
     9 ***************************************************************************/
    10 #ifndef _ORDERED_LIST_
    11 #define _ORDERED_LIST_
    12 #include "stdio.h"
    13 #include "stdlib.h"
    14 
    15 /*定义顺序表的最大长度*/
    16 #define MAX_SIZE 1000
    17 
    18 typedef struct node {
    19     int Element[MAX_SIZE];
    20     int length;
    21 }OrderedList;
    22 typedef int ElementType;
    23 
    24 /***************************************************
    25  *函数 Init()
    26  *功能 初始化顺序表,将它的长度置1
    27  *返回 void
    28 ***************************************************/
    29 extern void Init(OrderedList * L);
    30 
    31 /***************************************************
    32  *函数 GetLength()
    33  *功能 获取顺序表当前元素的个数
    34  *返回 int
    35 ***************************************************/
    36 extern int GetLength(OrderedList L);
    37 
    38 /***************************************************
    39  *函数 GetElement()
    40  *功能 根据索引获取元素
    41  *返回 ElementType
    42 ***************************************************/
    43 extern ElementType GetElement(OrderedList L,int location);
    44 
    45 /***************************************************
    46  *函数 GetLocation()
    47  *功能 根据元素内容获取元素位置
    48  *返回 int
    49 ***************************************************/
    50 extern int GetLocation(OrderedList L,ElementType e);
    51 
    52 /***************************************************
    53  *函数 GetPrior()
    54  *功能 求前驱元素
    55  *返回 ElementType
    56 ***************************************************/
    57 extern ElementType GetPrior(OrderedList L,ElementType e);
    58 
    59 /***************************************************
    60  *函数 GetNext()
    61  *功能 求后继元素
    62  *返回 ElementType
    63 ***************************************************/
    64 extern ElementType GetNext(OrderedList L,ElementType e);
    65 
    66 /***************************************************
    67  *函数 Insert()
    68  *功能 前插操作
    69  *返回 void
    70 ***************************************************/
    71 extern void Insert(OrderedList *L,int loc,ElementType e);
    72 
    73 /***************************************************
    74  *函数 Delete()
    75  *功能 删除操作:根据索引进行删除
    76  *返回 void
    77 ***************************************************/
    78 extern void Delete(OrderedList* L,int loc);
    79 
    80 /***************************************************
    81  *函数 Traverse()
    82  *功能 遍历输出顺序表
    83  *返回 void
    84 ***************************************************/
    85 extern void Traverse(OrderedList L);
    86 #endif // _ORDERED_LIST_
    View Code
    二:具体实现c
      1 #include "OrderedList.h"
      2 void Init(OrderedList* L)
      3 {
      4     L->length = 0;
      5 }
      6 
      7 int GetLength(OrderedList L)
      8 {
      9     return L.length;
     10 }
     11 
     12 ElementType GetElement(OrderedList L,int location)
     13 {
     14     if(location>=1 && location < GetLength(L))
     15     {
     16         return L.Element[location-1];
     17     }
     18     else
     19     {
     20         printf("位置参数非法!
    ");
     21         return 0;
     22     }
     23 }
     24 
     25 int GetLocation(OrderedList L,ElementType e)
     26 {
     27     int i=1;
     28     while (i <= L.length && e!=L.Element[i-1] )
     29         i++;
     30     if(i<=L.length)
     31         return i;
     32     else
     33     {
     34         printf("该表无此元素!
    ");
     35         return 0;
     36     }
     37 }
     38 
     39 ElementType GetPrior(OrderedList L,ElementType e)
     40 {
     41     int loc=GetLocation(L,e);
     42     if(loc!=0)
     43     {
     44         if(loc==1)
     45         {
     46             printf("第一个元素没有前驱!
    ");
     47             exit(0);
     48         }
     49         else
     50             return L.Element[loc-2];
     51     }
     52 }
     53 
     54 ElementType GetNext(OrderedList L,ElementType e)
     55 {
     56     int loc=GetLocation(L,e);
     57     if(loc!=0)
     58     {
     59         if(loc==L.length)
     60         {
     61             printf("最后一个元素没有后继!
    ");
     62             exit(0);
     63         }
     64         else
     65             return L.Element[loc];
     66     }
     67 }
     68 
     69 void Insert(OrderedList *L,int loc,ElementType e)
     70 {
     71     if(L->length == MAX_SIZE)   //表满
     72     {
     73         printf("表已经满了,无法继续插入!
    ");
     74         return;
     75     }
     76     if(loc < 1 || loc > L->length+1)//为什么+1,那个位置表示在最后添加
     77     {
     78         printf("loc的值:%d不合法!!!
    ",loc);
     79         return;
     80     }
     81     for(int i =L->length-1;i>=loc-1;i--)
     82     {
     83         L->Element[i+1]=L->Element[i];
     84     }
     85     L->Element[loc-1] = e;
     86     L->length++;
     87 }
     88 
     89 void Delete(OrderedList *L,int loc)
     90 {
     91     if(loc<1 || loc >(*L).length)//注意核实到底有没有+1
     92     {
     93         printf("
    ::::::loc的值不合法!
    ");
     94         return;
     95     }
     96     for(int i =loc;i<(*L).length;i++)
     97     {
     98         L->Element[i-1]=L->Element[i];
     99     }
    100     L->length--;
    101 }
    102 
    103  void Traverse(OrderedList L)
    104  {
    105      if(L.length>0)
    106      {
    107          printf("
    ");
    108          for(int i = 0; i<L.length; i++)
    109          {
    110              printf("%d	",L.Element[i]);
    111          }
    112      }
    113      else
    114         printf("这是一个空表!
    ");
    115  }
    View Code
     1 #include "OrderedList.h"
     2 int main(void)
     3 {
     4     OrderedList L;
     5     Init(&L);
     6     printf("此时表长:%d
    ",GetLength(L));
     7     for(int i =0;i<10;i++)
     8     Insert(&L,i+1,i*5);
     9     Traverse(L);
    10     Insert(&L,7,100);
    11     Traverse(L);
    12     Delete(&L,8);
    13     Traverse(L);
    14     printf("
    此时表长:%d
    ",GetLength(L));
    15     printf("位置5是元素:%d
    ",GetElement(L,5));
    16     printf("元素20在位置%d
    ",GetLocation(L,20));
    17     return 0;
    18 }
  • 相关阅读:
    ORACLE时间函数(SYSDATE)简析
    RestTemplate请求
    Java中ASM框架详解
    java进阶
    Java,Mysql-根据一个给定经纬度的点,进行附近500米地点查询–合理利用算法
    JAVA程序员必看的15本书-JAVA自学书籍推荐
    Linux curl命令详解
    Java 集合系列06之 Vector详细介绍(源码解析)和使用示例
    腾讯信鸽推送Android SDK快速指南
    信鸽推送.NET SDK 开源
  • 原文地址:https://www.cnblogs.com/Cirgo/p/8510283.html
Copyright © 2020-2023  润新知