今日完成pta上两道题
1,本题要求实现六个函数,顺序表为整型数据,可实现输入、输出、取值、查找、插入、删除功能。
1 void ListOutput(SqList L){ 2 for(int i=0;i<L.length;i++){ 3 cout<<L.elem[i]<<" "; 4 } 5 cout<<endl; 6 } 7 void ListInput(SqList &L){ 8 int n; 9 while(1){ 10 cin>>n; 11 if(n>=1 && n<=MAXSIZE){ 12 13 break; 14 } 15 } 16 for(int i=0;i<n;i++){ 17 int number; 18 cin>>number; 19 L.elem[i]=number; 20 } 21 L.length=n; 22 } 23 int GetElem(SqList L, int i, ElemType &e){ 24 if(i<=0 || i>L.length){ 25 return 0; 26 }else{ 27 e=L.elem[i-1]; 28 return 1; 29 } 30 } 31 int LocateElem_Sq(SqList L, double e){ 32 for(int i=0;i<L.length;i++){ 33 if(L.elem[i]==e){ 34 return i+1; 35 } 36 } 37 return 0; 38 } 39 int ListInsert_Sq(SqList &L, int i, ElemType e){ 40 if(i<=0 || i>L.length){ 41 return 0; 42 } 43 if(L.length==MAXSIZE){ 44 return 0; 45 } 46 for(int j=L.length-1;j>=i-1;j--){ 47 L.elem[j+1]=L.elem[j]; 48 } 49 L.elem[i-1]=e; 50 L.length++; 51 return 1; 52 } 53 int ListDelete_Sq(SqList &L, int i){ 54 if(i<=0 || i>L.length){ 55 return 0; 56 } 57 for(int j=i;j<=L.length-1;j++){ 58 L.elem[j-1]=L.elem[j]; 59 } 60 L.length--; 61 return 1; 62 }
标签:总结,return,9.16,elem,int,length,iL,SqList From: https://www.cnblogs.com/Lyh3012648079/p/17707229.html