数据结构-(顺序表)

顺序表的类型描述

#define MaxSize 100
typedef int ElemType;
typedef struct{
    ElemType data[MaxSize];//指示动态分配数组的指针 
    int length;//当前的长度 
}SqList;

 

顺序表示属于静态分配方式,但是也可以采用动态 的分配的方式。通过动态分配时,一旦数据空间占满,就可以另外开辟一块更大的空间,用以替换原来的存储空间,从而达到扩充存储空间的目的

typedef int ElemType;
typedef struct{
    ElemType *data;//
    int MaxSize,length;//
}SqList;

 

插入操作

//顺序链表插入操作 
bool ListInsert(SqList &L,int loc,ElemType e){
    //L是一个顺序链表,loc是代表数据 要插入的位置,e是要插入的数据 
    if(loc<1||loc>L.length+1){ 
        //判断i的范围是否有效
        return false;
     }
    if(L.length>=MaxSize){
        //储存空间已满
        return false;
    }
    for(int j=L.length;j>=loc;j--){//因为是插入,所以需要对顺序链表进行移位 
        L.data[j]=L.data[j-1];
    }
    L.data[loc-1]=e;//在loc的位置插入数据e 
    L.length++;
    return true;//插入成功 
}
//删除元素操作,删除顺序表中第i个元素
bool ListDeleteByLoc(SqList &L,int loc){
    if(loc<1||loc>L.length){
        return false;
    }
    for(int j=loc;j<L.length;j++){
        L.data[j-1]=L.data[j];
    }
    L.length--;
    return false;
}
//删除元素按照元素信息,
//首先需要先遍历查找确认元素是否存在如果存在那么删除如果不存在那么无法删除
bool ListDeleteByData(SqList &L,ElemType e){
    int  len=L.length;//获取顺序表的长度
    int loc=0;
    for(int i=0;i<len;i++){
        if(L.data[i]==e){//查找顺序表中是否存在这个元素 
            loc=i;//确认存在之后获取位置然后跳出循环 
            break;
        }
    }
    if(loc==0){
        return false;
    }else{ 
       for(int i=loc;i<len;i++){
           L.data[i-1]=L.data[i];
       }
       return true; 
    }
}
//按值查找元素,然后返回位置
int LocateElemType(SqList &L,ElemType e){
    int i;
    for(i=0;i<L.length;i++){
        if(L.data[i]==e)
            return i+1;
    }
    return 0;
}

 

整体代码

#include<bits/stdc++.h>
using namespace std;
#define MaxSize 100
typedef int ElemType;
typedef struct{
    ElemType data[MaxSize];//指示动态分配数组的指针 
    int length;//当前的长度 
}SqList;
//顺序链表插入操作 
bool ListInsert(SqList &L,int loc,ElemType e){
    //L是一个顺序链表,loc是代表数据 要插入的位置,e是要插入的数据 
    if(loc<1||loc>L.length+1){ 
        //判断i的范围是否有效
        return false;
     }
    if(L.length>=MaxSize){
        //储存空间已满
        return false;
    }
    for(int j=L.length;j>=loc;j--){//因为是插入,所以需要对顺序链表进行移位 
        L.data[j]=L.data[j-1];
    }
    L.data[loc-1]=e;//在loc的位置插入数据e 
    L.length++;
    return true;//插入成功 
}
//输出顺序链表的信息
void showSqList(SqList &L){
    int len=L.length;
    for(int i=0;i<len;i++){
        printf("%d  ",L.data[i]);
    }
}
//删除元素操作,删除顺序表中第i个元素
bool ListDeleteByLoc(SqList &L,int loc){
    if(loc<1||loc>L.length){
        return false;
    }
    for(int j=loc;j<L.length;j++){
        L.data[j-1]=L.data[j];
    }
    L.length--;
    return false;
}
//删除元素按照元素信息,
//首先需要先遍历查找确认元素是否存在如果存在那么删除如果不存在那么无法删除
bool ListDeleteByData(SqList &L,ElemType e){
    int  len=L.length;//获取顺序表的长度
    int loc=0;
    for(int i=0;i<len;i++){
        if(L.data[i]==e){//查找顺序表中是否存在这个元素 
            loc=i;//确认存在之后获取位置然后跳出循环 
            break;
        }
    }
    if(loc==0){
        return false;
    }else{ 
       for(int i=loc;i<len;i++){
           L.data[i-1]=L.data[i];
       }
       return true; 
    }
}
//按值查找元素,然后返回位置
int LocateElemType(SqList &L,ElemType e){
    int i;
    for(i=0;i<L.length;i++){
        if(L.data[i]==e)
            return i+1;
    }
    return 0;
}
//执行插入操作的步骤
 
int main(){
    SqList L;
    int select;
    do{
      cout<<"请选择对顺序表的相关操作"<<endl;
      cout<<"1.添加顺序表"<<endl;
      cout<<"2.输出顺序表"<<endl;
      cout<<"3.对顺序表进行排序"<<endl;
      cout<<"4.查找元素"<<endl;
      cout<<"请选择:";
        cin>>select;
        if(select==1){
               cout<<"添加顺序表"<<endl;
           int loc,num;
           cin>>loc>>num;
           if(ListInsert(L,loc,num)) cout<<"插入成功"<<endl;
           else cout<<"插入失败"; 
      }else if(select==2){
          showSqList(L);
          cout<<endl; 
       }
    }while(select!=-1);
    return 0;
}

 

posted @ 2020-09-02 17:11  薄眠抛却陈年事。  阅读(295)  评论(0编辑  收藏  举报