数据结构复习 顺序列表

#ifndef SQLIST
#define SQLIST
#include <iostream>
#include <stdio.h>
#define MaxSize 15
typedef int ElemType;

typedef struct
{
    ElemType data[MaxSize];
    int length;
}
SqList;

bool ListInsert(SqList &L,int i,ElemType e)
{
    //插入位置错误
    if (i<1||i>L.length+1) //1 2 ... length 为合法位置
            return false;
    //表满了
    if (L.length>=MaxSize) //length == MaxSize时,表满
        return false;
    // j = length 到 i
    for(int j = L.length;j>=i;j--) // 列表 1 2 ... [i i+1 i+2 ... Length] 后移
    {
        L.data[j] = L.data[j-1];   // 数组 0 1 ... [i-1 i i+1 ... Length-1] 后移
    }
    L.data[i-1] = e ; //
    L.length++;
    return true;
}


bool ListDelete(SqList &L,int i,ElemType &e)
{
    if (i<1||i>L.length)
        return false;
    e = L.data[i-1];

    // 数组 0 1 ... [i-1 i i+1 ... Length-1]
    for(int j=i;j<=L.length-1;j++)
    {
        L.data[j-1] = L.data[j];
    }
    L.length--;
    return true;
}

int LocateElem(SqList L,ElemType e)
{
    for(int j=0;j<L.length;j++)
    {
        if(L.data[j] == e)
            return j+1;
    }
    return 0;
}

void InitList(SqList &L)
{
    L.length = 0;
}

void PrintList(char *name,SqList L)
{
   std::cout << name << std::endl;
   for(int i=1;i<=L.length;i++)
   {
       std::cout << L.data[i-1] << " ";
   }
   std::cout<< std::endl;
}

#endif // SQLIST


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值