zoukankan      html  css  js  c++  java
  • 顺序表基本操作

    #include<stdio.h>
    #define MaxSize 10
    typedef int ElemType;
    typedef struct{
    ElemType data[MaxSize];
    ElemType length;
    }SqList;
    void InitList(SqList &L)	//初始化
    {
    L.length=0;
    }
    bool InserList(SqList &L,ElemType i,ElemType e)	//插入操作
    {
    if(i<0 || i>L.length+1 || L.length>=MaxSize)
    return false;
    for(ElemType j=L.length;j>=i;j--)    //将i及i后面位置元素依次后移
    L.data[j]=L.data[j-1];
    L.data[j]=e;
    L.length++;
    return true;
    }
    bool DeleteList(SqList &L,ElemType i,ElemType &e)	//删除操作
    {
    if(i<0 || i>L.length)
    return false;
    e=L.data[i-1];
    if(L.length==0)
    return false;
    for(ElemType j=i-1;j<L.length;j++)    //将i及i后面位置依次前移
    L.data[j]=L.data[j+1];
    L.length--;
    return true;
    }
    int GetElem(SqList L,ElemType i)	//按值查找
    {
    if(i<0 || i>L.length)
    return 0;
    return L.data[i-1];
    }
    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 PrElemType(SqList L)	//输出
    {
    for(ElemType i=0;i<L.length;i++)
    printf("data[%d]=%d
    ",i,L.data[i]);
    }
    void main()
    {
    SqList L;
    ElemType e=-1;
    InitList(L);
    InserList(L,1,2);
    InserList(L,2,3);
    InserList(L,3,4);
    InserList(L,4,5);
    InserList(L,1,10);
    InserList(L,7,20);
    PrElemType(L);
    printf("e=%d
    ",e);
    printf("第%d个位置是%d
    ",2,GetElem(L,2));
    printf("10是第%d个元素
    ",LocateElem(L,10));
    printf("*********************
    ");
    DeleteList(L,2,e);
    PrElemType(L);
    printf("e=%d
    ",e);
    }
    

      

  • 相关阅读:
    bzoj3757 苹果树
    bzoj2743 [HEOI2012]采花
    bzoj4241 历史研究
    bzoj4448 [Scoi2015]情报传递
    bzoj3295 [Cqoi2011]动态逆序对
    bzoj4034 [HAOI2015]T2
    bzoj3339 Rmq Problem
    BZOJ 1017 魔兽地图
    BZOJ 1021 循环的债务
    SUOI #37 清点更多船只
  • 原文地址:https://www.cnblogs.com/-slz-2/p/13154570.html
Copyright © 2011-2022 走看看