数据结构02-顺序表

2、线性表

2、1 顺序表

基本操作:CRUD (C++实现)
//创建顺序表L
bool CreateList(Sqlist &L){
    int x,n;
    cout<<"请输入n个元素:"<<endl;
    cin>>n;
    cout<<"请输入"<<n<<"个元素:"<<endl;
    for(int i = 0;i < n; i++){
        if(L.length==Maxsize){
            cout<<"顺序表已经满啦!";
            return false;
        }
        cin>>x;
        L.elem[i] = x;
        L.length++;
    }
}
//销毁顺序表
void DestroyList(Sqlist &L){
	if(L.elem)
	delete []L.elem;//释放存储空间
}
//增删改查
bool ListInsert_Sq(Sqlist &L,int i,int e){
	if(i<1||i>L.length+1) return false;	 //i值不合法
	if(L.length==Maxsize) return false; //存储空间已满
	for(int j=L.length-1;j>=i-1;j--)
	    L.elem[j+1]=L.elem[j];   //从最后一个元素开始后移,直到第i个元素后移
	L.elem[i-1]=e;              //将新元素e放入第i个位置
	L.length++;		     	//表长增1
	return true;
}

bool ListDelete_Sq(Sqlist &L,int i,int &e){
	if((i<1)||(i>L.length)) return false;//i值不合法
	e=L.elem[i-1];   //将欲删除的元素保留在e中
	for(int j=i;j<=L.length-1;j++)
		L.elem[j-1]=L.elem[j]; //被删除元素之后的元素前移
	L.length--; //表长减1
	return true;
}
//判断位置是否合法,合法就获取 i 位置的元素
bool GetElem(Sqlist &L,int i,int &e){
    if(i < 1||i > L.length) return false;
    //获取第 i 个位置的元素值
    e = L.elem[i-1];
    return true;
}
//根据元素值获取它在顺序表中的位置
int LocateELem(Sqlist &L,int x){
    for(int i = 0; i < L.length;i++){
        if(L.elem[i]==x){
            return i+1;
        }
        return -1;
    }
}
int GetLength(Sqlist &L){

    return L.length;
}
void Empty(Sqlist &L){
    if(L.length<=0){
        cout<<"顺序表为空!"<<endl;
    }else{
        cout<<"顺序表不为空!"<<endl;
    }
}

完整源码:

#include<iostream>

using namespace std;

#define Maxsize 100 //最大空间

typedef struct{
   int *elem; //基地址
   int length; //顺序表长度
}Sqlist;


//构造空的顺序表,为其分配固定空间Maxsize

//L加&表示引用类型参数,函数内部的改变跳出函数仍然有效
//不加&内部改变,跳出函数后无效
bool InitList(Sqlist &L){

    L.elem = new int [Maxsize];
    if(!L.elem){
        return false;
    }
    L.length = 0;
    return true;
}
//销毁顺序表
void DestroyList(Sqlist &L){
	if(L.elem)
	delete []L.elem;//释放存储空间
}
//创建顺序表L
bool CreateList(Sqlist &L){
    int x,n;
    cout<<"请输入n个元素:"<<endl;
    cin>>n;
    cout<<"请输入"<<n<<"个元素:"<<endl;
    for(int i = 0;i < n; i++){
        if(L.length==Maxsize){
            cout<<"顺序表已经满啦!";
            return false;
        }
        cin>>x;
        L.elem[i] = x;
        L.length++;
    }
}

//判断位置是否合法,合法就获取 i 位置的元素
bool GetElem(Sqlist &L,int i,int &e){
    if(i < 1||i > L.length) return false;
    //获取第 i 个位置的元素值
    e = L.elem[i-1];
    return true;
}
//根据元素值获取它在顺序表中的位置
int LocateELem(Sqlist &L,int x){
    for(int i = 0; i < L.length;i++){
        if(L.elem[i]==x){
            return i+1;
        }
        return -1;
    }
}
//增删改查
bool ListInsert_Sq(Sqlist &L,int i,int e){
	if(i<1||i>L.length+1) return false;	 //i值不合法
	if(L.length==Maxsize) return false; //存储空间已满
	for(int j=L.length-1;j>=i-1;j--)
	    L.elem[j+1]=L.elem[j];   //从最后一个元素开始后移,直到第i个元素后移
	L.elem[i-1]=e;              //将新元素e放入第i个位置
	L.length++;		     	//表长增1
	return true;
}

bool ListDelete_Sq(Sqlist &L,int i,int &e){
	if((i<1)||(i>L.length)) return false;//i值不合法
	e=L.elem[i-1];   //将欲删除的元素保留在e中
	for(int j=i;j<=L.length-1;j++)
		L.elem[j-1]=L.elem[j]; //被删除元素之后的元素前移
	L.length--; //表长减1
	return true;
}

void print(Sqlist L){
	cout<<"输出顺序表"<<endl;
	for(int j=0;j<=L.length-1;j++)
	    cout<<L.elem[j]<<"   ";
	cout<<endl;
}

int GetLength(Sqlist &L){

    return L.length;
}
void Empty(Sqlist &L){
    if(L.length<=0){
        cout<<"顺序表为空!"<<endl;
    }else{
        cout<<"顺序表不为空!"<<endl;
    }
}
int main(){
    Sqlist L;int i,e,x;
    cout<<"1. 初始化\n";
	cout<<"2. 创建\n";
	cout<<"3. 取值\n";
	cout<<"4. 查找\n";
	cout<<"5. 插入\n";
	cout<<"6. 删除\n";
	cout<<"7. 输出\n";
	cout<<"8. 销毁\n";
	cout<<"0. 退出\n";
	int choose=-1;
	while(choose!=0){
        cout<<"请选择:";
		cin>>choose;
		switch(choose){
		    case 1://初始化顺序表
		        cout<<"顺序表初始化..."<<endl;
		        if(InitList(L))
                    cout<<"顺序表初始化成功!"<<endl;
                else
                    cout<<"顺序表初始化失败!"<<endl;
		        break;
		     case 2://创建顺序表
		        cout<<"顺序表创建..."<<endl;
		        if(CreateList(L))
                    cout<<"顺序表创建成功!"<<endl;
                else
                    cout<<"顺序表创建失败!"<<endl;
                break;
            case 3://取值
                cout<<"输入整型数i,取第i个元素输出"<<endl;
                cin>>i;
                if(GetElem(L,i,e))
                    cout<<"第i个元素是: "<<e<<endl;
                else
                    cout<<"顺序表取值失败!"<<endl;;
                cout<<"第i个元素是: "<<e<<endl;
                break;
            case 4://查找
                cout<<"请输入要查找的数x:";
                cin>>x;
                if(LocateELem(L,x)==-1)
                    cout<<"查找失败!"<<endl;
                else
                    cout<<"查找成功!"<<endl;
                break;
            case 5://插入
                cout<<"请输入要插入的位置和要插入的数据元素e:";
                cin>>i>>e;
                if(ListInsert_Sq(L,i,e))
                    cout<<"插入成功!"<< endl;
                else
                    cout<<"插入失败!"<<endl;
                break;
             case 6://删除
                cout<<"请输入要删除的位置i:";
                cin>>i;
                if(ListDelete_Sq(L,i,e))
                    cout<<" 删除成功!"<<endl;
                else
                    cout<<"删除失败!"<<endl;
                break;
            case 7://输出
                print(L);
                break;
            case 8://销毁
                cout<<"顺序表销毁..."<<endl;
                DestroyList(L);
                break;
        }
	}
    return 0;
}
posted @ 2022-04-06 07:30  阿海是个呆瓜  阅读(77)  评论(0)    收藏  举报