顺序表基本操作(C 版)

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

for(i=0;i<n;i++) cin>>L.elem[i]; L.length=n; return OK; } //----------查找-----------------------int LocateElem_Sq(SqList L,ElemType e) { for(int i=0;i<L.length;i++) if(L.elem[i]==e) return i+1; return 0; } //-----------插入-----------------------Status ListInsert_Sq(SqList &L,int i,ElemType e) { //在顺序表 L 中的第 i 个位置之前插入新的元素 e //i 值的合法范围是 1<=i<=L.length+1 if(i<1||i>L.length+1) return ERROR;//i 值不合法 if(L.length==MAXSIZE) return ERROR;//当前存储空间已满 for(j=L.length-1;j>=i-1;j--) L.elem[j+1]=L.elem[j];//插入位置及之后的元素后移 L.elem[i-1]=e;//将新元素 e 放入第 i 个位置 ++L.length;//表长增 1 return OK; } //-----------删除第 i 个元素-------------------------Status ListDelete_Sq(SqList &L,int i,ElemType &e) { //在顺序表 L 中删除第 i 个元素,并用 e 返回其值 //i 的值的合法范围是 1<=i<=L.length if((i<1)||(i>L.length)) return ERROR; e=L.elem[i-1];//将欲删除的元素保留在 e 中 for(j=i;j<=L.length-1;j++) L.elem[j-1]=L.elem[j]; --L.length; return OK; }
//----------主函数--------------------------int main() { int choice=0; int i; SqList L; ElemType e; InitList_Sq(L); cout<<"选项:"<<endl <<"1、创建"<<endl <<"2、查找"<<endl <<"3、删除"<<endl <<"4、插入"<<endl <<"5、退出"<<endl <<"请选择:"; while(choice!=5) { cin>>choice; switch(choice) {
ቤተ መጻሕፍቲ ባይዱ
//-----------删除值为 e 的结点----------------------Status ListDel_Sq(SqList &L,int e) { int j; int temp=0; for(int i=0;i<L.length;i++) { if(L.elem[i]==e) { j=i+1; for(int k=j;k<=L.length-1;j++) L.elem[j-1]=L.elem[j]; --L.length; i--; temp=1; } } if(temp==0) cout<<"没有该元素"<<endl; return OK; }
case 1: Build_Sq(L); break; case 2: int n; cout<<"值为:"; cin>>e; n=LocateElem_Sq(L,e); cout<<"该元素的位置:"<<n<<endl; break; case 3: { int choice1; cout<<"1、删除第 i 个元素"<<endl <<"2、删除元素为 e 的元素"<<endl <<"请选择:"; cin>>choice1; switch(choice1) { case 1: cout<<"元素位置:"; cin>>i; ListDelete_Sq(L,i,e); cout<<e<<"被删除"<<endl; break; case 2: cout<<"元素值为:"; cin>>e; ListDel_Sq(L,e); cout<<e<<"被删除"<<endl; break; default: break; } break; } case 4: { cout<<"位置:"; cin>>i; cout<<"值为:"; cin>>e; ListInsert_Sq(L,i,e); break;
#include<iostream> using namespace std; int OVERFLOW=-2; int ERROR=0; int OK=1; int j; //---------顺序表的存储结构---------#define MAXSIZE 10 //当前顺序表可能达到的最大长度 #define LISTINCREMENT 5 typedef int ElemType; typedef int Status; typedef struct { ElemType *elem;//存储空间的基地址 int length;//当前长度,表中有多少个元素 int listsize; }SqList; //----------初始化-------------------Status InitList_Sq(SqList &L) { //构造一个空的顺序表 L L.elem=new ElemType[MAXSIZE];//为顺序表分配一个大小为 MAXSIZE 的数组空间 if(!L.elem) exit(OVERFLOW);//存储空间分配失败 L.listsize=MAXSIZE; L.length=0;//空表的长度为 0 return OK; } //----------建立-----------------------Status Build_Sq(SqList &L) { int i,n; cout<<"请输入要建立的顺序表的中元素的个数:"<<endl; cin>>n; if(n>MAXSIZE)//如果 n 的值大于当前空间 { L.elem=new ElemType[n+LISTINCREMENT]; if(!L.elem) exit(OVERFLOW); L.listsize=n+LISTINCREMENT; } cout<<"请输入这 n 个元素:"<<endl;
} case 5: return 0; default: break; } cout<<"请选择:"; } return 0; }
相关文档
最新文档