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);
    }
    

      

  • 相关阅读:
    [国家集训队]单选错位
    [USACO08DEC]Patting Heads S 轻拍牛头
    [SCOI2007]压缩 题解
    Json常用的转换
    cookie的读入和读出
    SQLHelp帮助类
    使用FFmpeg生成HLS视频
    如何选择HLS视频码流
    MacOS下的IntelliJ IDEA & Android Studio 通用配置
    在Windows上配置Django + WSGI
  • 原文地址:https://www.cnblogs.com/-slz-2/p/13154570.html
Copyright © 2011-2022 走看看