大话数据结构——线性表顺序存储结构

  1 #include<iostream>
  2 
  3 using namespace std;
  4 
  5 //线性表的顺序存储结构
  6 
  7 #define MAXSIZE 100//数组的最大值
  8 #define elemtype int//数据元素的类型
  9 #define OK 1
 10 #define ERROR 0
 11 
 12 #define TURE 1
 13 #define FALSE 0
 14 typedef int statues;
 15 
 16 //顺序存储的线性表的数据结构
 17 typedef struct{
 18     elemtype data[MAXSIZE];
 19     int length;
 20 }Sqlist;
 21 
 22 //初始化操作
 23 //初始化一个线性表,返回1.
 24 bool InitList(Sqlist *L)
 25 {
 26     L->length=0;
 27     return OK;
 28 }
 29 
 30 //将线性表清空
 31 //函数成功则返回1
 32 bool clearlist(Sqlist *L)
 33 {
 34     L->length=0;
 35     return 1;
 36 }
 37 
 38 //判断是否为空
 39 //若为空则返回值为1否则为0
 40 bool isempty(Sqlist L)
 41 {
 42     if(L.length!=0)
 43         return 0;
 44     else
 45         return 1;
 46 }
 47 
 48 
 49 //返回操作
 50 //返回线性表L的第i个元素给e,成功则返回1否则为0
 51 bool getelem(Sqlist L,int i,elemtype *e)
 52 {
 53     if(L.length==0)
 54         return FALSE;
 55     if(i<1||i>L.length)
 56         return FALSE;
 57     *e=L.data[i-1];
 58     return OK;
 59 }
 60 
 61 //查找操作
 62 //查找元素e在线性表L中的位置,查找成功则返回元素e的位置,否则返回0
 63 int locationlist(Sqlist L,elemtype e)
 64 {
 65     if(L.length==0)
 66         return 0;
 67     for(int i=0;i!=L.length;++i)
 68     {
 69         if(L.data[i]==e)
 70             return i+1;
 71     }
 72     return 0;
 73 }
 74 
 75 //插入元素
 76 //将元素e插入到线性表L中的第i个元素之前,若成功则返回1,否则返回0
 77 bool insertlist(Sqlist *L,int i,elemtype e)
 78 {
 79 
 80     if(L->length==MAXSIZE)
 81         return 0;
 82     if(i<1||i>L->length+1)
 83         return 0;
 84     if(i<=L->length)
 85     {
 86         for(int j=L->length-1;j!=i-2;--j)
 87             L->data[j+1]=L->data[j];
 88     }
 89     L->data[i-1]=e;
 90     L->length++;
 91     return 1;
 92 }
 93 
 94 
 95 
 96 
 97 int main()
 98 {
 99 
100     system("pause");
101     return 1;
102 }

 

转载于:https://www.cnblogs.com/yanliang12138/p/4325101.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值