数据结构02-顺序表

发布时间:2022-06-20 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了数据结构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; @R_126_2563@){
        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;
}

脚本宝典总结

以上是脚本宝典为你收集整理的数据结构02-顺序表全部内容,希望文章能够帮你解决数据结构02-顺序表所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。